How to implement hierarchical display and nested calling of the `categoryList` tag?

Calendar 👁️ 72

Build a clear and organized website navigation in AnQiCMS, especially when dealing with multi-level categories, is the key to improving user experience and website accessibility.categoryListThe tag is exactly for this, it provides powerful functions to help us flexibly display the classification hierarchy and make fine-grained nested calls.

UnderstandingcategoryListThe foundation of the tag

categoryListThe tag is a core component of the AnQiCMS template engine, used to obtain the list of categories of articles or products from the background.Its basic usage is simple and clear, but by matching different parameters, it can achieve extremely rich display effects.

Typical usage is shown as follows:

{% categoryList categories with moduleId="1" parentId="0" %}
    {# 在这里循环输出分类信息 #}
{% endcategoryList %}

There are several key parameters:

  • moduleId: This parameter is very important, it specifies which content model (such as article model or product model) under the category you want to retrieve.AnQiCMS supports custom content models, each model has its independent classification system.
  • parentIdThis is the core parameter controlling the display of category levels.
    • When set toparentId="0"At this time, it will retrieve the specifiedmoduleIdAll top-level categories under it.
    • When you are in a loop and want to get the subcategories of the current category, you can dynamicallyparentIdis set toitem.Idof whichitemis the category object in the current loop.
  • limit: Used to limit the number of displayed categories, such aslimit="10"It will only display 10 categories.
  • siteIdIn multi-site management mode, if you need to call data from other sites, you can specify the site ID through this parameter.

In{% for item in categories %}In the loop, we can access the properties of each category, for example:

  • item.Id: Category ID
  • item.Title: Category name
  • item.Link: Category link
  • item.ParentId: Parent category ID
  • item.HasChildren: A boolean value indicating whether the category has subcategories, which is very useful when performing nested judgments.
  • item.Spacer: A prefix used to visually create hierarchical indentation, which is convenient for displaying tree structures.

Implement hierarchical classification with level display and nested calling

The charm of multi-level classification lies in its ability to present complex website structures to users in a clear manner. In AnQiCMS, it is cleverly nested incategoryListtagscategoryListLabel, we can easily build a classification hierarchy of any depth.

The core idea is:Use an outer layercategoryListLabel to get the top-level classification, then nest it within the loop.categoryListLabel and set the inner label'sparentIdparameters to the outer category'sId.So, the inner label will automatically get the subcategories of the outer category.

Let's see an example of implementing a three-level nested call

{# 外层标签:获取所有顶级分类,moduleId="1"代表文章模型 #}
{% categoryList topCategories with moduleId="1" parentId="0" %}
<ul>
    {% for item in topCategories %}
    <li>
        {# 显示一级分类 #}
        <a href="{{ item.Link }}">{{ item.Title }}</a>

        {# 判断当前一级分类是否有子分类,如果有,则继续嵌套显示 #}
        {% if item.HasChildren %}
        <div>
            {# 内层标签1:获取当前一级分类的子分类。parentId=item.Id 是关键 #}
            {% categoryList subCategories1 with parentId=item.Id %}
            <ul>
                {% for inner1 in subCategories1 %}
                <li>
                    {# 显示二级分类 #}
                    <a href="{{ inner1.Link }}">{{ inner1.Title }}</a>

                    {# 判断当前二级分类是否有子分类,如果有,则继续嵌套显示 #}
                    {% if inner1.HasChildren %}
                    <div>
                        {# 内层标签2:获取当前二级分类的子分类 #}
                        {% categoryList subCategories2 with parentId=inner1.Id %}
                        <ul>
                            {% for inner2 in subCategories2 %}
                            <li>
                                {# 显示三级分类 #}
                                <a href="{{ inner2.Link }}">{{ inner2.Title }}</a>
                            </li>
                            {% endfor %}
                        </ul>
                        {% endcategoryList %}
                    </div>
                    {% endif %}
                </li>
                {% endfor %}
            </ul>
            {% endcategoryList %}
        </div>
        {% endif %}
    </li>
    {% endfor %}
</ul>
{% endcategoryList %}

In this example:

  1. The firstcategoryList(Variable name)topCategoriesPassedparentId="0"Get all top-level categories.
  2. IntopCategoriesIn the loop, we use{% if item.HasChildren %}to determine if the current top-level category has subcategories.
  3. If there is a nested category, the second one will be nestedcategoryList(Variable name)subCategories1), at this timeparentIdis set dynamically toitem.Id, which is the ID of the current primary category, thereby obtaining its direct subcategories.
  4. Similarly, insubCategories1inside the loop, we judge againinner1.HasChildrenIf it exists, continue to nest the thirdcategoryList(Variable name)subCategories2)parentIdIt is set dynamically againinner1.IdTo get the subcategories of the second-level category.

In this way, we can flexibly build a clear and scalable classification navigation structure, whether it is the main navigation of the website, the sidebar navigation, or the website map, it can easily cope with.

Practical Application and Advanced Techniques

In addition to pure category hierarchy display,categoryListit can also be combined witharchiveListand other tags to achieve more powerful functions:

  1. Document list display under the categoryIn traversing categories, we may wish to display some articles or products under the category at the same time.

    {% categoryList categories with moduleId="1" parentId="0" %}
    <div>
        {% for item in categories %}
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <ul>
            {# 在每个分类下,调用该分类下的文档列表 #}
            {% archiveList archives with type="list" categoryId=item.Id limit="6" %}
            {% for archive in archives %}
            <li><a href="{{ archive.Link }}">{{ archive.Title }}</a></li>
            {% empty %}
            <li>暂无文档</li>
            {% endfor %}
            {% endarchiveList %}
        </ul>
        {% endfor %}
    </div>
    {% endcategoryList %}
    

    This example shows how to get the top-level categories in a loop and display the latest 6 documents under each category, greatly enriching the content display on the page.

  2. UtilizeHasChildrenField optimization displayIn building navigation, you can decideHasChildrenwhether to display subcategories or directly display documents under the current category.

    {% categoryList productCategories with moduleId="2" parentId="0" %}
    <nav>
        {% for item in productCategories %}
        <a href="{{ item.Link }}">{{ item.Title }}</a>
        <ul class="sub-nav">
            {% if item.HasChildren %}
                {# 如果有子分类,则显示子分类 #}
                {% categoryList subCategories with parentId=item.Id %}
                {% for inner in subCategories %}
                <li><a href="{{ inner.Link }}">{{ inner.Title }}</a></li>
                {% endfor %}
                {% endcategoryList %}
            {% else %}
                {# 如果没有子分类,则显示该分类下的产品文档 #}
                {% archiveList products with type="list" categoryId=item.Id limit="8" %}
                {% for inner in products %}
                <li><a href="{{ inner.Link }}">{{ inner.Title }}</a></li>
                {% endfor %}
                {% endarchiveList %}
            {% endif %}
        </ul>
        {% endfor %}
    </nav>
    {% endcategoryList %}
    

    This way makes navigation more dynamic, able to automatically adjust the displayed content according to the changes in the classification structure.

By the above method, you can fully utilize AnQiCMScategoryListThe powerful function of tags, building a beautiful and practical multi-level classification navigation, providing an excellent browsing experience for your website visitors.


Frequently Asked Questions (FAQ)

  1. How to limit the depth of multi-level classification display?You can control nestingcategoryListThe number of tag levels to limit the display depth. For example, if you only need to display three levels of classification, like the first example in this article, then you only need to nest two layerscategoryListLabel. If there is no subcategory under a certain category level,item.HasChildrenit will befalse, the inner loop will naturally not execute, thereby avoiding the display of empty levels.

  2. Can I get the parent category information of the category in the category loop?Get itsdirect parentAll information can be obtained throughitem.ParentIdFields are completed, but if you want to get the specific details of the parent level,TitleorLinkyou need to combinecategoryDetailLabel, for example{% categoryDetail parentCategory with name="Title" id=item.ParentId %}{{parentCategory}}.

  3. What should I do if I only want to display a specific number of top-level categories, but all the subcategories are displayed?You can specify in the outermost layercategoryListUsed in tagslimitparameters to limit the number of top-level categories, for example{% categoryList topCategories with moduleId="1" parentId="0" limit="5" %}. The nested insidecategoryListtags do not need to be setlimitThey will default to retrieve all matching subcategories.

Related articles

How to retrieve the title, content, image, and custom fields of a specific document using the `archiveDetail` tag?

How to use the `archiveDetail` tag in AnQiCMS to finely control the display of document content?AnQiCMS as an efficient and flexible content management system, one of its core advantages lies in its powerful content display capabilities.For each independent article, product detail, or any other 'document' type of content on the website, we hope to fine-tune their display methods on the front page.This is the place where the `archiveDetail` tag really shines.

2025-11-07

How to precisely filter and display a document list with specific categories, models, or recommended attributes using the `archiveList` tag?

In AnQi CMS, the `archiveList` tag is the core tool for building dynamic content lists, allowing you to flexibly extract and display the content you want from the website's document library.Whether it is to display the latest articles under a specific category, or popular products under a certain content model, or special document topics with specific recommendation attributes, `archiveList` can help you accurately meet these needs through its rich parameter configuration.### Accurately locate content: Classification, Model and Recommendation Attributes First

2025-11-07

How to dynamically display data using Django-style tags and variables in AnQiCMS templates?

AnQiCMS provides a flexible and powerful template system, which adopts a syntax style similar to the Django template engine, making experienced developers able to get started quickly, and it is also very intuitive, even users who are not familiar with template language can dynamically present website content through simple learning.This system, with its concise labels and variable usage, makes data interaction with the front-end page efficient and easy to manage.To fully utilize the AnQiCMS template for dynamic data display, we need to understand its core syntax structure: variables, tags, and filters

2025-11-07

How to choose and apply the adaptive, code adaptation, PC+mobile terminal template mode supported by AnQiCMS?

How to ensure that the content is presented elegantly and efficiently on various devices when building and operating a website is a challenge that every operator has to face.AnQiCMS as a feature-rich enterprise-level content management system, fully considers this point, and provides three flexible template modes of adaptive, code adaptation, and independent sites for PC and mobile phones, allowing us to make **choices according to actual needs.Next, we will delve into the characteristics, application scenarios, and how to configure and use these three modes in AnQiCMS

2025-11-07

How to use the `prevArchive` and `nextArchive` tags to navigate to adjacent documents in the article detail page?

In today's content-driven world, the user experience of the website article detail page is particularly important.When a reader is immersed in an excellent content, if they can navigate smoothly to related or adjacent articles, not only can it extend their stay on the website and increase the page views, but it can also indirectly optimize the search engine crawling and ranking through the internal link structure.AnQi CMS is an efficient and SEO-friendly content management system that fully considers these details and provides simple and intuitive template tags to help us easily achieve this function. Today

2025-11-07

How to get and paginate the display of all relevant documents under a specific `tagDataList` tag?

When managing content in Anqi CMS, tags (Tag) are an important tool for organizing and categorizing information.It can not only help users quickly find relevant content, but also significantly improve the internal link structure and SEO performance of the website.When it is necessary to display all relevant documents under a specific tag and to manage pagination, the `tagDataList` tag becomes an indispensable core function.

2025-11-07

How to correctly configure the `pagination` tag to generate a functional pagination navigation on the list page?

In website content management, configuring a fully functional pagination navigation for the list page is a key factor in improving user experience and optimizing the website structure.AnqiCMS (AnqiCMS) provides a simple and powerful `pagination` tag, allowing developers to easily meet this need.To make the `pagination` tag generate the pagination navigation correctly on the list page, it is first necessary to understand its mechanism and the collaborative relationship with content list tags (such as `archiveList`).

2025-11-07

How to use the `stampToDate` tag to format a timestamp into a custom date and time format?

Good, I'm glad to deeply interpret the `stampToDate` tag of AnQiCMS, helping you easily control the time display on the website. --- ## Flexible Time Mastery: Customize Date and Time Format with AnQiCMS `stampToDate` Tag The way dates and times are displayed on a website often directly affects user experience and the clarity of information communication in content management.A concise and readable date format allows visitors to quickly understand the timeliness of the content. AnQiCMS

2025-11-07