As an experienced website operations expert, I am well aware of how to cleverly utilize the existing functions of the CMS system without touching the core code, which is crucial for maintaining system stability and reducing upgrade costs. AnQiCMS, with its flexible template engine and rich tag system, provides us with powerful tools for in-depth customization at the content operations level, among whichcategoryListThe label is a point that we often need to extend functionality.

In the Anqi CMS,categoryListcategoryListI will give a detailed explanation of several effective strategies for the additional features of labels.

Deep understandingcategoryListThe basic capabilities of the label

First, let's take a look back atcategoryListThe power of tags. It allows us tomoduleIdspecify content models (such as article models, product models), byparentIdfiltering subcategories under specific parent categories, and even byall=trueGet all categories. Each category item (item) comes with rich properties, such asId(Category ID),Title(category title),Link(category link),Description(Classification description),Logo(Classification thumbnail large image),Thumb(Category thumbnail),HasChildrenWhether there is a sub-category andArchiveCountsuch as the number of documents under the category. These built-in properties are the cornerstone of our functional expansion.

However, when built-in properties cannot meet our personalized needs, we need to seek additional extension methods.It is fortunate that AnQiCMS has left us enough room to maneuver, allowing us to enhance functionality at the template level without touching the core code of the Go language.

The extension philosophy of AnQi CMS: do not touch the core code.

AnQiCMS advocates a "decoupling" development philosophy, separating the core business logic from the front-end display logic.This means that even when we need to customize complex features, we can often find solutions at levels such as template files, custom fields, auxiliary tags, and filters.This approach not only ensures the smoothness of system upgrades, but also greatly reduces the threshold and risk of secondary development.

Practice Exercise: ExtensioncategoryListSeveral Practical Methods

Method 1: Use Custom Fields Wisely, Add Dimensions to Classification Information

In many cases, we want to add some non-standard attributes to each category, such as a unique icon, a special promotional slogan, or additional images related to the category.The AnQiCMS content model management feature allows us to customize fields for categories, which is the first step to meet this requirement.

Operation Steps:

  1. Add custom fields to the background classification model:Navigate to "Content Management" -> "Content Model", select the model (e.g., "Article Model") to which you want to extend the category.After entering the model editing interface, you can find the "Content Model Custom Fields" area.icon),“Background Image”bannerImage)etc.

    • Field type selection:Select the appropriate field type based on your needs, for example, 'Single line text' is used to store the URL of the icon or CSS class name, and the 'Image' type is used to store the background image.
    • Invoke field naming:Ensure that you set an easily identifiable 'invoke field' name for your custom field, for example:category_icon/category_banner.
  2. Fill in the custom field content on the category editing page:When creating or editing a specific category, you will see the custom field you just added. Enter the corresponding content for each category.

Call custom fields in the template:

Once the custom fields are set up, we can accesscategoryListin the loop, directly throughitem.你的自定义字段名to access these new properties.

{% categoryList categories with moduleId="1" parentId="0" %}
    {% for item in categories %}
        <li class="category-item">
            <a href="{{ item.Link }}" title="{{ item.Title }}">
                {# 调用自定义字段:分类图标 #}
                {% if item.category_icon %}
                    <i class="{{ item.category_icon }}"></i>
                {% endif %}
                <h3>{{ item.Title }}</h3>
                <p>{{ item.Description|truncatechars:50 }}</p>
                {# 调用自定义字段:背景图片 #}
                {% if item.category_banner %}
                    <div class="category-banner" style="background-image: url('{{ item.category_banner }}');"></div>
                {% endif %}
            </a>
            {# 还可以根据HasChildren判断是否显示子分类,并调用子分类列表 #}
            {% if item.HasChildren %}
                <ul class="sub-categories">
                    {% categoryList subCats with parentId=item.Id %}
                        {% for subItem in subCats %}
                            <li><a href="{{ subItem.Link }}">{{ subItem.Title }}</a></li>
                        {% endfor %}
                    {% endcategoryList %}
                </ul>
            {% endif %}
        </li>
    {% empty %}
        <p>暂无分类信息。</p>
    {% endfor %}
{% endcategoryList %}

In this way, we have added unique visual elements and additional information to each category without touching any Go language code in AnQiCMS.

Method two: Use template logic and filters to process list data as needed

AnQiCMS template engine supports conditional judgment (if), loop traversal (for), and rich filters (filtersThese tools are enough for us to perform complex data processing and display logic at the template level.

1. Conditional rendering and filtering:We may need to decide how to render it based on certain properties of the category. For example, only display categories with documents, or apply special styling to categories with specific IDs.

{% categoryList categories with moduleId="1" parentId="0" %}
    {% for item in categories %}
        {# 只显示文档数量大于0的分类 #}
        {% if item.ArchiveCount > 0 %}
            <li class="category-item {% if item.Id == 10 %}highlight{% endif %}">
                <a href="{{ item.Link }}">
                    <h4>{{ item.Title|upper }}</h4> {# 将分类标题转换为大写 #}
                    <p>{{ item.Description|truncatechars:80 }}</p> {# 截取描述文字 #}
                    <span>包含文档:{{ item.ArchiveCount }} 篇</span>
                </a>
            </li>
        {% endif %}
    {% empty %}
        <p>暂无分类信息。</p>
    {% endfor %}
{% endcategoryList %}

Here we useditem.ArchiveCountPerform a conditional judgment and combineitem.IdAdd specific styles. Also, utilizeupperThe filter converts the title to uppercase,truncatecharsThe filter truncates the description content, making it more concise.

2. Data Combination and联动:IncategoryListIn the loop, we can nest other tags to achieve interlinking between different data. For example, show the latest few documents under each category.

[en]`twig {% categoryList categories with moduleId=“1” parentId=“0” %}

{% for item in categories %}
    <div class="category-block">
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <ul class="latest-archives">
            {# 在每个分类下,调用该分类最新的5篇文档 #}
            {% archiveList archives with type="list" categoryId