As an experienced website operations expert, I know how to skillfully 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 (AnQiCMS) relies on its flexible template engine and rich tag system to provide us with a powerful tool for deep customization at the content operation level, among whichcategoryListTags are often a point where we need to extend functionality.
In AnQi CMS,categoryListThe tag is used to retrieve the classification list of the website, which can help us build navigation, display classification entries, or aggregate content under specific categories.However, in actual operation, we may wish for these classification lists to carry more information, or to be displayed in a more intelligent way, rather than simply being simple classification names and links.Next, I will focus on how to expand without modifying the core code of AnQiCMScategoryListTags' additional features, a detailed explanation of several effective strategies.
Deep understandingcategoryListTags' basic capabilities
First, let's reviewcategoryListThe power of tags. It allows us tomoduleIdspecify content models (such as article models, product models), toparentIdfilter child categories under specific parent categories, even toall=trueGet all categories. When iterating over the tag results, each category item (item) comes with rich properties such asId(Category ID),TitleCategory Title,Link(Category link),Description(Category Description),Logo(Category thumbnail large image),Thumb(Category Thumbnail),HasChildren(whether there are subcategories) andArchiveCount(The number of documents under the category) and others. These built-in properties are the foundation for our functional expansion.
However, when built-in properties fail to meet our personalized needs, we need to seek additional extension methods.Fortunately, AnQiCMS has left us ample room for maneuver, allowing us to enhance the functionality at the template level without touching the core code of the Go language.
The extension philosophy of AnQi CMS: not touching the core code
AnQiCMS advocates a "decoupling" development concept, separating the core business logic from the front-end display logic.This means that even if we need to customize complex functions, we can often find solutions at levels such as template files, custom fields, auxiliary tags, and filters.This method not only ensures the smoothness of system upgrades, but also greatly reduces the threshold and risk of secondary development.
Actual combat practice: extensioncategoryListSeveral practical methods
Method one: cleverly use custom fields to 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 catchy slogan, or additional images related to the category.The content model management function of AnQiCMS allows us to customize fields for categories, which is the first step to meet this requirement.
Operation steps:
Add custom fields to the classification model in the background:Navigate to "Content Management" -> "Content Model", select the model you want to expand the category to (for example, "Article Model").After entering the model editing interface, you can find the "content model custom field" area.Here, you can add new fields such as "category icon" (
icon)、“Background Image”(bannerImage) and so on.- Field type selection:Select the appropriate field type based on your needs, for example, 'Single line text' is used to store the URL or CSS class name of the icon, and the 'Image' type is used to store the background image.
- Field naming call:Make sure to set an easily identifiable 'call field' name for your custom field, for example
category_icon/category_banner.
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 the custom field in the template:
Once the custom fields are set up, we can incategoryListin the loop, pass through directlyitem.你的自定义字段名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 add 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)、looping over(for) and a rich set of filters (filters)。These tools are enough to allow 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 some 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 %}
We used hereitem.ArchiveCountPerform conditional judgment and combineitem.IdAdd specific styles. At the same time, useupperThe filter converts the title to uppercase,truncatecharsThe filter truncates the description content, making it more concise.
2. Data Combination and Linkage:IncategoryListIn the loop, we can nest other tags to achieve linkage between different data. For example, under each category, display the latest several documents.
`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