As a professional who deeply understands the operation of AnQiCMS, I know that template design plays a core role in the presentation of website content.Through the flexible application of conditional judgment and loop logic, we can transform static template files into a dynamic and intelligent content display platform, thereby better responding to user needs and enhancing the interactivity and user experience of the website.if/else) and loop (for)Logic label, helps you create a more expressive website.
Build dynamic content in AnQiCMS template: conditional judgment and loop logic
AnQiCMS's template engine syntax is similar to Django, which provides a friendly learning curve for developers familiar with the Python ecosystem. In the template, variables are passed through double curly braces{{变量}}Definition, while logical tags such as condition judgment and loop control use single curly braces and the percent sign{% 标签 %}are used to declare. Understanding and mastering these logical tags is the key to realizing dynamic content rendering.
Flexible condition judgment:if/elseApplication of tags
In website operation, we often need to decide whether to display a certain content block, use a certain style, or display different text information based on specific conditions. AnQiCMS'sif/elseLogic labels provide strong support for these scenarios.
basicifA statement is used to determine if an expression is true. If the condition is true, then execute{% if %}and{% endif %}Between the code blocks. For example, when we need to determine if the current document ID is a specific value, we can write it like this:
{% if archive.Id == 10 %}
<p>这是文档ID为10的文档的特殊内容。</p>
{% endif %}
In practical applications, conditions are often not just one, or need to handle multiple possibilities. At this point, we can introduceelif(else if abbreviation) andelseLabel.elifallow us to set multiple alternative conditions,elseThen it is the default handling when all conditions are not met.
For example, display different information based on the user's login status or the review status of the content:
{% if user.IsLoggedIn %}
<p>欢迎回来,{{ user.UserName }}!</p>
{% elif archive.Status == 0 %}
<p>该内容正在审核中,请耐心等待。</p>
{% else %}
<p>请登录以查看更多信息。</p>
{% endif %}
Conditional expressions support various operators, including comparison operators (==,!=,<,>,<=,>=)、logical operators(and,or,not)as well as member operators(inThis means you can build very complex logic to control the display of content. For example, determine if a value is in a certain set:
{% if category.Id in [1, 5, 8] %}
<p>这个分类属于特色分类。</p>
{% endif %}
{% if not archive.Thumb %}
<p>该文档没有缩略图。</p>
{% endif %}
Always be careful, alliftags must start with{% endif %}End, insteadelifandelseTags do not require separate closing tags. When writing conditional judgments, maintaining paired tags and correct nesting structure is the key to avoiding template rendering errors.
Powerful data traversal:forApplication of loop tags
Content management system has a major feature of presenting batch contents, such as article lists, product lists, category navigation, etc. AnQiCMS'sforThe loop tag is designed for this kind of scenario, it allows us to iterate over array, slice, and other collection types of data, and process each element.
The most basicforLoop structure as follows, it will iteratearchivesEvery item in the setitem:
{% for item in archives %}
<li>
<a href="{{ item.Link }}">{{ item.Title }}</a>
</li>
{% endfor %}
Inside the loop, we can use dot syntax (item.属性名)to access various attributes of the current element. AnQiCMS provides many built-in tags such asarchiveList,categoryList,navListThese tags return data that is usually availableforfor iteration collections.
To provide more control and information in the loop,forLoop also provides some special variables:
forloop.Counter: represents the current loop index, starting from 1.forloop.Revcounter: represents the reverse index of the current loop, starting from the length of the collection.
For example, add a special style class to the first item in the list:
{% for item in archives %}
<li {% if forloop.Counter == 1 %}class="active"{% endif %}>
<a href="{{ item.Link }}">{{ forloop.Counter }}. {{ item.Title }}</a>
</li>
{% endfor %}
When dealing with list data, we may also need to control the order of the data.forLoop supportreversedandsortedModifier:
reversed:Traverse the collection in reverse order.sorted:Sort the collection before traversing.
{# 倒序遍历 #}
{% for item in archives reversed %}
<li>{{ item.Title }}</li>
{% endfor %}
{# 排序后遍历 #}
{% for item in archives sorted %}
<li>{{ item.Title }}</li>
{% endfor %}
Sometimes, when the collection is empty, we want to display a prompt message instead of an empty list.forloop provides aemptyHandle this situation with a block:
{% for item in archives %}
<li>{{ item.Title }}</li>
{% empty %}
<li>目前还没有相关内容。</li>
{% endfor %}
In addition,cycleTags inforThis is very useful in loops, it allows you to output a series of values in order at each iteration of the loop. This is often used to add alternating background colors or CSS classes to list items:
{% for item in archives %}
<li class="{% cycle 'even' 'odd' %}">
{{ item.Title }}
</li>
{% endfor %}
Points to note in practice
When writing AnQiCMS templates, whether usingif/elseOrforloop, it should always be remembered that AnQiCMS template syntax is strictly case-sensitive. Variable names (such asarchive.Id,item.Title) and tag names (such asif,for,endfor都必须与文档中定义的精确匹配。任何细微的拼写或大小写错误都可能导致模板解析失败或显示异常。
To maintain the neatness and maintainability of template code, it is crucial to properly nest conditional judgments and loop structures. At the same time, if the line breaks or whitespace generated by logical tags in the template affect the layout of the final page, you can use the minus sign-Remove leading and trailing blank lines before and after the label, for example{%- if condition %}or{{ variable -}}.
Through proficiently mastering these conditional judgment and loop logic tags, you will be able to fully utilize the powerful functions of AnQiCMS to build highly dynamic, responsive to user needs, and easy to manage and optimize websites.
Frequently Asked Questions
AnQiCMS template'sifDoes the label support complex logical conditions?Yes, the AnQiCMS templateifThe label supports complex logical conditions. You can combine comparison operators (==,!=,<,>,<=,>=)、logical operators(and,or,not)as well as member operators(in)to construct multiple judgments. For example, you can check{% if item.Status == 1 and item.Views > 100 %}to filter items that meet two conditions.
InforIn the loop, how do I get the current loop index (e.g., determine if it is the first item or the last item)?In AnQiCMSforYou can useforloop.CounterTo get the current loop index (starting from 1), or useforloop.RevcounterTo get the reverse index. This is very useful for adding specific styles or logic to list items, such as{% if forloop.Counter == 1 %}to determine if it is the first item.
if my list data may be empty,forHow can a loop elegantly handle this situation?When yourforloop iterates over an empty collection, you can use{% empty %}a label to provide an alternative content block. If the collection does not contain any elements,{% for %}The content within the tag will not be rendered, instead,{% empty %}and{% endfor %}is displayed, allowing you to elegantly show the user a "no content" prompt.