As a professional well-versed in the operation of AnqiCMS, I know that template development is the core link in building an attractive and functional website.In the process of content creation, editing, and publishing, flexibly using logical judgments and loop tags in template language can make our website content dynamically presented and adaptable to diverse business scenarios, thereby better meeting reader needs and enhancing user experience.if/forThe flexible application of logical tags.
The foundation of dynamic content display:forLoop tags
In the AnqiCMS template system,forLoop tags are the core tools we use to handle list and collection data. Whether it's an article list, product categories, navigation menus, or tag clouds,forLoops can help us present the data obtained from the backend in a structured way on the front-end page.
AnqiCMSforThe tag syntax is concise and powerful. When we need to iterate over a collection, we usually use it like this:{% for item in collection %} ... {% endfor %}Here,collectionIt can be fromarchiveList/categoryList/navListThe data array obtained from tags. For example, to display the latest few article titles, we can combinearchiveListTags:
{% archiveList articles with type="list" limit="5" %}
{% for article in articles %}
<div class="latest-article">
<a href="{{ article.Link }}">{{ article.Title }}</a>
<span>发布日期:{{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
</div>
{% endfor %}
{% endarchiveList %}
Inside the loop, AnqiCMS also provides many practical auxiliary variables, such asforloop.CounterCan retrieve the current loop index (starting from 1),forloop.RevcounterThe number of remaining elements.This allows us to easily apply special processing or styling to a specific element in the list.
{% archiveList products with type="list" limit="10" %}
{% for product in products %}
<li {% if forloop.Counter == 1 %}class="new-arrival-item"{% endif %}>
{% if forloop.Counter == 1 %}<span class="badge">新品</span>{% endif %}
<a href="{{ product.Link }}">{{ product.Title }}</a>
</li>
{% endfor %}
{% endarchiveList %}
forThe loop also supportsreversedandsortedModifiers used for traversing the collection in reverse order and by default rules.This provides great convenience when content needs to be displayed in a different order.emptyblocks. Whenforthe loop iterates over thecollectionis emptyemptyThe content within the block will be rendered, which is very useful for displaying prompts such as 'No content available' to users, greatly enhancing the user interface friendliness.
{% archiveList search_results with type="page" q="AnqiCMS" limit="10" %}
{% for result in search_results %}
<div class="search-result-item">
<h3><a href="{{ result.Link }}">{{ result.Title }}</a></h3>
<p>{{ result.Description }}</p>
</div>
{% empty %}
<div class="no-results">抱歉,没有找到您搜索的内容。</div>
{% endfor %}
{% endarchiveList %}
Precise control over content presentation:ifLogical Judgment Tag
ifThe logic judgment tag gives AnqiCMS templates the ability to make dynamic decisions, allowing us to display or hide specific content blocks based on different conditions. Its basic syntax structure is similar to most programming languages and supportsif/elif(else if) andelseCombination, as well as various comparison and logical operators.
A common application scenario is to decide whether to render an image tag based on whether an article has a thumbnail. This not only optimizes the page structure but also avoids visual issues that may be caused by empty image links:
{% archiveList articles with type="list" limit="3" %}
{% for article in articles %}
<div class="article-preview">
{% if article.Thumb %}
<img src="{{ article.Thumb }}" alt="{{ article.Title }}">
{% else %}
<img src="/public/static/images/default_thumb.jpg" alt="默认图片">
{% endif %}
<h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
<p>{{ article.Description }}</p>
</div>
{% endfor %}
{% endarchiveList %}
ifTags can also be used to check if a variable exists, is empty, or is true, etc. For example, display personalized information when the user is logged in, or show unique elements on specific pages:
{# 假设有一个名为 'user' 的变量,在登录时为真值 #}
{% if user %}
<p>欢迎回来,{{ user.UserName }}!</p>
<a href="/user/dashboard">我的面板</a>
{% else %}
<a href="/user/login">登录</a> | <a href="/user/register">注册</a>
{% endif %}
CombineelifandelseWe can build more complex logical judgment chains. For example, according to the type of different pages (home page, category page, detail page) to render different SEO meta information:
{% tdk title_data with name="Title" %}
<title>
{% if title_data.PageType == "index" %}
{{ system.SiteName }} - {{ tdk.Description }}
{% elif title_data.PageType == "category" %}
{{ category.Title }} - {{ system.SiteName }}
{% elif title_data.PageType == "archive" %}
{{ archive.Title }} - {{ category.Title }} - {{ system.SiteName }}
{% else %}
{{ tdk.Title }}
{% endif %}
</title>
ifWithforThe flexible combination and application of
The real place to give full play to the power of AnqiCMS template tags is inifandforThe clever application of label combinations.This combination allows us to execute different logic based on the specific properties or states of each element while iterating over the data, thus creating highly dynamic and customized page layouts.
An example is to build a multi-level category navigation. In AnqiCMS, we cancategoryListobtain category data, and combineitem.HasChildrenProperty to determine if the current category has subcategories. If it does, continue using the nestedcategoryListorforloop to render subcategories; if not, it may directly display the article list under the category:
{% categoryList main_categories with moduleId="1" parentId="0" %}
<ul class="main-nav">
{% for category in main_categories %}
<li class="nav-item">
<a href="{{ category.Link }}">{{ category.Title }}</a>
{% if category.HasChildren %}
<ul class="sub-nav">
{% categoryList sub_categories with parentId=category.Id %}
{% for sub_category in sub_categories %}
<li><a href="{{ sub_category.Link }}">{{ sub_category.Title }}</a></li>
{% endfor %}
{% endcategoryList %}
</ul>
{% else %}
{# 如果没有子分类,可以考虑直接显示该分类下的文章 #}
<ul class="articles-in-category">
{% archiveList articles_in_cat with type="list" categoryId=category.Id limit="3" %}
{% for article in articles_in_cat %}
<li><a href="{{ article.Link }}">{{ article.Title }}</a></li>
{% endfor %}
{% endarchiveList %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>
{% endcategoryList %}
This pattern also performs well when building complex data filtering interfaces. For example, on the product list page, we can usearchiveFiltersRetrieve available filter conditions and generate corresponding links for each filter option. When rendering these filter conditions, useif val.IsCurrentHighlight the currently selected filter option to provide clear visual feedback to the user.
{% archiveFilters filters with moduleId="2" allText="不限" %}
<div class="product-filters">
{% for filter_group in filters %}
<div class="filter-group">
<h4>{{ filter_group.Name }}:</h4>
<ul>
{% for option in filter_group.Items %}
<li {% if option.IsCurrent %}class="active"{% endif %}>
<a href="{{ option.Link }}">{{ option.Label }}</a>
</li>
{% endfor %}
</ul>
</div>
{% endfor %}
</div>
{% endarchiveFilters %}
PassifandforThe organic combination, we are able to achieve high flexibility and customization in AnqiCMS templates.It allows us to dynamically adjust the page layout and content presentation based on the data, providing users with a more accurate and personalized browsing experience.As a website operator, mastering the application of these tags is undoubtedly a key factor in enhancing website competitiveness.
Frequently Asked Questions
Q1: InforHow do I apply special styling to only the first few or the last elements in a loop?A1: In AnqiCMS'sforYou can useforloop.Counterandforloop.Revcounterthese two variables.forloop.CounterCounting starts from 1, indicating the current element in the loop;forloop.RevcounterThis indicates the current element is the nth from the end. For example, to apply a special style to the first element, you can use:{% if forloop.Counter == 1 %} ... {% endif %}If you need to process the last element, you can use{% if forloop.Revcounter == 1 %} ... {% endif %}.
Q2: If my content list may be empty, how can I avoid the page from displaying blank or crashing?A2: When you useforto iterate over a possibly empty set, AnqiCMS providesemptya keyword to elegantly handle this situation. You can add a{% for item in collection %}and{% endfor %}between{% empty %}If the block is empty,.collectionThe content inside the loop will not be rendered, instead, the content of the block will be displayed.forFor example, displaying a prompt message such as 'No content available'.)emptyQ3: In
Q3: InifJudging, how to check multiple conditions at the same time, such as 'If the article has pictures and is a recommended article'?A3: AnqiCMS'sifTags support the use of logical operatorsand/orandnotCombine multiple conditions. For example, you can use{% if article.Thumb and article.Flag contains 'c' %}to check if an article meets both conditions of having a thumbnail and being marked as recommended. Please note,containsCheck if a string or list contains a certain value. For more complex combinations, you can also use parentheses to explicitly specify the order of operations.