How to implement nested category display in AnQiCMS template

Calendar 👁️ 73

As an experienced website operations expert, I know the value of a flexible and efficient content management system for the success of a website.AnQiCMS (AnQiCMS) boasts its high-performance architecture based on the Go language and the Django-style template engine, which brings great convenience and endless possibilities to content display.Today, let's delve deeply into a very common demand in actual operation: How to implement nested category display in AnQiCMS templates, making your website structure clearer and user experience even better.

The foundation of organizing content: Understanding AnQiCMS classification structure

Before delving into the template code, we first need to understand how AnQiCMS organizes categories in the background.One of the core strengths of AnQi CMS is itsEach category can specify its 'parent category' when created, which naturally establishes a hierarchical relationship.For example, you can have a 'news center' as a primary category, under which you can have 'industry news' and 'company dynamics' as secondary categories. If needed, you can even set up a third-level category.This hierarchical relationship is the basis for nested display in our template.

In the background management interface, we can see that each category not only has a name and a brief introduction, but also some important attributes, such as the "document model" it belongs to and the most important "parent category" ID.At the same time, AnQiCMS also allows us to set custom URLs, thumbnails, and even exclusive 'category templates' for categories, which provides us with rich operability for subsequent template design.

The art of template engine: The use of Django-like syntax

The template creation of AnQiCMS is very user-friendly, it uses a syntax similar to Django's template engine.This means you can use intuitive labels to control the display logic of content.{{ 变量名 }}while logical judgments and loop control use single curly braces and percentage signs{% 标签 %}The core tool for nested display of multi-level categories isforloop tags andifConditional judgment tags, they will help us traverse the category data and render it conditionally according to its hierarchical relationship.

The core of realizing multi-level category nesting:categoryListTag

We mainly rely on displaying category lists in the AnQiCMS template,categoryListLabel. This label is powerful and can retrieve classification data of articles or products, and supports flexible filtering conditions.

categoryListThe key parameters of the label include:

  • moduleId: Used to specify which content model category we want to obtain (e.g., article model ID is 1, product model ID is 2).
  • parentId: This is the core of achieving nested display. When set to"0"When it does, it retrieves all top-level categories; while if we want to get the child categories of a specific category, we can setparentIdto the parent category ofId.
  • allIf set totrueand combinemoduleIdCan retrieve all categories under a specified model, regardless of the level.

categoryListThe label will return an array containing category objects.forIn the loop, each category object (we usually name ititem) includes many useful properties, such as:

  • IdUnique ID of the category.
  • Title: Category name.
  • Link: Link to the category page.
  • ParentId: ID of the parent category.
  • HasChildrenA boolean value indicating whether the current category contains subcategories. This property is particularly important when building nested structures, as it can be used to determine whether to render the next level menu.
  • SpacerA prefix used to display the hierarchy relationship in the background, usually not used in the front-end display, but can help understand the hierarchy.

Build nested classification templates step by step

Let's go through a specific example to show how to usecategoryListtags to achieve nested display of three-level classification.

