How to accurately control the count and iteration order of list loops in the template?

Calendar 👁️ 61

In AnQi CMS, the content list is a core component of our website, whether it is an article list, product display, or other data modules, it cannot do without looping through the data in the template and displaying it.Efficiently and accurately control the looping behavior of these lists, which not only makes the content presentation more flexible and diverse, but also significantly improves user experience and the overall performance of the website.The AnQi CMS template engine borrows the syntax of Django, providing us with powerful list loop control capabilities.

Overview of basic loops and iterations

First, let's start with the most basic loop tag{% for %}Start. In the AnqiCMS template, when we want to display a collection of content (such as a series of articles or products), we will usually combine sucharchiveList/categoryListorpageListsuch data tags to retrieve data, then useforloop tags to iterate over these data.

A typical example is to retrieve and display a list of articles:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
        <div class="article-item">
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <p>{{item.Description}}</p>
        </div>
    {% endfor %}
{% endarchiveList %}

Here, archivesIsarchiveListthe article set returned by the tag,itemIt is the variable of each article in the loop. Understanding this basic structure is the key to precise control later.

Precisely master the loop count and position

In many scenarios, we are not just simply traversing data, but also need to know which item the current loop is on, or how many items are left until the end of the loop. Anqi CMS provides for thisforloop.Counterandforloop.RevcounterThese are very useful variables:

  • forloop.Counter: This variable will return the current loop count, from1Start incrementing. It is very suitable for displaying numbers or determining whether it is the first or second item in the list.
  • forloop.Revcounter: This variable returns how many items are left until the end of the loop, the same from1Start decreasing. It becomes particularly convenient when you need to handle the last item or the last few items in a list.

Suppose we want to add numbering to the article list and identify the second-to-last article:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
        <div class="article-item {% if forloop.Revcounter == 2 %}last-second-item{% endif %}">
            <span class="index">{{forloop.Counter}}.</span>
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <p>{{item.Description}}</p>
        </div>
    {% endfor %}
{% endarchiveList %}

This code not only clearly displays the article number, but also through judgment:forloop.RevcounterThe value, added a special CSS class to the second last article for easy styling customization.

Flexibly adjust the iteration order:reversedandsorted

Sometimes, we want to traverse the data we have retrieved in a specific order. Anqi CMS'sforLoop tags supportreversedandsortedKeywords, to change the order of iteration:

  • reversed: If you want to traverse the data in reverse order, you canforadd areversed. For example, if the list is originally sorted from new to old by release time, addingreversedwill display it from old to new.
  • sorted: This keyword allows you to sort the elements in the list. It is usually more effective for lists containing sortable values (such as numbers, strings). It should be noted that for througharchiveListLabel to get the content data, we are more throughorderParameters are sorted during the data acquisition phasesortedMore suitable for general data arrays

For example, if we have a simple list of numbersmyNumbers = [5, 2, 8, 1]We can traverse like this:

{% set myNumbers = "[5,2,8,1]"|list %}
<div>
    <p>原始顺序:</p>
    {% for num in myNumbers %}
        <span>{{num}} </span>
    {% endfor %}
</div>
<div>
    <p>反转顺序:</p>
    {% for num in myNumbers reversed %}
        <span>{{num}} </span>
    {% endfor %}
</div>
<div>
    <p>排序后:</p>
    {% for num in myNumbers sorted %}
        <span>{{num}} </span>
    {% endfor %}
</div>

This will output separately:5 2 8 1/1 8 2 5and1 2 5 8.

Conditional display in the loop:ifThe clever use of tags

Nested in the loop:ifLogical judgment tag, which can achieve more detailed display control. It is very useful for highlighting specific content or changing the layout of content based on certain conditions.

Except for utilizingforloop.Counterandforloop.RevcounterTo judge items other than the first and last, we can also judge the attributes of the data items according to business requirements:

