In the template world of AnQi CMS.ifandforLabels are the foundation for building dynamic and flexible pages.They enable templates to display content based on different data conditions and efficiently loop through datasets, allowing your website content to be presented vividly to visitors.{{变量}}Get data, while conditional judgment and loop control are implemented using{% 标签 %}syntax.
Useiftags to implement conditional display of dynamic content
ifThe core function of the label is to determine whether a certain part of the content on the page is displayed based on specific conditions. Its syntax structure is very intuitive, supporting single condition judgment, as well as multi-condition branching, such as: {% if 条件 %}
...
{% elif 其他条件 %}
...
{% else %}
...
{% endif %}
In actual application,ifThe "condition" of the label can be very diverse.You can perform boolean value judgments, such as checking if a switch is turned on or off to display or hide a specific functional module.{% if archive.Thumb %}Can determine if the current document has a thumbnail, if so, display it, and if not, consider displaying a default placeholder image to avoid blank areas on the page that may appear to be missing content.
Comparison operators (==/!=/</>/<=/>=)Let you control the display based on the comparison result of numbers or strings. For example,{% if archive.Id == 10 %}You can display unique information for a specific document with ID 10 in the template. In addition,inandnot inThe operator is used to check if a value exists in a collection (such as an array, string), which is very useful when filtering content based on user permissions or article tags.
ingeniously utilizeifLabels can help you achieve many functions: such as, displaying personalized welcome messages to logged-in users, or according to the "recommend" attribute of the article (such asarchive.Flag == "h"This is a headline, add a prominent mark. This conditional display mechanism is the key to creating a personalized, interactive website experience.
UtilizeforTag to achieve the cyclic display of content
When you need to display a series of similar contents,fortags come in handy. It can iterate over each item in a collection and present them one by one according to the template definition. Its basic syntax is:{% for item in collection %}
...
{% endfor %}
In each loop iteration,itemthe variable will represent the current element in the set, and you can access its properties through,{{item.Title}}/{{item.Link}}etc. Safe CMS'sforThe loop also provides several very useful built-in variables:
forloop.CounterThis represents the current loop count, starting from 1. This is especially convenient when adding special styles or numbering to the first item in a list.forloop.RevcounterThe current loop iteration count remaining after the loop ends.
A feature worth paying attention to is.emptyThe clause. When the collection you loop over is empty,emptyThe content under the clause will be executed, which is more concise and elegant than traditionally usingifJudging whether the collection is empty before the loop to be more concise and elegant.{% for item in archives %}
...
{% empty %}
<div>暂无相关内容。</div>
{% endfor %}
forThe label plays an important role in building the various parts of a website: it can traverse and display article lists, product lists, navigation menus, friend links, and even image groups in custom fields. ThroughreversedThe modifier can enable reverse iteration, andsortedthe modifier can sort the collection according to specific rules, meeting more diverse display needs.
ifWithforcollaborative applications: building complex dynamic pages
In actual template development,ifandforLabels are usually used together to achieve more complex logic and richer dynamic effects.
For example, in the loop of article lists, you may want to add a special "latest" tag or unique style to the first article. At this point, you can combineforloop.CounterandifTags:{% for item in archives %}
<li{% if forloop.Counter == 1 %} class="first-article"{% endif %}>
<a href="{{ item.Link }}">{{ item.Title }}</a>
</li>
{% endfor %}
Or, when building multi-level category navigation, you may need to determine whether the current category has subcategories: if there are, then display the subcategory list; if not, then display the article list under the category.ifandforThe nested usage is indispensable:{% categoryList categories with parentId="0" %}
{% for category in categories %}
<div>
<h3>{{ category.Title }}</h3>
{% if category.HasChildren %}
{% categoryList subCategories with parentId=category.Id %}
<ul>{% for subCategory in subCategories %}<li><a href="{{ subCategory.Link }}">{{ subCategory.Title }}</a></li>{% endfor %}</ul>
{% endcategoryList %}
{% else %}
{% archiveList articles with categoryId=category.Id limit="5" %}
<ul>{% for article in articles %}<li><a href="{{ article.Link }}">{{ article.Title }}</a></li>{% endfor %}</ul>
{% endarchiveList %}
{% endif %}
</div>
{% endfor %}
{% endcategoryList %}
In addition, to make the template code more tidy, Anqi CMS also supports the usage of hyphens at theifandforbeginning or end of the tag-Remove the blank lines occupied by logic labels. For example,{%- if condition %}or{% endfor -%}This is very useful when generating HTML structures that do not require additional whitespace.
By using flexibilityifandforThese powerful template tags allow you to organize data effectively, make conditional judgments based on business logic, and present it dynamically and expressively on the website frontend, thereby building a feature-rich and user-friendly CMS website.
Common Questions (FAQ)
How to add special style or content to items that meet specific conditions in a loop (such as the first, last, or even-numbered items)?You can use
forloop.Counterandforloop.RevcounterThese two built-in variables work togetherifLabel to implement. For example,{% if forloop.Counter == 1 %}Can determine if it is the first item,{% if forloop.Last %}(if supported) or{% if forloop.Revcounter == 1 %}Can determine if it is the last item. For even or odd items,{% if forloop.Counter % 2 == 0 %}for judgment.When through
forHow to avoid the page from displaying blank when the list to be traversed by the label may be empty, but instead give the user a friendly prompt?In this case,forTagsemptyThe clause is an ideal solution. WhenforThe data collection is empty,{% empty %}and{% endfor %}the content between them will be executed. For example:{% for item in articles %} ... {% empty %} <div>抱歉,暂时没有找到任何文章。</div> {% endfor %}.ifWhat types of conditional judgments can the tags make? Besides simple true/false, what else can I do?ifTag supports various powerful conditional judgments. You can perform precise comparisons of numbers or strings (==/!=) size comparisons (>/</>=/<=),and check if a variable exists or is not empty. Moreover, you can useinandnot into determine if a value is contained within a collection (such as an array or string). Complex logic can be achieved byandandorConnect multiple conditions.