Suppose we want to display the article model (moduleId="1"All categories under this level are presented in a nested list format:

{%- comment -%} 首先,我们获取所有顶级分类 (parentId="0") {%- endcomment -%}
{% categoryList categories with moduleId="1" parentId="0" %}
<ul>
    {%- for item in categories %}
    <li>
        <a href="{{ item.Link }}">{{item.Title}}</a>
        {%- comment -%} 检查当前分类是否有子分类,如果有,则继续获取下一级 {%- endcomment -%}
        {%- if item.HasChildren %}
        {%- comment -%} 获取当前一级分类的子分类,将当前分类的ID (item.Id) 作为下一级的 parentId {%- endcomment -%}
        {% categoryList subCategories with parentId=item.Id %}
        <ul>
            {%- for inner1 in subCategories %}
            <li>
                <a href="{{ inner1.Link }}">{{inner1.Title}}</a>
                {%- comment -%} 再次检查当前二级分类是否有子分类 {%- endcomment -%}
                {%- if inner1.HasChildren %}
                {%- comment -%} 获取当前二级分类的子分类,将当前二级分类的ID (inner1.Id) 作为下一级的 parentId {%- endcomment -%}
                {% categoryList subCategories2 with parentId=inner1.Id %}
                <ul>
                    {%- for inner2 in subCategories2 %}
                    <li>
                        <a href="{{ inner2.Link }}">{{inner2.Title}}</a>
                    </li>
                    {%- endfor %}
                </ul>
                {%- endcategoryList %}
                {%- endif %}
            </li>
            {%- endfor %}
        </ul>
        {%- endcategoryList %}
        {%- endif %}
    </li>
    {%- endfor %}
</ul>
{%- endcategoryList %}

In this code, we巧妙ly use nested structurescategoryListTags andforLoop:

  • The outermostcategoryListResponsible for retrieving the top-level categories.
  • Within the loop of each top-level category, we use{% if item.HasChildren %}to determine if it has subcategories.
  • If there are subcategories, we callcategoryListagain, but this time we setparentIdto the current parent category's{{ item.Id }}Thus, you can obtain the next level of classification.
  • This process can be nested infinitely according to your actual needs until all levels of classification are displayed.

Advanced Practice: Combining Navigation List Nesting

In addition to directly displaying categories, the navigation menu of our website also often needs to present multi-level categories. AnQiCMS'snavListTags also support multi-level nesting and can be easily combined with category data to achieve dynamic navigation.

InnavListEach navigation item in the tag,itemhasNavListA property, which itself is also an array, containing the child navigation items of the current navigation item. If the navigation item is linked to a category page, its

Related articles

How to reference the common header and footer files in AnQiCMS templates?

As an experienced website operations expert, I fully understand how important it is to maintain consistency in website structure and development efficiency in a content management system.Especially when using a highly efficient and flexible system like AnQiCMS, it is reasonable to refer to common header and footer files, which can not only greatly improve the maintenance efficiency of templates, but also ensure the unity of the entire site style.Today, let's delve into how to achieve this goal in AnQiCMS templates.AnQiCMS with its high-performance architecture based on Go language and Django-style template syntax

2025-11-07

How to determine if a variable is empty or execute conditional logic in AnQiCMS templates?

As an experienced website operation expert, I am well aware of how crucial precise control of template logic is when building and maintaining a high-efficiency, user-friendly website.Especially when using a content management system like AnQiCMS, which is based on Go language and focuses on performance and flexibility, it is possible to flexibly judge the status of variables and execute conditional logic according to different situations, which is the foundation for ensuring the correct display of content, avoiding page errors, and improving the user experience.The template engine of AnQi CMS uses a tagging style similar to Django

2025-11-07

How to use a for loop to iterate over a data list in AnQiCMS template?

## Master AnQiCMS Templates: The Art of Using `for` Loops to Traverse Data Lists Hello, all AnQiCMS operation partners!In daily website content operations, we often need to display a series of data, whether it is the latest article list, product category navigation, or comment details, it is inseparable from the traversal and display of the data set.AnQiCMS as an efficient and flexible content management system, deeply understands the importance of templates in content presentation. Today

2025-11-07

What control flow tags does AnQi CMS template language support (such as if, for)?

## AnQi CMS template language: if, for, allowing flexible control of content display As an experienced website operation expert, I deeply understand the importance of a flexible and efficient content management system to a company.AnQiCMS (AnQiCMS) stands out in the content operation field with its high-performance architecture based on the Go language and its highly customizable nature.In daily content display and page construction, control flow tags in template languages, such as `if` and `for`, are undoubtedly the core tools for us to achieve dynamic and personalized content presentation. Today

2025-11-07

What are the common filters supported by AnQiCMS for processing template data (such as string truncation, date formatting)?

In the Anqi CMS template world, data is not just raw text or numbers, they are the foundation of excellent website content.However, the original data often needs to be polished before it can present itself in a certain posture to the user.At this time, the template filter has become an indispensable helper to us.They are like a Swiss Army knife for data processing, capable of performing various transformations, formatting, and refining template variables, bringing new luster to the data and meeting all kinds of display needs.As an experienced website operation expert

2025-11-07

How to format timestamp display in AnQiCMS template?

As an experienced website operation expert, I am well aware that the timeliness and presentation of content are crucial to user experience.In an efficient content management system like AnQiCMS, even a seemingly simple date display contains operational wisdom to enhance the professionalism and readability of the website.Today, let's delve into how to beautifully transform the original timestamps into user-friendly date formats in the AnQiCMS template.

2025-11-07

How to define and use variables (set, with) in AnQiCMS templates?

Mastery in AnQiCMS templates: Unlocking the Power of Variables (Deep Analysis of set and with) As an experienced website operations expert, I know that a flexible and efficient content management system is crucial for the success of a website.AnQiCMS with its concise and efficient architecture and support for Django template engine syntax, provides us with great convenience.In template development, being proficient in variable definition and assignment is undoubtedly the key to enhancing template flexibility and maintainability.

2025-11-07

How to generate random text for testing in the AnQiCMS template?

As an experienced website operations expert, I am well aware that during the website development and content testing stage, it is often difficult to cook without the necessary ingredients - lacking real content to fill the page, in order to verify design, layout and function.Especially for operators and developers who use powerful and flexible content management systems like AnQiCMS (AnQiCMS), efficiently simulating content is the key to improving work efficiency.

2025-11-07