How to traverse an array and control display style with loop tags (for) in AnQiCMS templates?

Calendar 81

During website operation, we often need to display various list data, such as article lists, product lists, image galleries, or navigation menus. AnQiCMS (AnQiCMS) provides a powerful mechanism based on the Go language template engine, among whichforLoop tags are the core tool to meet this requirement. By using them flexibly,forTag and its auxiliary function, we can easily traverse array or list data, and accurately control the display style of each element according to actual needs.

Loop tag in AnqiCMS templateforBasic Usage

In the Anqi CMS template,forThe syntax of loop tags is similar to that of most popular template engines, allowing us to iterate over each element in a collection. The most basic usage is:

{% for item in collection %}
    {# 在这里显示每个item的内容 #}
{% endfor %}

HerecollectionThis is the array or list data you want to iterate over, anditemrepresents a temporary variable for the current element in each loop. For example, if you want to display the latest article list, you can use it like thisarchiveListTag to get data, then throughforDisplay through a loop:

<ul class="article-list">
{% archiveList articles with type="list" limit="5" %}
    {% for article in articles %}
    <li class="article-item">
        <a href="{{ article.Link }}" title="{{ article.Title }}">
            <h3>{{ article.Title }}</h3>
            <p>{{ article.Description }}</p>
        </a>
    </li>
    {% endfor %}
{% endarchiveList %}
</ul>

In this example,articlesIt is througharchiveListThe article set obtained by the tag,articleThen it is the variable representing each article in the loop. You can use it toarticle.TitleGet the article title,article.LinkGet the article link, etc., these properties usually correspond directly to the fields of the backend data model.

WhencollectionMay be empty when,forthe tag also provides a{% empty %}Branch used to display alternative content when there are no elements in the collection: It helps improve user experience:

<ul class="article-list">
{% archiveList articles with type="list" limit="5" %}
    {% for article in articles %}
        {# 文章内容展示 #}
    {% empty %}
        <li class="no-content">暂无最新文章。</li>
    {% endfor %}
{% endarchiveList %}
</ul>

Control display style: auxiliary variable during traversal:

InforInside the loop, the Anqi CMS template engine provides some special helper variables that can help us control the display style more finely, or add serial numbers to list items.

  • forloop.Counter: The current loop number, starting from 1.
  • forloop.Revcounter: The reverse loop number, for example, if the list has 5 items, the first item'sRevcounterAre 5, the second item is 4, and so on.

By using these variables, we can easily implement alternating row colors, adding special styles to the first item, and more. For example, setting different background colors for even and odd items in the list:

<ul class="article-list">
{% archiveList articles with type="list" limit="5" %}
    {% for article in articles %}
    <li class="article-item {% if forloop.Counter is divisibleby 2 %}even-row{% else %}odd-row{% endif %}">
        <span>{{ forloop.Counter }}.</span>
        <a href="{{ article.Link }}" title="{{ article.Title }}">
            <h3>{{ article.Title }}</h3>
            <p>{{ article.Description }}</p>
        </a>
    </li>
    {% empty %}
        <li class="no-content">暂无最新文章。</li>
    {% endfor %}
{% endarchiveList %}
</ul>

here,is divisibleby 2is a filter used to judgeforloop.Counterwhether it can be divided evenly by 2 to achieve odd-even line judgment.

Conditional judgment and data filtering:ifThe clever use of tags

ifThe tag is inforLoops play an important role as well, allowing us to decide whether to display an element or apply a specific style based on conditions. CombineditemThe properties of the object can implement very flexible display logic.

For example, you may only want to display articles with thumbnails, or display a placeholder for articles without thumbnails:

`twig

    {% archiveList articles with type=“list” limit=“5” %}

    {% for article in articles %}
    <li class="article-item">
        <a href="{{ article.Link }}" title="{{ article.Title }}">
            {% if article.Thumb %} {# 检查是否存在缩略图 #}
                <img src="{{ article.Thumb }}" alt="{{ article.Title }}" class="article-thumb">
            {% else %}
                <img src="/static/images/placeholder.webp" alt="无图" class="article-thumb-placeholder">
            {% endif %}
            <h3>{{ article.Title }}</h3>
            <p>{{ article.Description }}</p>
        </a>
    </li>
    {% empty %}
        <li class
    

Related articles

How to call data from other sites in a multi-site environment using the siteId parameter?

In the multi-site environment of AnQiCMS, data intercommunication is a key factor in improving operational efficiency and achieving content integration.Sometimes, we may need to display the content of a sub-site on the main site, or call the contact information of another brand site in a business site.At this time, the `siteId` parameter provided by AnQiCMS is particularly important, as it helps us easily implement cross-site data calls and display.### Understanding the multi-site advantages of AnQiCMS AnQiCMS was initially designed with the need for multi-site management in mind

2025-11-08

How to debug variable content and type in AnQiCMS template (using dump filter)?

When using AnQiCMS to build websites and customize templates, we often encounter situations where the content displayed on the page is not as expected, or it seems that a variable is not passing correctly.At this moment, it is particularly important to be able to quickly view the actual content and data type of variables in the template.Fortunately, AnQiCMS's template engine (which adopts a syntax similar to Django) provides us with a very practical tool—the `dump` filter, which helps us easily reveal the mysteries of variables.### Core Function: `dump`

2025-11-08

How to implement pagination display and like function for comment content in AnQiCMS?

In website operation, user comments and interaction are the key to enhancing the vitality of content.AnQiCMS (AnQiCMS) provides powerful content management capabilities, among which the comment feature is an important bridge for user interaction with content.This article will discuss in detail how to implement pagination for comment lists in Anqi CMS, as well as add a like function for comment content, enhancing the interactive experience of your website to a higher level.### CMS Review Function Overview The CMS has built-in article comment and comment management functions from the early version, which provides native user interaction support for the website

2025-11-08

How to enable anti-crawling interference code and image watermark in AnQiCMS to protect the display of content?

In today's era of increasingly rich digital content, protecting original content on websites is particularly important.AnQiCMS (AnQiCMS) understands the pain points of content creators and corporate users, and has specifically built anti-crawling interference codes and image watermarking functions to help users effectively prevent their content from being maliciously scraped and stolen, while also strengthening brand identity.### Protect content security: Enable anti-capture interference code The value of original content is self-evident, but information collection tools on the Internet may copy your articles in large quantities without permission, which not only harms the rights and interests of the original creators but may also divert website traffic

2025-11-08

How to set up a canonical link (CanonicalUrl) in article content and ensure it displays correctly on the page?

In website operation, we often encounter situations where content is similar or repetitive, which may not only scatter the search engine's crawling budget, but also dilute the page weight, affecting SEO performance.Fortunately, AnQi CMS provides us with the function to set canonical links (Canonical URL), which can effectively help us solve these problems and clearly inform search engines which page is the 'original' or 'preferred' version of the content.The value of understanding the significance of canonical URL in simple terms

2025-11-08

How to configure and display structured data (Json-LD) in AnQiCMS to enhance search engine understanding?

In today's digital world, search engines are the main way users discover website content.In order to help search engines better "understand" the content on our website, structured data plays a crucial role.Json-LD (JSON for Linking Data) is a lightweight, easy-to-implement standardized data format that can clearly describe web content in a structured way, thereby enhancing the visibility and display effects of websites in search results.AnQiCMS as a content management system focusing on SEO optimization

2025-11-08

How to set a default thumbnail in AnQiCMS to ensure that articles without uploaded thumbnails can still display images?

It is crucial to maintain the visual appeal and consistency of content in website operation.Many times, the articles we publish may not have a specific picture, or the editor may have missed uploading the thumbnail, which may lead to blank pages or inconsistent styles.AnQiCMS (AnQiCMS) has fully considered this user requirement and provided flexible default thumbnail setting features to ensure that even articles without individually uploaded thumbnails can have beautiful image displays.Why do you need to set a default thumbnail?\n\nA default thumbnail is like a "backup plan" for a website.

2025-11-08

How to automatically convert newline characters in multi-line text in AnQiCMS templates to the HTML `<br/>` tag?

In AnQiCMS template, how to make the newline character of multiline text automatically display as the HTML `<br/>` tag?In website content operation, we often encounter situations where we need to display multi-line text, such as article summaries, product descriptions, or user comments.This text content is usually created by pressing the Enter key when editing in the background.However, when this text containing newline characters (usually `\n`) is directly output to an HTML page, the browser will not parse it as a visual line break.On the contrary, HTML

2025-11-08