In the Anqi CMS template world,ifandforTags are the foundation for building dynamic and flexible web pages. They give templates the ability to display content based on different data conditions and efficiently loop through datasets, making your website content vividly presented to visitors.The AnQi CMS adopts a template engine syntax similar to Django, which makes it easy for you, who is familiar with web development, to get started, through double curly braces{{变量}}Get data, while conditional judgment and loop control are used{% 标签 %}Syntax.

UseifTags implement conditional display of dynamic content

ifThe core function of the tag is to decide whether to display a certain part of the page content based on specific conditions. Its syntax structure is very intuitive, supporting single condition judgment and multi-branch conditions, such as: {% if 条件 %} ... {% elif 其他条件 %} ... {% else %} ... {% endif %}

In practical applications,ifThe label's 'condition' can be very diverse. You can perform boolean judgments, such as checking if a switch is turned on to display or hide a specific function module.Data variable non-empty checks are also very common, such as{% if archive.Thumb %}Can determine whether the current document has a thumbnail, if it does, it should be displayed, if not, consider displaying a default placeholder to avoid blank areas on the page that are missing content.

Comparison operator (==/!=/</>/<=/>=)You can 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 set (such as an array or string), which is very useful for content filtering based on user permissions or article tags.

Skillfully useifLabels can help you achieve many functions: for example, displaying personalized welcome messages for logged-in users, or based on the 'recommended' attribute of articles (such asarchive.Flag == "h"This represents the headline and adds a prominent mark. This conditional display mechanism is the key to creating a personalized, interactive website experience.

UtilizeforTags implement the cyclic display of content.

When you need to display a series of similar contents,fortags come into play. It can iterate over each item in a collection and present each one according to the template definition. Its basic syntax is:{% for item in collection %} ... {% endfor %}

In each iteration,itemthe variable will represent the current element of the collection, and you can access its properties by{{item.Title}}/{{item.Link}}and so on. Anqi CMS'sforLoops also provide several very useful built-in variables:

  • forloop.CounterThis indicates 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.Revcounter: Indicates the number of iterations remaining after the loop ends.

A noteworthy feature is thatemptyclause. When the collection you are looping through is empty,emptyThe content under the clause will be executed, which is more concise and elegant than using it traditionally first.ifIt is more concise and elegant to loop over the empty set before the judgment.{% for item in archives %} ... {% empty %} <div>暂无相关内容。</div> {% endfor %}

forTags play an important role in building website parts: it can traverse and display article lists, product lists, navigation menus, friend links, and even image groups in custom fields. ThroughreversedModifiers can enable reverse traversal,sortedModifiers can sort the collection according to specific rules, meeting more diverse display needs.

ifwithforcollaborative application: building complex dynamic pages

In actual template development,ifandforLabels are often combined to achieve more complex logic and richer dynamic effects.

For example, in the loop of the article list, you may want to add a special "latest" tag or a unique style to the first article. At this point, you can combineforloop.CounterandifTag implementation:{% for item in archives %} <li{% if forloop.Counter == 1 %} class="first-article"{% endif %}> <a href="{{ item.Link }}">{{ item.Title }}</a> </li> {% endfor %}

Or perhaps, when building a multi-level category navigation, you may need to determine whether the current category has subcategories: if it does, then display the subcategory list; if not, then display the article list under the category. In this case,ifandforNested use 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 %}

Moreover, in order to make the template code cleaner, Anqin CMS also supports using hyphens at the beginning or end of tagsifandforUsing hyphens at the beginning or end of tags-Remove the whitespace taken up by logical tags. For example,{%- if condition %}or{% endfor -%}This is very useful when generating HTML structures that do not require additional whitespace.

By flexible applicationifandforThese powerful template tags allow you to organize data effectively, make conditional judgments based on business logic, and present it dynamically and expressively on the front end of the website, thereby building a functional and user-friendly Anqi CMS website.

Frequently Asked Questions (FAQ)

  1. How to add special styles or content to items that meet specific conditions in a loop (such as the first, last, or even-numbered items)?You can useforloop.Counterandforloop.RevcounterThese built-in variables work togetherifTo implement a tag. For example,{% if forloop.Counter == 1 %}It can be determined whether it is the first item,{% if forloop.Last %}Or if supported{% if forloop.Revcounter == 1 %}It can be determined whether it is the last item. For even or odd items, it can be determined through{% if forloop.Counter % 2 == 0 %}to judge.

  2. When passedforHow to avoid the page displaying blank when the label traversal list may be empty, but instead give the user a friendly prompt?In this case,forlabel'semptyThe clause is an ideal solution. WhenforWhen the data collection has no content,{% empty %}and{% endfor %}the content between them will be executed. For example:{% for item in articles %} ... {% empty %} <div>抱歉,暂时没有找到任何文章。</div> {% endfor %}.

  3. ifWhat types of conditional judgments can the tag make? Besides simple true or false, what else can I do? ifTags support various powerful conditional judgments. You can make precise numerical or string comparisons(==/!=), size comparisons(>/</>=/<=),as well as checking 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 a string). Complex logic can be achieved byandandorConnect multiple conditions.