How to use the (for) loop tag in AnQiCMS template to traverse and display the content list?

Calendar 👁️ 76

Use efficiently in AnQiCMS templateforDisplaying content list with loop tags, which is an indispensable core skill for building dynamic websites. By using it flexiblyforLoop, we can easily traverse various types of data collections and display them on the website page in a customized style, whether it is a news list, product display, category navigation, or any other repetitive content block, ...forEvery loop can help you a hand.

Next, we will delve into how to use it in AnQiCMS templates.forLoop tags, from basic usage to advanced techniques, to help you better organize and display website content.

AnQiCMS template basic review

AnQiCMS's template system draws on the syntax of the Django template engine, which is simple and efficient as a whole. In the template file, we mainly encounter two types of tags:

  • Variable output:Use double curly braces{{ 变量名称 }}to display data content. For example,{{ item.Title }}It will output the title of the list item.
  • Tag control:Use single curly braces and percent signs{% 标签名称 参数 %}To execute logical control, such as conditional judgment, loop traversal, etc. All control tags need a corresponding end tag, such as{% if ... %}corresponding{% endif %},{% for ... %}corresponding{% endfor %}.

Understanding these basic syntax is to useforthe premise of the loop.

forThe core function of the loop tag

forThe loop tag is used to iterate over an iterable object (such as an array, slice, or list), and in each iteration, the current element is assigned to a specified variable so that its properties can be accessed and displayed within the loop body.This allows us to use a template code to dynamically render multiple contents.

Its basic structure is usually like this:

{% for item in collection %}
    {# 在这里,你可以使用 item 变量来访问当前遍历到的数据项的属性 #}
    {{ item.Property1 }}
    {{ item.Property2 }}
{% endfor %}

Here, collectionIt is a list that contains multiple data items,itemwhich is a temporary variable representing the current data item in each loop.

forThe basic usage and examples of loops

In AnQiCMS, various content list tags (such asarchiveListDocument list,categoryListCategory list,pageListSingle page list, etc.) will return a可供forThe collection of data to be traversed. Let's take a common requirement as an example: displaying the list of the latest released documents.

First, we need to usearchiveListTag to get the document collection. Suppose we want to get the latest 5 documents under the category with ID 1:

{% archiveList documents with type="list" categoryId="1" limit="5" %}
    {% for doc in documents %}
        <div>
            <h3><a href="{{ doc.Link }}">{{ doc.Title }}</a></h3>
            <p>{{ doc.Description }}</p>
            <p>发布时间: {{ stampToDate(doc.CreatedTime, "2006-01-02") }}</p>
            <img src="{{ doc.Thumb }}" alt="{{ doc.Title }}">
        </div>
    {% endfor %}
{% endarchiveList %}

In this example:

  • {% archiveList documents with ... %}Got a nameddocumentsdocument collection.
  • {% for doc in documents %}Start traversing this collection, each iteration assigns the current document todocVariable.
  • In the loop body, we use{{ doc.Link }}/{{ doc.Title }}/{{ doc.Description }}/{{ doc.Thumb }}Access the link, title, description, and thumbnail of the current document.
  • {{ stampToDate(doc.CreatedTime, "2006-01-02") }}demonstrates how to usestampToDateThe auxiliary label formats the document's creation timestamp into a readable date.

EnhanceforPractical tips for loops

forLoops are not limited to simple iteration, they also provide some very useful auxiliary functions and advanced usage, making your content display more flexible.

1. Handling an empty list (emptyblock statement)

When the data set may be empty,forrepeatedlyemptythe statement block can provide friendly prompt information rather than displaying a blank area. This is crucial for enhancing the user experience.

{% archiveList documents with type="list" categoryId="999" limit="5" %}
    {% for doc in documents %}
        <div>
            <h3><a href="{{ doc.Link }}">{{ doc.Title }}</a></h3>
            <p>...</p>
        </div>
    {% empty %}
        <p>抱歉,当前分类下暂无文档。</p>
    {% endfor %}
{% endarchiveList %}

IfdocumentsThe collection is empty, the page will display the prompt

2. Get the loop number and reverse number (forloopvariable)

In the loop body, the system will automatically provide a specialforloopvariable that contains some useful information about the current loop:

  • forloop.Counter: The serial number of the current iteration starting from 1.
  • forloop.Revcounter: The reverse order index starting from the total number of the collection currently iterated over.

This is needed when adding numbers to list items, or applying different styles to even/odd rows.

Related articles

How to use conditional judgment (if/else) tags to control the display logic of content in AnQiCMS templates?

In the development and maintenance of website templates, we often encounter the need to display or hide different content based on specific conditions.This dynamic content display logic is crucial for improving user experience, realizing personalized functions, and optimizing the website structure.AnQiCMS (AnQiCMS) is a powerful content management system that provides a flexible template engine, among which the condition judgment (`if/else`) tags are the core tools to achieve this goal.

2025-11-07

How to implement pagination for document lists in AnQiCMS and support custom page number quantities?

AnQiCMS document list pagination and custom page number settings: Make your content management more efficient Imagine, when your website content becomes rich, with thousands of articles, products, or news lists neatly arranged, if you cannot organize and display them effectively, users may feel at a loss.A good pagination mechanism not only improves user experience and helps visitors quickly find the information they need, but is also an important aspect of SEO optimization.

2025-11-07

How to call and display friend links in AnQiCMS templates?

Friendship links, as part of the website content ecosystem, not only help to improve search engine rankings and optimize website weight, but also provide users with more navigation to related resources.In AnQiCMS, managing and displaying friend links is a direct and flexible task, even if you do not have a deep programming background, you can easily achieve it. ### Backend Management: Easily Configure Friend Links In the AnQiCMS backend management interface, you will find the friend link settings under the "Function Management" menu.

2025-11-07

How to implement pagination and like function in AnQiCMS comment list?

The Anqi CMS plays a crucial role in content operation, where user interaction is an important factor in enhancing website activity. The comment function, along with its配套 pagination display and like function, is undoubtedly the key to achieving this goal.Benefiting from AnQiCMS's flexible template engine and rich built-in tags, we can relatively easily add these features to the articles or products of the website. ### 1.The basic enablement and configuration of the comment function Firstly, ensure that the website's comment function is enabled.

2025-11-07

How to correctly format timestamps in AnQiCMS templates so that content is displayed as needed?

In the daily operation of website content, time information plays a vital role.No matter the publication date of the article, the launch time of the product, or the latest update of the content, these time points directly affect users' perception and trust in the information.However, in the templates of content management systems, we often encounter dates stored in the form of "timestamps", which are precise but not intuitive.How should we correctly format these timestamps in the AnQiCMS template so that the content can be displayed in the way we expect?

2025-11-07

How to define and use variables in AnQiCMS templates to optimize the display and management of content?

In AnQi CMS, flexibly using template variables is the key to optimizing website content display and management efficiency.It not only makes your website content more dynamic, but also greatly improves the maintainability and reusability of the template.This article will delve into how to define and use variables in AnQiCMS templates, as well as how to better present content through these techniques. ### Variables: The Foundation of Living Content Imagine how cumbersome it would be if every title, every paragraph on a website had to be manually modified.The introduction of variables is to solve this pain point

2025-11-07

How to reuse common code snippets through the `include` tag in AnQiCMS templates to optimize the display structure?

When building and managing websites, improving efficiency and maintaining code neatness is the goal pursued by every operator.AnQiCMS (AnQiCMS) as an efficient content management system, provides powerful template functions, among which the `include` tag is the tool to achieve this goal.It allows us to abstract out repeated code snippets from the website, forming independent modules, and then referencing them where needed, greatly optimizing the template structure and subsequent maintenance work.

2025-11-07

How to implement page layout inheritance and rewriting using the `extends` tag in AnQiCMS templates?

When building a website, maintaining the uniformity of the page style, improving development efficiency, and reducing maintenance costs is the goal pursued by every website operator.AnQiCMS (AnQiCMS) deeply understands this, and its template system design draws on the excellent ideas of the Django template engine, among which the `extends` tag is one of the core functions to achieve this goal. ### The Core Concept of Template Inheritance Imagine you are designing a series of posters.Each poster has a common brand logo, top title, and bottom contact information, with only the promotional content in the middle being different

2025-11-07