How to implement nested category display with multiple levels and how to judge if there is a subcategory in the template?

Calendar 👁️ 124

When building a website, a clear and organized content structure is crucial for enhancing user experience and SEO performance.AnQiCMS (AnQiCMS) relies on its flexible content model and powerful template engine to help us easily implement complex multi-level nested display of categories, perform subcategory judgments, and thus create highly customized navigation and content layouts.

Backend settings: Build category hierarchy

In the Anqi CMS backend, the category management feature (usually located under the 'Document Category' menu below 'Content Management') is the foundation for implementing multi-level classification.

First, we will create the top-level category. When adding a new category, you need to select a 'document model' (such as 'article model' or 'product model'), and make sure that the 'parent category' is selected as 'top-level category'.This defines the main structure of your website content.

Continue, create subcategories for these top-level categories. When adding subcategories, you also need to select a document model, but the key is to point the 'parent category' to its parent category.The AnQi CMS allows you to create multi-level nested categories as needed, for example:公司新闻The (first-level) can have行业动态(Second level),行业动态There can also be国内新闻(Third level). The system will properly manage these hierarchical relationships, preparing data for front-end display."),

Implement multi-level classification nesting display in the template.

Our AnQi CMS adopts a syntax similar to the Django template engine, allowing us to operate data in a straightforward manner within template files. To achieve nested display of multi-level categories, we mainly usecategoryListtags, with the help offorLooping and conditional judgment.

Suppose we want to display multi-level categories in the website navigation bar or sidebar. First, we need to get all the top-level categories. This can be done bycategoryListtags and setparentId="0"to achieve:

