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

Calendar 👁️ 75

In website operation, we often need to build complex and hierarchical content structures, such as multi-level classification navigation, product tree structure, or nested display of related articles under categories.AnQiCMS (AnQiCMS) relies on its flexible template engine and rich tag functions, making it simple and efficient to traverse and display these nested structures in templates.

The AnQi CMS template system uses a syntax similar to Django, which means you will mainly{{变量}}and control logic by{% 标签 %}to perform logical control, such as conditional judgments and loop traversals. Deep understanding{% for ... in ... %}Loop tags and the parameters and return structures of various data tags are the key to implementing complex nested displays.

Understanding loops and conditions in templates.

In AnqiCMS templates,{% for item in items %}The structure is the core of traversing list data. Each loop,itemthe variable will point to an element in the list, and you can useitem.属性名Access its internal data in the formitemMay represent a category object, you can accessitem.TitleCategory Title,item.Link(Category link) etc.

At the same time,{% if 条件 %}Tags allow you to conditionally render based on the specific state of the data, which is particularly useful when dealing with nested structures, such as determining if a category has subcategories. If the traversed list may be empty, you can also use{% empty %}Labels to provide alternative display when there is no content.

Traverse and display nested category structure

AnQi CMS providescategoryListLabels to obtain category data. The key to displaying nested categories lies in巧妙地利用parentIdParameters and the category object itselfHasChildrenProperty.

Firstly, you can get the top-level category list:

