As a senior security CMS website operator, I am very clear about the core role of templates in content presentation.An flexible and efficient template can not only enhance the user experience, but also help us achieve accurate content distribution and management.forloop andifThe condition judgment tag is one of the two cornerstones for building dynamic and interactive web pages. They make the content no longer static text blocks, but can intelligently display according to data changes.
Understand the control tags in the AnQi CMS template
The template engine of Anqi CMS adopts syntax similar to Django and Blade, which makes it very easy for content developers to get started. In the template, we use double curly braces{{变量}}Print the value of the variable, and for logical control tags such as loops and conditional judgments, we use single curly braces and percent signs{% 标签 %}These control tags must appear in pairs, that is, there must be a start tag as well as a corresponding end tag, for example{% if 条件 %} ... {% endif %}or{% for item in 集合 %} ... {% endfor %}.
UtilizeforLoop through the data set
forLoop tags are a key tool in Anqi CMS templates used for iterating over arrays, slices, or any iterable data collection.It allows us to dynamically display a series of contents, such as article lists, product albums, category navigation, or friend links, etc.
A basicforThe loop syntax is as follows:
{% for item in 变量集合 %}
<!-- 在这里输出每一个 item 的内容 -->
{% endfor %}
Inside the loop,itemrepresented each element of the current iteration set, we can access its internal fields by{{ item.属性名 }}to access its internal fields. For example, when we usearchiveListtags to get a list of articles,itemThis can represent each article object, and we can easily obtain the title, link, and other information of the article:
{% archiveList archives with type="list" limit="10" %}
{% for article in archives %}
<div class="article-item">
<h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
<p>{{ article.Description }}</p>
<span>发布日期:{{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
</div>
{% endfor %}
{% endarchiveList %}
Anqi CMS'sforThe loop also provides some built-in variables and modifiers to enhance its flexibility. For example,forloop.CounterCan retrieve the current loop index (starting from 1),forloop.RevcounterThen get the number of remaining elements. This is very useful when special styles need to be applied to the first or last element in a list.
{% archiveList archives with type="list" limit="5" %}
{% for article in archives %}
<div class="article-item {% if forloop.Counter == 1 %}first-item{% endif %}">
<h3>{{ forloop.Counter }}. <a href="{{ article.Link }}">{{ article.Title }}</a></h3>
</div>
{% endfor %}
{% endarchiveList %}
When we need to traverse the data in a different order,reversedandsortedModifiers can be useful.reversedIt will traverse the collection in reverse order.sortedIt will try to sort the numeric collection.
{% for num in numberList sorted %}
<!-- 按升序输出数字 -->
{% endfor %}
{% for article in archives reversed %}
<!-- 按倒序输出文章 -->
{% endfor %}
In addition,forThe loop also supportsemptyTags. When the collection being traversed is empty or undefined,emptyThe content within the label will be rendered, not the content within the loop. This is very useful for elegantly handling the situation where 'no content to display' is the case:
{% archiveList archives with type="list" categoryId="10" limit="5" %}
{% for article in archives %}
<div class="article-item">
<h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
</div>
{% empty %}
<p>当前分类下暂无文章。</p>
{% endfor %}
{% endarchiveList %}
ApplicationifConditional judgment controls the display of content
ifThe conditional judgment tag is the core of logical control in the Anqi CMS template.It allows us to decide which parts of the page should be rendered and which should be hidden based on specific conditions.This is particularly important when building responsive layouts, displaying different content based on user permissions, or handling missing data.
ifThe basic structure of the label includes{% if 条件 %}/{% elif 其他条件 %}(optional) and{% else %}(optional), and){% endif %}End:
{% if 条件1 %}
<!-- 条件1为真时显示的内容 -->
{% elif 条件2 %}
<!-- 条件1为假且条件2为真时显示的内容 -->
{% else %}
<!-- 所有条件都为假时显示的内容 -->
{% endif %}
The condition expression can include various comparison operators (such as)==/!=/>/</>=/<=), logical operators (such as)and/or/not),and check if the variable exists (i.e., its 'truthiness').
For example, we can decide whether to display the image based on whether the article has a thumbnail:
{% archiveList archives with type="list" limit="1" %}
{% for article in archives %}
<h3>{{ article.Title }}</h3>
{% if article.Thumb %}
<img src="{{ article.Thumb }}" alt="{{ article.Title }}">
{% else %}
<p>本文没有缩略图。</p>
{% endif %}
<p>{{ article.Description }}</p>
{% endfor %}
{% endarchiveList %}
When dealing with more complex logic,elifandelseProvided the ability to make multiple branch choices. For example, based on the article'sFlagattributes to display different tags:
{% archiveDetail archive %}
<h1>{{ archive.Title }}</h1>
{% if "h" in archive.Flag %}
<span class="flag-hot">热点</span>
{% elif "c" in archive.Flag %}
<span class="flag-recommend">推荐</span>
{% else %}
<span class="flag-normal">普通</span>
{% endif %}
<p>{{ archive.Content|safe }}</p>
{% endarchiveDetail %}
We can also usenotOperator to determine if a condition is false. For example, to check if the website is closed:
{% system siteStatus with name="SiteStatus" %}
{% if not siteStatus %}
<p>欢迎访问我们的网站!</p>
{% else %}
<p>{{ system with name="SiteCloseTips" }}</p>
{% endif %}
CombineforandifImplement advanced customization
forloop andifThe true power of conditional judgment tags lies in their combination use.By nesting conditional judgments within loops, we can provide a refined content display for each element in the data set based on its own properties or status.
An English translation of 'auto' is 'English'.
{% categoryList categories with moduleId="1" parentId="0" %}
<ul class="main-nav">
{% for category in categories %}
<li>
<a href="{{ category.Link }}">{{ category.Title }}</a>
{% if category.HasChildren %}
<ul class="sub-nav">
{% categoryList subCategories with parentId=category.Id %}
{% for subCategory in subCategories %}
<li><a href="{{ subCategory.Link }}">{{ subCategory.Title }}</a></li>
{% endfor %}
{% endcategoryList %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>
{% endcategoryList %}
Another example is to apply different styles to odd or even rows of articles while traversing the list of articles, or display a "hot" tag based on the number of views of the articles:
{% archiveList archives with type="list" limit="10" %}
{% for article in archives %}
<div class="article-card {% if forloop.Counter % 2 == 0 %}even{% else %}odd{% endif %}">
<h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
{% if article.Views > 1000 %}
<span class="badge-hot">热门</span>
{% endif %}
<p>{{ article.Description }}</p>
</div>
{% endfor %}
{% endarchiveList %}
To avoid unnecessary blank lines during template rendering, we can use{%-and-%}syntax to control the whitespace before and after tags. Use at the start of the tag{%-It will remove all leading whitespace and use it at the end of the tag.-%}Will remove all trailing whitespace.This is particularly important for keeping the generated HTML code neat, although it is not a direct logic tag, but it is an important detail that works in collaboration with them.
Concluding remarks
In the template world of AnQi CMS.forloop andifThe conditional judgment tag is the foundation for dynamic content display and fine-grained control.By mastering their usage, we can build highly flexible, powerful, and user-friendly website interfaces.These tags can provide solid technical support for our content operations, helping us attract and retain users better, whether it's displaying complex article lists, building multi-level navigation, or adjusting page elements based on specific conditions.
Common Questions (FAQ)
1. How toforIn the loop, judge whether the current one is the last element?
You can useforloop.LastBoolean variable to judge. When the current iteration is the last element in the loop,forloop.Lasthas a value oftrue. For example:
{% for item in collection %}
<div class="item{% if forloop.Last %} last-item{% endif %}">
{{ item.Title }}
</div>
{% endfor %}
2. When the list is empty, how to display a friendly prompt instead of a blank?
InforYou can use{% empty %}Labels are used to handle this situation. When you try to iterate over an empty or undefined collection,{% empty %}and{% endfor %}the content between the brackets will be rendered, not the loop body itself.
{% archiveList articles %}
{% for article in articles %}
<p>{{ article.Title }}</p>
{% empty %}
<p>抱歉,目前没有可显示的文章。</p>
{% endfor %}
{% endarchiveList %}
3. How toifHow to judge multiple conditions at the same time in a statement?
You can use logical operatorsandandorto combine multiple conditions.andAll conditions must be true,orAt least one condition must be true.notto negate a condition.
auto English
<p>这是一篇高浏览量热点文章!</p>
{% elif article.Views > 500 or “c” in article.Flag %}
<p>这篇文章值得关注。</p>