How to add custom HTML styles or CSS classes to the category names returned by `categoryList`?

Calendar 👁️ 61

As an experienced website operation expert, I know that the readability and visual aesthetics of website content are crucial for user experience, and the flexible template customization capability is the key to achieving this goal.In AnQiCMS, even for a simple category list, we have multiple ways to add personalized HTML styles or CSS classes to enhance the overall attractiveness and functionality of the website.Today, let's delve deep into how to use forcategoryListThe category name returns a custom style.

AnQi CMS, with its efficiency in Go language and the flexible syntax of Django template engine, provides us with great creative freedom. When we usecategoryListTags are usually used to loop through website categories and get a series of category names.How can we make these category names more prominent visually, or present different styles according to different business logic?The core idea lies in cleverly utilizingcategoryListThe category data exposed by the tag, and through conditional judgment or direct concatenation, add a unique CSS class or inline style to the HTML element where the category name is located.

UnderstandingcategoryListLabel core output

While usingcategoryListWhen labeling, we usually write the template code like this:

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

Here, itemThe variable represents each category object in the current loop. It does not only contain the category'sTitle(Name) andLink(Link), it also includes a series of useful properties such asId(Category ID),IsCurrent(Whether the current category),HasChildren(Do they have subcategories) etc. It is these properties that provide us with a rich 'handle' for customizing styles.

Strategy one: use built-in properties to add general or specific styles.

The most direct and recommended way is to useitemexisting properties in the object, as a container element for category names (usually<a>or the internal<span>). Add CSS classes to it.

  1. Highlight styles are added based on whether it is the current category (IsCurrent)In the website navigation, the category currently occupied by the user often needs to be highlighted.IsCurrentThe property is a boolean value, its value is set when the category is the category of the current pagetrue.

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

    Then, you can define it in your CSS file.activethe style of the class, for example:

    .category-nav .category-link.active {
        color: #007bff; /* 蓝色字体 */
        font-weight: bold; /* 加粗 */
        border-bottom: 2px solid #007bff; /* 底部边框 */
    }
    
  2. Add unique styles based on the category ID (Id)If a particular category needs a completely unique visual style, we can take advantage of its uniqueId.

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

    Thus, the category with ID 5 will havecategory-id-5This class name, you can control it accurately in CSS:

    .category-nav .category-id-5 {
        background-color: #ffc107; /* 黄色背景 */
        color: #333;
        padding: 5px 10px;
        border-radius: 5px;
    }
    
  3. Add style based on whether it contains a subcategory (HasChildren)For categories that contain subcategories, sometimes a visual hint is needed, such as a small arrow icon or a different font style.

    {% categoryList categories with moduleId="1" parentId="0" %}
        <ul class="category-nav">
            {% for item in categories %}
                <li class="nav-item">
                    <a href="{{ item.Link }}" class="category-link {% if item.HasChildren %}has-sub-category{% endif %}">
                        {{ item.Title }}
                        {% if item.HasChildren %}<i class="icon-arrow-down"></i>{% endif %} {# 假设您有icon-arrow-down图标类 #}
                    </a>
                </li>
            {% endfor %}
        </ul>
    {% endcategoryList %}
    

    This can be defined in CSS as:

    .category-nav .category-link.has-sub-category {
        font-style: italic; /* 斜体 */
    }
    .category-nav .has-sub-category .icon-arrow-down {
        margin-left: 5px;
        /* 样式化箭头图标 */
    }
    

Strategy two: Make conditional judgments and style customization based on the content of the category name

When you need to apply styles based on the text content of the category name, you can combine the template with itifLogical judgments and string filters.

For example, if you want the name of the "Company News" category to be displayed in red, and the "Industry Dynamics" to be displayed in green:

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

<ul class="category-nav">
    {% for item in categories %}
        <li class="nav-item">

Related articles

How to avoid performance issues when the `categoryList` tag is used with large amounts of data?

As an experienced website operations expert, I have accumulated rich experience in content management and performance optimization, especially familiar with many functions of AnQiCMS.In daily operations, we often encounter problems such as large amounts of website classification data, leading to slow page loading and increased server pressure.This is often one of the key factors affecting performance, the way the `categoryList` tag is used.

2025-11-06

How to use `categoryList` to build a flat list of all categories without hierarchy display?

As an experienced website operation expert, I know how important it is to flexibly display category lists when building website content structure.AnQiCMS (AnQiCMS) has provided us with great convenience with its powerful template tag system.Today, let's delve into a seemingly simple yet extremely practical feature: how to elegantly build a flat list of all categories using the `categoryList` tag, so that all categories are clearly displayed on your website without any hierarchy.Why do we need a flat category list?

2025-11-06

`categoryList` can exclude certain specific category IDs from displaying in the list?

As an experienced website operations expert, I know how important it is to have fine control over the list content in content management.Especially in the context of increasingly complex website structures and increasingly rich content, how to flexibly display or hide specific content is directly related to user experience and operational efficiency.Today, let's delve into a common but cleverly handled requirement in AnQiCMS (`categoryList` tag) - whether it can exclude certain specific category IDs from displaying in the list.###

2025-11-06

When will the `item.Content` field be used in the `categoryList` loop, can it display rich text content?

HelloAs an experienced website operations expert, I am willing to deeply analyze the application of the `item.Content` field in the `categoryList` loop of AnQiCMS (AnQiCMS), as well as how to elegantly display rich text content.

2025-11-06

What is the corresponding relationship between the `categoryList` tag in template development and the background "document classification" function settings?

As an experienced website operation expert, I deeply understand that the core value of a Content Management System (CMS) lies in the seamless connection between the backend configuration and the frontend display.AnQiCMS (AnQiCMS) excels in this aspect with its efficient architecture based on the Go language and flexible design.Today, let's delve deeply into the development of the Anqi CMS template and the `categoryList` tag, how it closely corresponds to the background "Document Category" function, and how we巧妙运用 this correlation to build powerful website functions.##

2025-11-06

When should the `categoryList` tag be used instead of the `navList` tag to build website navigation?

In the flexible and powerful template system of AnQiCMS, building website navigation is one of the basic tasks of content operation.We often encounter two seemingly similar but distinct tags: `categoryList` and `navList`.As an experienced website operations expert, I am well aware of how to cleverly use these tools according to the specific needs of the website to achieve **user experience and content management efficiency.Today, let's delve deeper into when we should prioritize `categoryList` over

2025-11-06

Does the `categoryList` return category link automatically adapt to the pseudo-static rule set in the background?

As an experienced website operations expert, I know the importance of URL structure for website SEO and user experience.AnQiCMS (AnQiCMS) fully considered this from the beginning, its static and SEO optimization features are one of its core highlights.Today, let's delve into a common concern of operators: When using the AnQiCMS `categoryList` tag to call category links, will these links automatically adapt to the pseudo-static rules set in the background?

2025-11-06

How to display different content in the `categoryList` loop based on the different `moduleId`?

As a senior website operations expert, I fully understand that the flexibility of content presentation is crucial for user experience and operational efficiency.AnQiCMS (AnQiCMS) relies on its powerful template engine and content model mechanism to provide us with a powerful tool for achieving this flexibility.Today, let's delve into a very practical scenario: how to intelligently display distinctive content according to different `moduleId` in the `categoryList` loop.

2025-11-06