{% categoryList categories with moduleId="1" parentId="0" %}
    <ul>
        {% for item in categories %}
            <li>
                <a href="{{ item.Link }}">{{ item.Title }}</a>
                {# 这里我们将判断是否有子分类并显示 #}
            </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

In the code above,categoriesThis is an array containing all the top-level categories.forThe loop will iterate through these categories one by one.itemThe variable represents the current category object being looped over.

To display subcategories, we can add tags to each parent category's<li>Call internallycategoryListand pass the list of parent categories to the subcategories so that we can obtain their下属 subcategories:IdasparentId下属的子分类:

{% categoryList categories with moduleId="1" parentId="0" %}
    <ul>
        {% for item in categories %} {# 一级分类 #}
            <li>
                <a href="{{ item.Link }}">{{ item.Title }}</a>
                <div>
                    {% categoryList subCategories with parentId=item.Id %}
                        <ul>
                            {% for inner1 in subCategories %} {# 二级分类 #}
                                <li>
                                    <a href="{{ inner1.Link }}">{{ inner1.Title }}</a>
                                    {# 继续嵌套以获取三级分类 #}
                                    <div>
                                        {% categoryList subCategories2 with parentId=inner1.Id %}
                                            <ul>
                                                {% for inner2 in subCategories2 %} {# 三级分类 #}
                                                    <li>
                                                        <a href="{{ inner2.Link }}">{{ inner2.Title }}</a>
                                                    </li>
                                                {% endfor %}
                                            </ul>
                                        {% endcategoryList %}
                                    </div>
                                </li>
                            {% endfor %}
                        </ul>
                    {% endcategoryList %}
                </div>
            </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

Such a nested structure can clearly demonstrate the level hierarchy of categories. You can increase or decrease the nested levels according to your actual needs.

Determine if there are subcategories in the template.

In the display of multi-level categories, we often hope to render the submenu structure only when a category indeed has subcategories, otherwise, it may only display a simple link. Anqi CMS provides us withitem.HasChildrenThis is a very practical boolean attribute, which can be directly used for judgment in the template.

item.HasChildrenThe attribute will tell us whether the current category (item) contains any subcategories. We can combine{% if ... %}Label to control the display of content.

This is an optimized example that not only shows hierarchical categories, but also displays some documents under the category when there are no subcategories, or directly shows a category link:

{% categoryList topCategories with moduleId="1" parentId="0" %}
    <nav>
        <ul>
            {% for topCat in topCategories %} {# 遍历一级分类 #}
                <li>
                    <a href="{{ topCat.Link }}">{{ topCat.Title }}</a>

                    {% if topCat.HasChildren %} {# 判断当前一级分类是否有子分类 #}
                        <ul class="sub-menu">
                            {% categoryList subCategories with parentId=topCat.Id %}
                                {% for subCat in subCategories %} {# 遍历二级分类 #}
                                    <li>
                                        <a href="{{ subCat.Link }}">{{ subCat.Title }}</a>

                                        {% if subCat.HasChildren %} {# 判断当前二级分类是否有子分类 #}
                                            <ul class="sub-sub-menu">
                                                {% categoryList thirdCategories with parentId=subCat.Id %}
                                                    {% for thirdCat in thirdCategories %} {# 遍历三级分类 #}
                                                        <li>
                                                            <a href="{{ thirdCat.Link }}">{{ thirdCat.Title }}</a>
                                                        </li>
                                                    {% endfor %}
                                                {% endcategoryList %}
                                            </ul>
                                        {% else %}
                                            {# 如果二级分类没有子分类,可以显示该分类下的热门文档 #}
                                            <ul class="category-articles">
                                                {% archiveList articles with type="list" categoryId=subCat.Id limit="5" %}
                                                    {% for article in articles %}
                                                        <li><a href="{{ article.Link }}">{{ article.Title }}</a></li>
                                                    {% endfor %}
                                                {% endarchiveList %}
                                            </ul>
                                        {% endif %}
                                    </li>
                                {% endfor %}
                            {% endcategoryList %}
                        </ul>
                    {% else %}
                        {# 如果一级分类没有子分类,可以直接显示该分类下的最新文档 #}
                        <ul class="category-articles">
                            {% archiveList articles with type="list" categoryId=topCat.Id limit="5" %}
                                {% for article in articles %}
                                    <li><a href="{{ article.Link }}">{{ article.Title }}</a></li>
                                {% endfor %}
                            {% endarchiveList %}
                        </ul>
                    {% endif %}
                </li>
            {% endfor %}
        </ul>
    </nav>
{% endcategoryList %}

In this example, we usetopCat.HasChildrenandsubCat.HasChildrenPrecisely controlled the display of the submenu. When a category does not have subcategories, we choose to display the list of articles below it instead of an empty submenu, thus optimizing the user experience and improving the visibility of the content.

Summary

AnQi CMS provides a powerful and flexible template tag, which makes it very simple to implement nested display of multi-level categories and judgment of subcategories. By reasonable use ofcategoryListTags anditem.HasChildrenAttribute, you can build highly structured, feature-rich website navigation and content layout to meet various complex website design requirements.In order to improve user navigation efficiency or optimize search engine crawling, these techniques will help you a lot.

Frequently Asked Questions (FAQ)

  1. Can I display category images or description information in multi-level category display at the same time?Of course. IncategoryListthe loop,itemVariables are used toId/Title/LinkandHasChildrenIn addition to basic attributes, it also includesLogo(Thumbnail, full image),,Thumb(Thumbnail) andDescription(Category description) fields. You can directly use them in the template through{{ item.Logo }}or{{ item.Description }}in this way to call this information. If the category content contains HTML tags, remember to use{{ item.Content|safe }}filter to prevent content from being escaped.

  2. How to limit the number of subcategories displayed at each category level?You can control the display quantity at each nestedcategoryListthe tag withlimitparameter. For example,{% categoryList subCategories with parentId=topCat.Id limit="5" %}Only the first 5 subcategories will be displayed. If you need to start from a certain position, you can also uselimit="offset,count"Pattern, for examplelimit="2,5"which means starting from the second

Related articles

How to retrieve and display details of a specific category, including description, content, and Banner image?

When operating a website, we often need to display unique content for different category pages, such as a product category may have a detailed introduction and an eye-catching Banner image, while a news category may focus more on a concise description.AnQi CMS provides a flexible mechanism that allows us to easily retrieve and display detailed information about specific categories, including descriptions, content, and Banner images.### Backend settings are fundamental: enriching categories Before displaying category details on the frontend, we need to fill in this information in the Anqi CMS backend

2025-11-08

How to add and display a captcha in the comment or message form to prevent spam?

When operating a website, spam comments and malicious messages are always a headache, as they not only affect user experience but may also damage the professional image of the website.Fortunately, AnQi CMS provides a simple and effective way to solve this problem - that is to add a captcha to the comment or message form.This can greatly enhance the security of the website, filtering out most automated spam information.Below, let's take a detailed look at how to add a captcha to your comment and message form in Anqi CMS.This process can be divided into two steps, which is very intuitive and easy to operate.### Step 1

2025-11-08

How to retrieve and display the list of friend links of a website?

In website operation, friendship links play an important role in connecting each other, promoting traffic, and improving search engine rankings.A well-maintained, clear display of the friendship link area, which not only reflects the openness of the website, but also provides users with more valuable jump paths.AnQiCMS provides users with a convenient friend link management feature, allowing you to easily obtain and display these links on your website. ### Manage your friend links: Backend settings Before starting the front-end display, we need to make sure that the friend links have already been configured in the Anqi CMS backend. Usually

2025-11-08

How to get and display the list of all documents associated with a specified Tag?

Manage content in AnQi CMS, besides organizing through categories, flexibly using the "Tag" feature is also the key to improving the aggregation ability of website content and user experience.Tags can connect documents of different categories, different models, but with the same theme, providing visitors with more ways to discover content.How can we specifically retrieve and display the list of all documents associated with the specified tags?Next, we will delve deeper into this practical skill. ### Understanding the Tag System of AnQi CMS The tag design of AnQi CMS is very flexible, it does not depend on any specific category or model

2025-11-08

How to integrate a category list in the navigation menu and display product documents under each category?

In AnQi CMS, integrating product categories into the navigation menu and displaying the product documents below is a crucial step to enhance website user experience and content organization efficiency.This can help visitors quickly find the information they need, and it also helps search engines better understand the structure of the website. To achieve this goal, we need to make some basic settings in the Anqi CMS backend and integrate the corresponding code in the template files according to these settings.

2025-11-08

How to obtain and display the links and titles of the previous and next articles?

When we manage and publish content in Anqi CMS, in order to enhance the user experience and content discoverability of the website, it is usually necessary to provide the function of navigating to the 'previous article' and 'next article' on the article detail page.This not only encourages readers to continue browsing the content within the site, but also provides a good internal link structure for search engine optimization (SEO).The Anqi CMS provides a very concise and intuitive template tag, allowing us to easily meet this requirement.### Core Functionality: `prevArchive` and `nextArchive`

2025-11-08

How to retrieve and display the Logo, thumbnail, and slideshow group on a single page?

In Anqi CMS, single pages (such as "About Us", "Contact Us", etc.) can not only carry rich text information but also greatly enhance the attractiveness and professionalism of the page through visual elements.Retrieve and display the Logo, thumbnail, and slideshow group on a single page, which is a key step to optimize the page display effect.AnQi CMS provides flexible template tags, allowing you to easily achieve these functions.### Understand the composition of visual elements on a single page In Anqi CMS, each single page can have exclusive visual elements to meet different display needs

2025-11-08

How to display copyright information and link to the record number at the bottom of the website?

Clearly display copyright information and filing number at the bottom of your website, which is not only a legal requirement but also a key factor in enhancing the professionalism and user trust of the website.AnQi CMS as an efficient content management system, fully considers these details, allowing you to easily manage and present these important site information. ### Understanding the Importance of Copyright Information and Filing Numbers The copyright statement at the bottom of the website, like a brand watermark, clearly identifies the ownership of the content to visitors, effectively protecting your original achievements.As for websites operating in mainland China

2025-11-08