How to implement diverse data layout and display with the `for` loop tag in AnQi CMS?

Calendar 👁️ 65

How to present information in a richer and more attractive way during website content operation is the key to improving user experience and site activity.AnQiCMS (AnQiCMS) provides us with the tools we need to achieve this goal with its flexible content model and powerful template engine, among which,forThe looping tag is the essential core tool for us to perform diverse typesetting and display.

Imagine, your website is not just a static pile of articles, but can intelligently present article lists, product albums, categorized navigation, user comments, and even custom parameters based on different scenarios.forLoop tags in Anqi CMS are the foundation for implementing these dynamic displays.

Master data flexibility, starting with the basic list.

The Anqi CMS template engine uses syntax similar to Django, which makes it very intuitive for content developers to manipulate data. When we talk aboutforLoops, the most direct application is to traverse a set of data and display it one by one.

For example, to display the latest list of articles on a page, we can usearchiveListTag to retrieve data set and then cooperateforLoop to present the title, link, introduction, and thumbnail information of each article one by one.

{% archiveList articles with type="list" limit="5" %}
    <div class="latest-articles">
        {% for article in articles %}
            <div class="article-item">
                {% if article.Thumb %}<img src="{{ article.Thumb }}" alt="{{ article.Title }}" class="article-thumb">{% endif %}
                <h3 class="article-title"><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
                <p class="article-description">{{ article.Description }}</p>
                <span class="article-date">发布时间:{{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
            </div>
        {% endfor %}
    </div>
{% endarchiveList %}

In this code block,articlesIs a collection containing multiple articles.forLoop through each one of themarticleObject, we can thenarticle.Title/article.LinkBy using these properties to access and format the detailed data of each article, this method makes our content display effortless. Whether it's a simple text list or a card layout with pictures, it can be easily realized.

Build a hierarchical structure, create intelligent navigation and content aggregation

The website's navigation and classification is an important way for users to find content. Anqi CMS'scategoryListLabel combinationforLoops can help us build multi-level category navigation, and even directly display related content within categories.

Assuming we need to display product categories, and there may be subcategories under each category, we can handle it flexibly like this:

{% categoryList mainCategories with moduleId="2" parentId="0" %}
    <ul class="main-category-nav">
    {% for category in mainCategories %}
        <li>
            <a href="{{ category.Link }}">{{ category.Title }}</a>
            {% if category.HasChildren %}
                {# 如果有子分类,则再次循环展示 #}
                {% categoryList subCategories with parentId=category.Id %}
                    <ul class="sub-category-nav">
                    {% for subCategory in subCategories %}
                        <li><a href="{{ subCategory.Link }}">{{ subCategory.Title }}</a></li>
                    {% endfor %}
                    </ul>
                {% endcategoryList %}
            {% endif %}
        </li>
    {% endfor %}
    </ul>
{% endcategoryList %}

Through this nestedforLoop structure, we can not only clearly show the classification level of the website, but also, when necessary, according toitem.HasChildrenThis conditional judgment determines whether to further load and display subcategories, or directly display part of the documents under the category next to it, thereby building a more intelligent and interactive content aggregation area.

Details determine experience: personalized processing in the loop

forThe power of loops goes beyond this, as they also have many built-in variables and functions to help us control each element in the loop more finely, thus achieving diverse layout and display effects.

  • forloop.Counterandforloop.RevcounterThese two variables can be used to obtain the current loop index (starting from 1) and the number of remaining elements.We can use them to add special styles to the first element of the list or implement alternate row coloring effects, etc.

    {% archiveList articles with type="list" limit="5" %}
        <div class="article-list-with-numbers">
        {% for article in articles %}
            <div class="article-item {% if forloop.Counter == 1 %}is-highlighted{% endif %}">
                <span class="item-number">{{ forloop.Counter }}.</span>
                <a href="{{ article.Link }}">{{ article.Title }}</a>
            </div>
        {% endfor %}
        </div>
    {% endarchiveList %}
    
  • emptyTagWhen the data set is empty, we may not want the page to display a blank space, but to show a friendly prompt message instead.emptyThe label can elegantly handle this situation.

    {% archiveList emptyArticles with type="list" categoryId="999" %} {# 假设此分类下无文章 #}
        {% for article in emptyArticles %}
            ...
        {% empty %}
            <p class="no-content-tip">很抱歉,当前分类暂无文章内容。</p>
        {% endfor %}
    {% endarchiveList %}
    
  • ifConditional judgmentIn the loop, we often need to decide how to display based on certain properties of the data.For example, images are displayed only when the article has a thumbnail, or different buttons are displayed according to the product status.“`twig {% archiveList products with type=“list” limit=“4” %}

    <div class="product-cards">
    {% for product in products %}
        <div class="product-card">
            {%
    

Related articles

What are some practical scenarios for using the `if/else` logical judgment tags in AnQi CMS to control content display conditions?

In AnQi CMS template design, the `if/else` logical judgment tag is an extremely powerful tool that endows the website content with the vitality of dynamic display.By flexibly using this tag, we can intelligently control the display and hiding of elements on the page based on different conditions, thereby providing users with more accurate and personalized browsing experiences, and also greatly improving the operational efficiency of the website.Imagine if your website needs to display different content for different scenarios or if certain information is only visible under specific conditions

2025-11-09

How to use the `pagination` tag to generate a page navigation that conforms to user habits for articles or product lists?

In website operation, how to allow users to easily browse a large amount of content and find the information they are interested in is a crucial topic.Especially on pages such as article lists and product displays, reasonable pagination navigation not only improves user experience but is also an indispensable part of search engine optimization (SEO).AnQiCMS (AnQiCMS) understands this point and therefore provides a powerful and flexible `pagination` tag to help us easily generate professional and user-friendly pagination navigation for website content.We will delve deeper into how to utilize Anqi CMS

2025-11-09

How to display the friend link list in the website background configuration of Anqi CMS?

In website operation, friendship links are an important link to enhance website weight, increase traffic, and improve user experience.AnQiCMS (AnQiCMS) provides us with a convenient way to manage and display these links.How can the list of friend links configured on the back-end be displayed on the website front-end?This is actually simpler than imagined, mainly involving two steps: backend configuration and frontend template invocation. ### Step 1: Configure the友情链接 in the background Firstly, we need to add and manage the friendship links in the Anq CMS background management system.According to system design

2025-11-09

How to use the `guestbook` tag to build and display a dynamic guestbook form on the front page?

In AnQiCMS, building a functional message form is an important way to interact effectively with users, collect feedback, or obtain potential customer information.It is pleasing that with its powerful template tag system, especially the `guestbook` tag, we can easily create and display a dynamic and flexible guestbook form on the front page without writing complex backend code.The core of flexibly building a message form

2025-11-09

How to format a timestamp into a readable date and time using the `stampToDate` tag in AnQi CMS?

In website operation, the display of time information is crucial. Whether it is the publication date of articles, the update time of products, or the submission time of comments, a clear and easy-to-read date and time format can greatly enhance the user experience.The original timestamp is machine-friendly, but it is cryptic to ordinary visitors.Fortunately, AnQiCMS (AnQiCMS) provides a very convenient template tag——`stampToDate`, which can easily format timestamps into the date and time we are familiar with.### `stampToDate` label

2025-11-09

How to use the `system` tag to retrieve and display the global information of a website, such as the website name, Logo, and filing number?

It is crucial to maintain the consistency of brand image and the accuracy of information in website operation.AnQi CMS is an efficient content management system that provides us with a convenient way to manage and display these core information.Today, let's delve into how to use the `system` tag of Anqi CMS to easily obtain and display global information on the website template, such as the website name, Logo, and filing number.

2025-11-09

How to display the contact information set in the website backend on the front page of AnQi CMS?

When using Anq CMS to build a website, displaying the contact information set in the background to the website front end is a key step to enhance user experience and establish trust.AnQi CMS provides a very intuitive and flexible way, even without a strong technical background, it can be easily realized. ### Configure the contact information of the website backend Firstly, you need to configure the contact information in the Anqi CMS backend management interface. Generally, you can find the 'Background Settings' in the left navigation bar, clicking on it will show the 'Contact Information Settings' option.

2025-11-09

How to dynamically set the `title`, `keywords`, and `description` metadata of a page using the `tdk` tag?

In the practice of website operation and search engine optimization (SEO), page metadata plays a crucial role.Among them, the page title (`title`), keywords (`keywords`), and description (`description`) directly affect the display effect and the willingness of users to click on the search engine results page.AnQiCMS (AnQiCMS) fully understands its importance and provides a powerful and flexible `tdk` tag to help us dynamically and efficiently manage these key metadata.

2025-11-09