In AnQiCMS template development, we often need to display a series of contents, such as article lists, product lists, or navigation menus.But often, we do not want to display all the data at once, but rather hope to skip a part of it cleverly based on certain conditions, so that the final content displayed is more accurate and meets the user's needs.AnQiCMS uses a syntax similar to the Django template engine, which provides a powerful and flexible tool for us to implement this conditional skipping.
To implementforIn a loop, to skip the display of certain items based on specific conditions, the most core tool isifa logical judgment tag. By wrapping the HTML segment that needs to be displayed with conditionsifIn the tag, we can easily control the visibility of each item.
Core mechanism:iflogical judgment tag
Imagine you are iterating over an article list and you want to display only those articles that are not marked as "recommended". At this point, you can make use of{% if ... %}the structure. AnQiCMS'sarchiveListEach article (or other model content) obtained by the label usually has oneFlagfield to mark its recommended attributes, such ascrepresenting a recommendation. If aitem(the current item in the loop) of theFlagField is not equalcOnly then do we display it:
{% 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 key role. It checks the currentitemofFlagwhether it is not equal to"c". If the condition is true, then<li>The content inside the tag 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 to)>(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 features.This means you can set skip conditions based on any built-in or custom field of an article, product, or single page.
For example, if you want to skip articles that do not have a thumbnail set, you can do this:
{% 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.ThumbDoes the field have 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 a custom field, assume you have added a field namedEditorRecommendA boolean field used to mark articles recommended by the editor. Then, you can display all non-editor recommended 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 filters
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 substring, which is very useful when skipping based on keywords.
Suppose you have a single-page list, but you don't want to display any page with a title 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 title of the current page contains the keyword "Privacy Policy". If it does,notthe operator will change it tofalse, thus skipping the item.
Another example is usinglengthA filter to determine the length of content. For example, you only want to display those briefDescription) articles that exceed 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 %}
Practical case: 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 with an ID of
5The navigation item is temporary or should not 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 display those category items that contain child categories, or vice versa, only display leaf categories: “`twig {% categoryList categories with moduleId=“1” parentId=“0” %}
{% for item in categories %} {% if item.HasChildren %} {# 只显示有子分类的项 #} <li><a href="{{ item.Link }}">{{ item.