{% archiveList articles with type="list" limit="8" flag="c" %} {# 获取推荐文章 #}
    {% for article in articles %}
        {% if forloop.Counter == 1 %}
            <div class="featured-article">
                <h2><a href="{{article.Link}}">{{article.Title}}</a></h2>
                <p>这是特别推荐的第一篇文章,不容错过!</p>
            </div>
        {% elif article.Views > 1000 %}
            <div class="popular-article">
                <h4><a href="{{article.Link}}">{{article.Title}}</a></h4>
                <span>阅读量已破千!</span>
            </div>
        {% else %}
            <div class="normal-article">
                <h5><a href="{{article.Link}}">{{article.Title}}</a></h5>
            </div>
        {% endif %}
    {% endfor %}
{% endarchiveList %}

This code sets a unique style for the first article, articles with more than a thousand reads also have a separate style, and other articles are displayed in a normal style, greatly enriching the visual hierarchy of the content.

Gracefully handle empty lists:emptyTag

Sometimes, througharchiveListThe result may be empty for other labels. If not handled properly, it may cause blank or unfriendly prompts on the page.{% empty %}Labels can help us elegantly handle this situation:

{% archiveList searchResults with type="page" q=urlParams.q limit="10" %}
    {% for item in searchResults %}
        <div class="search-result-item">
            <h4><a href="{{item.Link}}">{{item.Title}}</a></h4>
            <p>{{item.Description}}</p>
        </div>
    {% empty %}
        <div class="no-results">
            <p>抱歉,没有找到您搜索的 "{{urlParams.q}}" 相关内容。</p>
            <p>您可以尝试更换关键词或浏览其他分类。</p>
        </div>
    {% endfor %}
{% endarchiveList %}

WhensearchResultsWhen the list is empty,{% empty %}the content in the block will be rendered, providing clear feedback to the user.

alternating mode in the loop:cycleTag

If you need to apply a repeated style or text pattern to each item in the list, for example, to make the background color of adjacent list items different,cycleTags are a very practical tool:

{% archiveList products with type="list" limit="6" %}
    {% for product in products %}
        <div class="product-card {% cycle 'odd-bg' 'even-bg' %}">
            <img src="{{product.Thumb}}" alt="{{product.Title}}">
            <h5><a href="{{product.Link}}">{{product.Title}}</a></h5>
        </div>
    {% endfor %}
{% endarchiveList %}

Here, {% cycle 'odd-bg' 'even-bg' %}This will output alternately in each loop'odd-bg'and'even-bg'These strings can be used as CSS class names to easily implement a zebra line effect list style.

Optimize data source: list tag parameters

Although the aforementioned method can accurately control the iteration of the loopmethodBut the real 'accurate control' often starts fromdata sourceStart. The list label of Anqi CMS (such as `

Related articles

How to quickly fill content through content collection and batch import functions, and ensure its normal display?

## Quickly fill in website content, Anqi CMS helps you display efficiently At the beginning of website operation or when a large amount of content needs to be updated, manually publishing content one by one is undoubtedly a time-consuming and labor-intensive task.The content collection and batch import function has emerged, aimed at helping us solve this pain point and making the filling of website content more efficient than ever before.AnQiCMS (AnQiCMS) is a powerful content management system that provides a mature and easy-to-use solution in this aspect, not only helps us quickly obtain content, but also ensures that these contents are presented normally and beautifully on the website.###

2025-11-08

How to display and manage exclusive content for different user groups or VIP users in AnQiCMS?

Today in the era of digital content operation, providing exclusive content for specific user groups has become a common and efficient strategy, whether it is for content monetization, enhancing user loyalty, or creating a unique community experience.AnQiCMS as a flexible enterprise-level content management system provides strong and easy-to-operate support in this regard.One of the core strengths of AnQiCMS is its comprehensive user group management and VIP system.

2025-11-08

How to traverse and display nested categories or content structures in a template?

In website operation, we often need to build complex and layered content structures, such as multi-level classification navigation, product tree structures, or nested display of related articles under categories.AnQiCMS (AnQiCMS) with its flexible template engine and rich tag features makes it simple and efficient to traverse and display these nested structures in templates.The AnqiCMS template system uses syntax similar to Django, which means you will mainly output data through `{{variable}}` and execute logical control through `{% tags %}`

2025-11-08

How can Anqi CMS protect original content display on the front end through anti-crawling interference codes and watermark functions?

## Protect Originality: How Anqi CMS Protects Your Front-end Content Through Anti-Crawling Interference Codes and Watermark Features In today's explosive growth of digital content, the value of original content is self-evident, but the problem of content theft and plagiarism that follows also causes countless creators a headache.When you put a lot of effort into writing articles or designing beautiful pictures, and they are picked up and published by others without any effort, the feeling of frustration is indescribable.AnQi CMS knows that originality is not easy, so it specially built strong anti-collection interference codes and picture watermarking functions, aiming to display on the front-end layer

2025-11-08

How to implement content parameter filtering and dynamically adjust the list display results in AnQiCMS?

In a content management system, allowing users to quickly find the information they need according to their own needs is the key to enhancing the website experience and content value.AnQiCMS provides a powerful custom content model and flexible template tags, which helps us easily implement content parameter filtering, dynamically adjust the display results of the list, and greatly enhance the interaction and user-friendliness of the website content.This article will discuss in detail how to use these features in AnQiCMS, from backend configuration to frontend display, and step by step to implement content parameter filtering.

2025-11-08

How to troubleshoot when the AnQiCMS program is upgraded and the front-end page does not update?

How to troubleshoot when the front page does not update after the AnQiCMS program upgrade? When you eagerly upgraded the AnQiCMS program, expecting new features and optimizations to immediately appear on your website, only to find that the front page remained unchanged and stuck in the old version, that feeling is indeed quite crazy.Don't worry, this is usually not a big problem, mostly due to caching or program loading mechanisms.Today we will work together to investigate in detail and see how to make your website's front-end look brand new.

2025-11-08

How to display content from a specific site by using label parameters in a multi-site environment?

When using AnQiCMS for website management, the multi-site feature undoubtedly brings great convenience to users with multiple brands, sub-sites, or content branches.It allows us to efficiently manage multiple independent websites on a unified backend.However, in actual operation, we sometimes encounter a specific requirement: to display the content of another specific site on a site.For example, the main site wants to reference some of the latest articles from the child site, or a specific thematic site wants to display product information from another cooperative site.At this moment, 'How to set up an environment across multiple sites'

2025-11-08

How can AnQi CMS display a list of documents under a specified category and perform pagination?

AnQiCMS (AnQiCMS) with its flexible template mechanism and powerful content management features makes the display of website content very simple and efficient.For many websites, clearly displaying content by category and providing convenient pagination navigation is the key to improving user experience and optimizing search engine rankings.Let's learn together how to easily implement document list display and pagination functions under specified categories in Anqi CMS.### Flexible configuration, control content display Anqi CMS template system uses syntax similar to Django template engine

2025-11-08