In AnQiCMS template development, we often need to display a series of contents, such as article lists, product lists, or navigation menus.But many times, we do not want to display all the data at once, but rather hope to skip a part of it cleverly based on certain specific conditions, making the final content displayed more accurate and in line with user needs.AnQiCMS uses syntax similar to Django template engine, which provides a powerful and flexible tool for us to implement this conditional skip.
To implementforSkipping the display of certain items based on specific conditions within a loop is the most core tool, which isifLogical judgment tags. By wrapping the HTML fragments that need to be conditionally displayed inifIn tags, we can easily control the visibility of each item.
Core Mechanism:ifLogical Judgment Tag
Imagine you are iterating over a list of articles and you want to display only those that are not marked as 'recommended'. At this point, you can utilize{% if ... %}the structure. AnQiCMS'sarchiveListEach article (or other model content) obtained by the tag usually has aFlagfield used to mark its recommended properties, such ascrepresenting recommendation. If a certainitem(the current item in the loop) of theFlagField is not equalcand then it will be displayed:
{% archiveList archives with type="list" limit="10" showFlag=true %}
{% for item in archives %}
{% if item.Flag != "c" %}
<li class="article-item">
<a href="{{ item.Link }}">
<h3>{{ item.Title }}</h3>
<p>{{ item.Description }}</p>
</a>
</li>
{% endif %}
{% endfor %}
{% endarchiveList %}
In the above example,{% if item.Flag != "c" %}This line of code plays a crucial role. It checks if the currentitemofFlagis not equal"c". If the condition is true, then<li>The tag and its content will be rendered; if the condition is false (i.e.,Flagequals"c"), then the entire<li>and its content will be skipped and will not appear on the final page.
Except!=(Not equal), you can also use other comparison operators:
==(Equal)>(Greater than)<(Less than)>=(Greater than or equal to)<=(Less than or equal to)and(Logical AND)or(Logical OR)not(Logical NOT)in(Contains)
These operators allow you to build very complex conditions.
Combine content attributes with custom fields
The flexibility of AnQiCMS lies in its powerful content model and custom field functionality.This means you can set skip conditions based on any built-in properties or custom fields of any article, product, or single page.
For example, if you want to skip articles that do not have a thumbnail set, you can do it this way:
{% archiveList archives with type="list" limit="10" %}
{% for item in archives %}
{% if item.Thumb %} {# 如果 item.Thumb 存在且不为空,则为 True #}
<li class="article-with-thumb">
<a href="{{ item.Link }}">
<img src="{{ item.Thumb }}" alt="{{ item.Title }}">
<h3>{{ item.Title }}</h3>
</a>
</li>
{% endif %}
{% endfor %}
{% endarchiveList %}
Here,{% if item.Thumb %}will checkitem.ThumbField has a value. If it does, show this item; if not, skip. Conversely, if you want to display only those articles without thumbnails, you can use{% if not item.Thumb %}.
For custom fields, suppose you added a field namedEditorRecommendThe boolean type field, used to mark articles as editor's picks. Then, you can display all non-editor's pick articles like this:
{% archiveList archives with type="list" moduleId="1" limit="10" %}
{% for item in archives %}
{% if not item.EditorRecommend %} {# 假设 EditorRecommend 是一个布尔值 #}
<li class="normal-article">
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
<p>{{ item.Description }}</p>
</li>
{% endif %}
{% endfor %}
{% endarchiveList %}
More flexible filtering: use the filter
AnQiCMS template engine also supports various filters, which can provide more advanced logic in conditional judgments. For example,containThe filter can determine if a string contains a certain substring, which is very useful when skipping based on keywords.
Assume you have a single-page list, but you don't want to display any pages with titles containing 'Privacy Policy' in the main navigation:
{% pageList pages %}
{% for item in pages %}
{% if not item.Title|contain:"隐私政策" %}
<li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
{% endif %}
{% endfor %}
{% endpageList %}
Here,item.Title|contain:"隐私政策"It will check if the current page title contains the keyword 'Privacy Policy'. If it does,notthe operator will turn it intofalse, thus skipping the item.
Another example is usinglengthFilter to determine the length of content. For example, you only want to display those summaries that.Description) Have more than 50 characters:
{% archiveList archives with type="list" limit="10" %}
{% for item in archives %}
{% if item.Description|length > 50 %}
<li class="detailed-description-article">
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
<p>{{ item.Description }}</p>
</li>
{% endif %}
{% endfor %}
{% endarchiveList %}
Practice cases: Common skip scenarios
Let's take a look at several common application scenarios:
Hide navigation items with specific IDs:If you have a navigation menu and one of the IDs is:
5The navigation item is temporary or not intended to be displayed in the current template:{% navList navs %} {% for item in navs %} {% if item.Id != 5 %} <li><a href="{{ item.Link }}">{{ item.Title }}</a></li> {% endif %} {% endfor %} {% endnavList %}Skip parent categories without subcategories:When displaying the category list, sometimes we only want to show those categories that contain subcategories, or conversely, only show leaf categories: “`twig {% categoryList categories with moduleId=“1” parentId=“0” %}
{% for item in categories %} {% if item.HasChildren %} {# 只显示有子分类的项 #} <li><a href="{{ item.Link }}">{{ item.