{% categoryList categories with moduleId="1" parentId="0" %}
    {# 一级分类列表 #}
    <ul>
        {% for category in categories %}
            <li>
                <a href="{{ category.Link }}">{{ category.Title }}</a>
                {# 判断当前分类是否有子分类 #}
                {% if category.HasChildren %}
                    {# 如果有子分类,则再次调用 categoryList 标签,并将其 parentId 设置为当前分类的ID #}
                    <ul>
                        {% categoryList subCategories with parentId=category.Id %}
                            {% for subCategory in subCategories %}
                                <li><a href="{{ subCategory.Link }}">{{ subCategory.Title }}</a></li>
                            {% endfor %}
                        {% endcategoryList %}
                    </ul>
                {% endif %}
            </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

This code demonstrates how to recursively traverse two-level categories. For deeper nesting, you can follow the same pattern, insubCategoriesthe loop check again.subCategory.HasChildrencallcategoryList.

Display related content in nested categories

The website structure often needs to associate categories with specific content (such as articles, products). In AnqiCMS, you can associatecategoryListwith the tag andarchiveListUsing tags together, it displays the category while also showing some of the content under that category.

{% categoryList categories with moduleId="1" parentId="0" %}
    {# 遍历一级分类 #}
    {% for category in categories %}
        <div class="category-block">
            <h3><a href="{{ category.Link }}">{{ category.Title }}</a></h3>
            <ul>
                {# 在每个分类下,获取其关联的文章列表 #}
                {% archiveList articles with type="list" categoryId=category.Id limit="5" %}
                    {% for article in articles %}
                        <li>
                            <a href="{{ article.Link }}">{{ article.Title }}</a>
                            <span>{{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
                        </li>
                    {% empty %}
                        <li>该分类暂无文章。</li>
                    {% endfor %}
                {% endarchiveList %}
            </ul>
        </div>
    {% endfor %}
{% endcategoryList %}

here,archiveListTag throughcategoryId=category.IdThe parameter accurately retrieves the articles under the current looped category. You can adjust it as needed.limitParameters to control the number of articles displayed.

Use navigation lists to implement multi-level menus.

In addition to manually building category nesting, Anqi CMS'snavListThe tag is an ideal choice for building multi-level navigation menus. The tag directly returns a well-processed nested data structure, greatly simplifying the development of multi-level menus.

A multi-level navigation configured in the background can be called directly in the template:

{% navList mainNavs %}
    <ul class="main-menu">
        {% for nav in mainNavs %}
            <li {% if nav.IsCurrent %}class="active"{% endif %}>
                <a href="{{ nav.Link }}">{{ nav.Title }}</a>
                {# 判断当前导航项是否有子导航 #}
                {% if nav.NavList %}
                    <ul class="sub-menu">
                        {% for subNav in nav.NavList %}
                            <li {% if subNav.IsCurrent %}class="active"{% endif %}>
                                <a href="{{ subNav.Link }}">{{ subNav.Title }}</a>
                                {# 还可以继续检查 subNav.NavList 实现更深层次的嵌套 #}
                            </li>
                        {% endfor %}
                    </ul>
                {% endif %}
            </li>
        {% endfor %}
    </ul>
{% endnavList %}

navListreturnednavThe object hasNavListproperties that can be directly used to iterate through its child navigation items, making the construction of multi-level menus very intuitive.

Further, you can even navigate under these items, based on their associatedPageId(If the navigation item links to a category or a single page), call it againarchiveListorcategoryListto display related content:

{% navList mainNavs %}
    <ul class="main-menu">
        {% for nav in mainNavs %}
            <li>
                <a href="{{ nav.Link }}">{{ nav.Title }}</a>
                {% if nav.NavList %}
                    <ul class="sub-menu">
                        {% for subNav in nav.NavList %}
                            <li>
                                <a href="{{ subNav.Link }}">{{ subNav.Title }}</a>
                                {# 如果子导航关联的是一个分类,显示该分类下的文章 #}
                                {% if subNav.PageId > 0 %}
                                    {% archiveList articles with type="list" categoryId=subNav.PageId limit="3" %}
                                        {% if articles %}
                                            <div class="articles-in-menu">
                                                {% for article in articles %}
                                                    <p><a href="{{ article.Link }}">{{ article.Title }}</a></p>
                                                {% endfor %}
                                            </div>
                                        {% endif %}
                                    {% endarchiveList %}
                                {% endif %}
                            </li>
                        {% endfor %}
                    </ul>
                {% endif %}
            </li>
        {% endfor %}
    </ul>
{% endnavList %}

This example combines navigation, classification, and content in a clever way, creating a highly dynamic and rich information display.

Nested application of model custom fields.

In AnQi CMS, both content models and categories support custom fields.These custom fields can also include more complex, nested data structures, such as image galleries (Images).You can display these fields by iterating through them.

If the document detail page has a namedImagescustom image group field:

{% archiveDetail archiveImages with name="Images" %}
    <div class="product-gallery">
        {% for img in archiveImages %}
            <img src="{{ img }}" alt="产品图片">
        {% endfor %}
    </div>
{% endarchiveDetail %}

Similarly, if you define categories or document models in the background,ExtraParameters (for example, additional information containing multiple key-value pairs) can bearchiveParamsorcategoryDetailtraversed after retrieval:

{% archiveParams params %}
    <div class="extra-info">
        {% for item in params %}
            <p><strong>{{ item.Name }}:</strong> {{ item.Value }}</p>
        {% endfor %}
    </div>
{% endarchiveParams %}

This allows you to flexibly display custom data, whether it's simple text or complex image collections.

Summary

The Anqi CMS template engine provides powerful flexibility to handle various nested structures. By mastering{% for %}/{% if %}Label, as wellcategoryList/archiveListand `navList

Related articles

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 define fields in the content model to achieve personalized display of different types of content?

In AnQi CMS, the content model is the core of website content management, which provides a structured way to organize and display different types of information.Flexibly utilizing the custom field function in the content model can greatly enhance the personalized display capabilities of website content, meeting various unique business needs. ### Core: The flexibility of content models and custom fields AnQi CMS allows us to create or modify content models according to the actual operational needs of the website.This is not just a simple article and product classification, but also the key to customizing data structures for each content type. For example

2025-11-08

How to use the AnQiCMS backend settings to centrally manage and display the default thumbnail of the website?

In website operation, images play a vital role, especially when it comes to content lists or sharing, a suitable thumbnail can greatly enhance click-through rates and user experience.However, manually uploading thumbnails for each piece of content is time-consuming and prone to omissions, leading to broken images or a lack of visual consistency on the website page.AnQiCMS is well-versed in this field, providing powerful backend settings features, allowing you to easily achieve unified management and intelligent display of default thumbnails on your website. We will explore the convenient settings of the AnQiCMS backend step by step.

2025-11-08

When the custom URL does not work, how should I investigate the content display problem?

In website operation, a clear and easy-to-understand URL structure is crucial for search engine optimization (SEO) and user experience.AnQiCMS (AnQiCMS) offers powerful custom URL functionality, allowing you to flexibly configure links based on content models, categories, articles, etc.However, you may sometimes find that the custom URL set up in the background does not display as expected on the frontend page, or that clicking on it results in a 404 error.When encountering this situation, we can follow the following steps to troubleshoot, locate, and solve the problem step by step.

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 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 accurately control the count and iteration order of list loops in the template?

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 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 the user experience and the overall performance of the website.The template engine of Anqi CMS has borrowed the syntax of Django, providing us with powerful list loop control capabilities.

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