How to display the category list and its hierarchical relationship in AnQiCMS template?

Calendar 👁️ 69

When building a website, a clear classification structure is the key to improving user experience and content discoverability.AnQiCMS with its flexible content model and powerful template engine allows us to easily display category lists and their hierarchical relationships on the website front end.Today, let's delve deeper into how to implement this feature in the AnQiCMS template.

Understand the AnQiCMS category list tags:categoryList

The AnQiCMS template is based on the Django template engine syntax in the Go language, providing intuitive and feature-rich tags for data manipulation. To display the category list, we will mainly usecategoryListThis tag. This tag helps us obtain classification data for content models such as articles or products, and display them in the way we expect.

Let's start with a simple example. Suppose we want to display all top-level article categories in the website sidebar, we can write the template code like this:

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

In this code block:

  • {% categoryList categories ... %}We define a variable namedcategoriesThe variable will carry the category list data retrieved from the database.
  • moduleId="1"This is a very critical parameter. In AnQiCMS, each category belongs to a specific content model (for example, the article model is usually for...1The product model may be2Specified by"),moduleIdWe ensure that only the required content model classification is obtained.
  • parentId="0"This parameter tells the system that we only want to retrieve the top-level categories. If we want to retrieve the subcategories under a specific category, we will pass the parent category'sId.
  • {% for item in categories %}We useforto loop throughcategoriesEach category item in the variable.
  • {{ item.Link }}This will output the current category's URL link.
  • {{ item.Title }}This will display the current category's name.

Elegantly display multi-level category hierarchy

If our website category structure has a hierarchical relationship, such as 'News Dynamic' under 'Company News' and 'Industry Information', we can also usecategoryListLabels easily achieve multi-level nested display.

Here is an example of displaying two-level classification:

{% categoryList mainCategories with moduleId="1" parentId="0" %}
    <ul class="main-nav">
        {% for mainItem in mainCategories %}
        <li class="main-nav-item">
            <a href="{{ mainItem.Link }}">{{ mainItem.Title }}</a>
            {% if mainItem.HasChildren %}
            <ul class="sub-nav">
                {% categoryList subCategories with parentId=mainItem.Id %}
                    {% for subItem in subCategories %}
                    <li class="sub-nav-item">
                        <a href="{{ subItem.Link }}">{{ subItem.Title }}</a>
                        {# 如果需要更多层级,可以继续嵌套 categoryList 标签 #}
                    </li>
                    {% endfor %}
                {% endcategoryList %}
            </ul>
            {% endif %}
        </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

Here we introduce several new concepts:

  • {% if mainItem.HasChildren %}: This is a very useful conditional judgment.HasChildrenThe property returns a boolean value that tells us whether the current category has subcategories. We only render the submenu when a category has subcategories to avoid generating empty<ul>Tags, make the page structure cleaner.
  • parentId=mainItem.Id: Inside the inner loop, we will iterate over the outer loopmainItemofIdasparentIdpass tocategoryListTags, so we can accurately get tomainItemthe direct child category.
  • item.Spacer: AnQiCMS also provides aSpacerfield, which automatically generates prefixes based on the category level (for example&nbsp;&nbsp;|--), in the management background or in some specific display scenarios, it can help us intuitively see the level depth of the categories. When displayed on the front end, it is usually controlled by CSS to control the indentation and level styles, butSpacerProvided an alternative option, remember to use|safeA filter to ensure HTML entities are parsed correctly

Display documents under the category list at the same time

Sometimes, we want not only to display the category itself, but also to show some of the latest articles or products under each category. AnQiCMS also allows us to flexibly combinecategoryListandarchiveListTag to achieve this requirement.

{% categoryList categories with moduleId="1" parentId="0" %}
    <div class="category-block">
        {% for catItem in categories %}
        <h3><a href="{{ catItem.Link }}">{{ catItem.Title }}</a></h3>
        <ul class="latest-articles">
            {% archiveList archives with type="list" categoryId=catItem.Id limit="5" %}
                {% for article in archives %}
                <li>
                    <a href="{{ article.Link }}">
                        <h4>{{ article.Title }}</h4>
                        <p>{{ article.Description|truncatechars:100 }}</p>
                    </a>
                </li>
                {% empty %}
                <li>当前分类暂无文章。</li>
                {% endfor %}
            {% endarchiveList %}
        </ul>
        {% endfor %}
    </div>
{% endcategoryList %}

In this example:

  • We do this in eachcatItemA loop nested inside anotherarchiveList.
  • categoryId=catItem.Id: Here we will classify the currentcatItemofIdpass toarchiveList, to ensure that only the articles under this category are obtained.
  • limit="5": Limit each category to show the latest 5 articles.
  • type="list": It means we just need a simple list, not a paginated list.
  • {{ article.Description|truncatechars:100 }}: UsedtruncatecharsA filter to truncate the first 100 characters of the article description and automatically add an ellipsis to keep the page neat.

Summary

AnQiCMS'categoryListThe label and its rich parameters and return properties provide us with great flexibility to build various complex classification displays.Whether it is a simple flat list, a multi-level nested menu, or directly embedding related content in the category, it can be easily realized through an intuitive template syntax.Reasonably utilizing these features will help us build a website with clear structure, rich content, and user-friendly interface.In actual operation, don't forget to check the tag details in the official AnQiCMS documentation, where there will be more detailed parameter descriptions and advanced usage, helping you to realize more creativity.


Frequently Asked Questions (FAQ)

1. Why is my category list code written, but there is no content displayed on the frontend page?

This usually has several reasons: First, please checkmoduleIdAre the parameters correct. In AnQiCMS, categories are bound to specific content models (such as articles, products), ifmoduleIdIncorrect or no article/product category, the list will not display naturally. Secondly, confirm the specifiedparentIdIf used, subcategories indeed exist. Finally, check if the corresponding category data has been created in your AnQiCMS backend.

2. I want to display categories in the navigation bar, but the tags can also be used for navigation, which one should I use?navList标签也可以做导航,我应该用哪一个?

navListThe tag is specifically used to display the navigation menu configured in the background "Website Navigation Settings".It focuses more on the global navigation structure of the website, which may include combinations of categories, single pages, or external links and so on.categoryListThe tag focuses on retrieving and displaying pure classification data from the content model. If you need a navigation generated automatically based on backend classification data (for example, displaying all article categories),categoryListIt is more suitable; if you want the navigation menu content to be more flexible, you can manually drag and adjust the order in the background, and it includes various link types, thennavListIt would be a better choice. Both can be combined, for example, innavListnested under some menu itemcategoryListto display subcategories.

How to set different styles for different levels of categories (such as using bold for first-level categories and italic for second-level categories)?

IncategoryListofforIn the loop, you can useitem.ParentIdTo determine the level of the current category. For example, ifitem.ParentId == 0, it is a top-level category; ifitem.ParentId > 0, it is a subcategory. You can also combine CSS class names to achieve differentiated styles. For example:

{% categoryList categories with moduleId="1" %}
    <ul>
        {% for item in categories %}
            <li class="{% if item.ParentId == 0 %}level-1{% else %}level-2{% endif %}">
                <a href="{{ item.Link }}">{{ item.Title }}</a>
                {# ... 更多嵌套分类 ... #}
            </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

In this way, you can uselevel-1andlevel-2CSS classes to define different levels of styles.

Related articles

How to set image lazy loading in AnQiCMS to optimize page display performance?

Website loading speed is one of the key factors in user experience and search engine optimization.Among the many factors affecting page performance, images are often the elements that consume the most resources and take the longest to load.To provide users with a smoother browsing experience and improve the website's performance in search engines, it is particularly important to optimize the image loading strategy.AnQiCMS itself is known for its efficiency and lightness, developed in Go language, with excellent high concurrency processing capabilities and fast response speed.On this basis, by introducing the Lazy Load image technology

2025-11-08

How can I automatically convert AnQiCMS document content to Markdown format and correctly render it on the webpage?

AnQiCMS as a powerful content management system provides great flexibility in content creation and presentation.For users accustomed to writing in Markdown syntax, AnQiCMS also provides convenient support, allowing content creators to focus on the text itself without paying too much attention to formatting details.Starting from version 2.1.1, AnQiCMS has deeply integrated support for Markdown editors, making it easy to format document content in Markdown and render web pages

2025-11-08

How to control the special display of articles in different areas of AnQiCMS's 'recommended attributes'?

In AnQiCMS, the 'recommended attribute' is a very practical feature that allows us to fine-tune the marking and classification of the content we publish, thereby achieving characteristic displays in different areas of the website.The core of this feature lies in assigning specific "flags" or "tags" to articles, allowing them to be selectively called in templates, thereby enhancing the visibility of the content and the overall operational efficiency of the website.What is a 'Recommended Attribute'? To put it simply, a 'Recommended Attribute' is some special tags that we can add to an article when we publish or edit it.

2025-11-08

How to configure AnQiCMS to implement multilingual content switching and display?

Today, with the increasing popularity of globalization, multilingual support for websites is no longer an optional feature, but an important cornerstone for enterprises to expand into international markets and serve global users.AnQiCMS (AnQiCMS) is an efficient and customizable enterprise-level content management system that fully considers this requirement and provides a flexible multi-language content switching and display solution.This article will discuss in detail how to configure and manage multilingual content in AnQiCMS, allowing your website to easily face the world.--- ### One

2025-11-08

How to retrieve and display detailed information of a specific category, such as the category introduction and Banner image?

In website operation, clear and attractive categorization information is crucial for improving user experience and optimizing search engine performance.A well-designed category page can not only help users quickly understand the theme of the category, but also attract users by displaying relevant visual elements (such as banner images) and provide rich content for SEO.Strong and flexible features provided by AnQi CMS, making it easy to achieve these goals. ### Configure Category Information in AnQi CMS Backend Managing detailed category information in AnQi CMS is very intuitive.Log in to the backend management interface after

2025-11-08

How does AnQiCMS's 'Time Factor - Scheduled Publishing' feature affect the public display of content?

In the era where content is king, the timing and rhythm of website content release often determines its propagation effect and user reach rate.For content operators, manually launching content at every important moment is not only time-consuming and labor-intensive, but also prone to missing the opportunity due to carelessness.AnQiCMS knows this pain point and has specially introduced the "Time Factor-Scheduled Publishing" feature, aiming to make the content launch process more intelligent and accurate, providing users with a more relaxed content management experience.Understanding the operation mechanism of "Time Factor-Scheduled Publishing" In the AnQiCMS management background

2025-11-08

How to display the title and link of the 'Previous' and 'Next' articles on the article detail page?

In website content operation, the navigation design of the article detail page is crucial.A well-designed user experience detail page that can clearly present the current content and naturally guide readers to browse more related or coherent articles.The display of the 'Previous' and 'Next' articles is a classic approach to enhance user stickiness and the efficiency of the website's internal link structure.AnqiCMS is an enterprise-level content management system developed based on the Go language, which fully considers the efficiency and flexibility of content management from the beginning of its design.It relies on a concise and efficient architecture

2025-11-08

How does AnQiCMS display a list of recommended content related to the current article?

In content management and website operations, how to effectively attract visitors, extend their stay on the website, and guide them to discover more interesting content is a crucial issue.AnQiCMS (AnQiCMS) provides a powerful and flexible mechanism that helps us easily display a list of related recommended content on the current article page, thereby greatly enhancing the user experience and the overall performance of the website.**Core Mechanism: `archiveList` tag's `type="related"` attribute** AnQiCMS

2025-11-08