How does the AnQiCMS `categoryList` tag display hierarchical category structures and show category information?

Calendar 👁️ 67

In AnQi CMS, website categories are the foundation for organizing content, whether it is articles, products, or other custom models. A clear category structure can greatly enhance user experience and the SEO effect of the website. AnQi CMS provides powerfulcategoryListLabels, allowing us to flexibly display single or multi-level classification structures and easily obtain detailed information about each classification item.

MastercategoryListBasic usage of tags

To display the classification list, we first need to usecategoryListThe label. Its core function is to retrieve category data from the background and display it in a list on the front-end page. The basic usage usually specifies which content model to retrieve.moduleIdThe classification, as well as from which level to start(parentId)

For example, if we want to display the article model (assumingmoduleIdAll top-level classifications for 1, we can write the template code like this:

{% categoryList categories with moduleId="1" parentId="0" %}
    <ul>
        {% for item in categories %}
        <li>
            <a href="{{ item.Link }}">{{item.Title}}</a>
        </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

Here, categoriesIt is our custom variable name, which carries the category data list obtained from the background.parentId="0"It indicates that the request is for the top-level category without a parent.itemIs each category object in the loop, throughitem.Linkanditem.TitleWe can respectively obtain the link and name of the category.

Build a multi-level category structure

Of Security CMScategoryListThe tag is very suitable for building nested multi-level classification menus. The key to achieving this is to call the loop again inside.categoryListThe tag, and use the current category ID as the subcategory ofparentId. At the same time,item.HasChildrenThis field helps us determine whether the current category has subcategories, thereby conditionally rendering the submenu.

For example, creating a three-level navigation menu might look like this:

<nav>
    <ul class="main-nav">
        {% categoryList topCategories with moduleId="1" parentId="0" %}
            {% for item in topCategories %}
            <li class="{% if item.HasChildren %}has-dropdown{% endif %}">
                <a href="{{ item.Link }}">{{ item.Title }}</a>
                {% if item.HasChildren %}
                    <ul class="sub-nav">
                        {% categoryList subCategories with parentId=item.Id %} {# 使用当前分类的ID作为父ID #}
                            {% for inner1 in subCategories %}
                            <li class="{% if inner1.HasChildren %}has-dropdown{% endif %}">
                                <a href="{{ inner1.Link }}">{{ inner1.Title }}</a>
                                {% if inner1.HasChildren %}
                                    <ul class="third-nav">
                                        {% categoryList thirdCategories with parentId=inner1.Id %} {# 再次嵌套获取第三级分类 #}
                                            {% for inner2 in thirdCategories %}
                                            <li>
                                                <a href="{{ inner2.Link }}">{{ inner2.Title }}</a>
                                            </li>
                                            {% endfor %}
                                        {% endcategoryList %}
                                    </ul>
                                {% endif %}
                            </li>
                            {% endfor %}
                        {% endcategoryList %}
                    </ul>
                {% endif %}
            </li>
            {% endfor %}
        {% endcategoryList %}
    </ul>
</nav>

This code first retrieves all top-level categories, and then inside the loop of each top-level category, ifitem.HasChildrenis true, it will call againcategoryListTo get its subcategories. This process can be nested according to actual needs, perfectly presenting the hierarchical structure of the website.

It is worth mentioning,item.SpacerThis field may contain a prefix for visual indentation when displayed on the background category list page, such as|--. If you want to retain this visual hierarchy in the front-end template, you can use{{item.Spacer|safe}}Display it before the category title.

Show the detailed information of the category

In addition to the name and link, the Anqi CMS'scategoryListtags can also help us obtain many other practical information about the category in.forin the loopitemAn object that contains rich data fields, which can help us build richer classification displays.

Common classification information includes:

  • item.Id: Category ID, it is the unique identifier for identifying the category.
  • item.Description: Brief introduction or description of the category, often used in page headers or list details.
  • item.Content: Detailed category content, if filled in the background, it needs to be used{{ item.Content|safe }}to ensure correct rendering of HTML content.
  • item.Logo: The category thumbnail large image address, which can be used for banners or prominent positions.
  • item.Thumb: The category thumbnail address, usually used for list display.
  • item.ArchiveCountThe number of documents in this category, which helps to provide data insights.
  • item.IsCurrentDetermine whether the current category is the category of the current page, which can be used to highlight the current navigation item.

For example, we may need to display the category description and thumbnail next to the category list:

{% categoryList categories with moduleId="1" parentId="0" %}
    <div class="category-grid">
        {% for item in categories %}
        <div class="category-card">
            <a href="{{ item.Link }}">
                {% if item.Thumb %}
                    <img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="category-thumb">
                {% endif %}
                <h3 class="category-title">{{ item.Title }}</h3>
            </a>
            {% if item.Description %}
                <p class="category-desc">{{ item.Description }}</p>
            {% endif %}
            <span class="article-count">文档数量: {{ item.ArchiveCount }}</span>
        </div>
        {% endfor %}
    </div>
{% endcategoryList %}

If the category has custom fields, for example, the background adds a field called "category负责人" whose calling field ismanager, then you can also use{{ item.manager }}To directly obtain its value or by loopingitem.ExtraTo obtain all custom fields.

By flexible applicationcategoryListThe tags and fields they provide allow us to build a multi-level classification structure that is both beautiful and practical in AnQi CMS, providing users with an intuitive and efficient navigation experience.


Frequently Asked Questions (FAQ)

1. How to determine in the category list whether the current category is the category of the page being viewed and highlight it?

IncategoryListIn the loop, eachitemAn object contains aIsCurrentboolean value. Whenitem.IsCurrentWithtrueAt the time, it indicates that the category is the category belonging to the current page. You can use this property to add a CSS class and achieve a highlight effect.

For example:

{% categoryList categories with moduleId="1" parentId="0" %}
    <ul>
        {% for item in categories %}
        <li class="{% if item.IsCurrent %}active-category{% endif %}">
            <a href="{{ item.Link }}">{{item.Title}}</a>
        </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

**2. I want to display categories that belong to different content models, such as displaying article categories and product categories,

Related articles

How does the `relatedArchive` tag display a list of related articles based on document relevance?

In the daily operation of a website, how to make users naturally discover more interesting content after reading an article is the key to improving user experience, extending the time spent on the website, and even optimizing SEO effects.AnQi CMS understands this and provides powerful template tag features, where the `relatedArchive` tag is exactly used to intelligently display related article lists.## `relatedArchive` label

2025-11-07

How to display the link to the previous and next documents on the AnQiCMS document detail page?

In the ocean of website content, users often hope to browse related information smoothly, not wanting to return to the list to find the next article after reading a wonderful article.This not only affects user experience, but also benefits the internal link structure of the website and search engine optimization (SEO) a lot.AnQiCMS has fully considered this point, providing a convenient and quick navigation function for the content detail page, allowing users to easily navigate between different documents.The AnQiCMS template system is very flexible, it adopts the syntax of the Django template engine

2025-11-07

How to retrieve and display the complete content and properties of a single document using the `archiveDetail` tag?

When using AnQiCMS to build website content, displaying the complete information of a single document is one of the core requirements.No matter whether it is the detailed content of an article, a detailed introduction of a product, or any other specific entry of a custom content model, a highly efficient and flexible way is needed to extract and present this data.This is where the `archiveDetail` tag comes into play.

2025-11-07

How to add pagination and optimize the display effect for document lists in AnQiCMS?

In website operation, how to efficiently display a large amount of content while ensuring the smoothness of user experience is a topic that cannot be ignored.The pagination feature of the document list is the key to solving this problem.AnQiCMS (AnQiCMS) is an efficient and flexible content management system that provides intuitive and powerful template tags, allowing you to easily add pagination features to document lists and optimize the display on top of that, making your website content more attractive.

2025-11-07

How to retrieve and display detailed information such as description, Logo, etc. for a specific category using the `categoryDetail` tag?

In AnQiCMS template development, obtaining and displaying detailed category information is a key step in building rich and user-friendly pages.The `categoryDetail` tag was created for this purpose, it can help you easily retrieve the description, Logo, custom fields, and other detailed content of a specific category at any location on the page.### `categoryDetail` tag's core function The main function of the `categoryDetail` tag is to obtain detailed data of a single category

2025-11-07

How does the `pageList` tag display all the titles and links of the individual pages of the website?

In Anqi CMS, using the `pageList` tag to display the titles and links of all single pages is a very common and practical need in website layout and navigation settings.Whether it is used for website footer navigation, sidebar link list, or building a simple HTML site map, the `pageList` tag can help us efficiently achieve these functions. ### Understanding AnQi CMS Single Page Firstly, let's briefly review the "single page" in AnQi CMS.In the Anqi CMS backend management, there is a special "Page Resources" module

2025-11-07

How does the `pageDetail` tag display the detailed content of a single page, such as the 'About Us' page?

In Anqi CMS, single pages (such as "About UsTo present the detailed content of these single pages on the website frontend, we will use a very core and practical template tag——`pageDetail`.This tag helps us accurately extract and display various information on a specified single page, ensuring the flexibility and accuracy of content presentation.

2025-11-07

How does the AnQiCMS `tagList` tag display all associated tags of the document?

## Easily display document-related tags: In-depth analysis of AnQiCMS's `tagList` tag In content management, tags (Tag) play a crucial role.They can not only help us better organize content and improve the internal link structure of the website, but also play a huge role in search engine optimization (SEO), making it easier for users and search engines to find relevant information.AnQiCMS has fully considered this point and provided a flexible and powerful tag management system.Among, `tagList`

2025-11-07