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

Calendar 👁️ 65

As an experienced website operation expert, I am well aware that the flexibility of content presentation is crucial for user experience and operational efficiency.AnQiCMS (AnQiCMS) provides a powerful tool for implementing this flexibility with its strong template engine and content model mechanism.Today, let's delve into a very practical scenario: how tocategoryListIn a loop, displaying content with differentmoduleIdsmartly, each with its own characteristics.

Use cleverlymoduleIdEnabling the Anqi CMS category list to be varied in content.

In the template development of AnQi CMS,categoryListThe tag is undoubtedly the core of building navigation and classification blocks. It allows us to easily navigate the website's classification structure.However, the content of the website is often diverse, we may have articles, products, services and other types of information.When these different types of content are categorized under different "content models", how can they be listed in the same category according to the content model they belong to (i.e.,moduleIdThis is a topic of advanced level to display different content layouts and information.

The Anqi CMS template engine supports Django-style syntax, which means we can take advantage of powerful conditional judgment and loop structures to achieve highly customized content display. Among them,moduleIdIt is the key 'bridge' that connects the classification and content model and enables dynamic display.

UnderstandingcategoryListwithmoduleIdcollaboration

First, we know{% categoryList categories %}The label can obtain a series of classification data. Within the loop, each classification object is usually nameditemIt contains various attributes of the classification, such asitem.Id/item.Title/item.Linketc.

Anditem.ModuleIdThis attribute is the main character of today. It records the ID of the content model associated with the current category. According to the default settings of Anqi CMS, the article model is usually correspondingmoduleId=1,The product model corresponds tomoduleId=2. If we customize other content models, they will also have correspondingmoduleId.

Nowitem.ModuleId,we cancategoryListInside the loop, different logic is executed according to different model IDs, thus displaying completely different content layouts.This is like tailoring a 'display mode' for each category content model.

Core technology:ifThe wonderful use of conditional judgment

The core of achieving this goal lies incategoryListUsing a loopifmaking conditional judgments with tags. The basic structure will be like this:

{% categoryList categories with parentId="0" %}
    {% for item in categories %}
        <div class="category-block">
            <h3><a href="{{ item.Link }}">{{item.Title}}</a></h3>
            {% if item.ModuleId == 1 %}
                {# 文章模型的分类,展示文章列表 #}
                <div class="articles-list">
                    <h4>最新文章</h4>
                    <ul>
                    {% archiveList archives with type="list" categoryId=item.Id limit="5" %}
                        {% for archive in archives %}
                            <li>
                                <a href="{{archive.Link}}">{{archive.Title}}</a>
                                <span>({{stampToDate(archive.CreatedTime, "2006-01-02")}} - {{archive.Views}}阅读)</span>
                            </li>
                        {% empty %}
                            <li>暂无文章内容</li>
                        {% endfor %}
                    {% endarchiveList %}
                    </ul>
                </div>
            {% elif item.ModuleId == 2 %}
                {# 产品模型的分类,展示产品图片和名称 #}
                <div class="products-grid">
                    <h4>热门产品</h4>
                    <ul class="product-items">
                    {% archiveList products with type="list" categoryId=item.Id limit="4" %}
                        {% for product in products %}
                            <li>
                                <a href="{{product.Link}}">
                                    <img src="{{product.Thumb}}" alt="{{product.Title}}" loading="lazy">
                                    <p>{{product.Title}}</p>
                                    <span class="price">${{product.Price}}</span>
                                </a>
                            </li>
                        {% empty %}
                            <li>暂无产品内容</li>
                        {% endfor %}
                    {% endarchiveList %}
                    </ul>
                </div>
            {% else %}
                {# 其他模型的分类,或者默认展示方式 #}
                <div class="default-content">
                    <p>这是一个特殊的分类:{{item.Title}},其所属模型ID为:{{item.ModuleId}}。</p>
                    <a href="{{ item.Link }}">查看更多</a>
                </div>
            {% endif %}
        </div>
    {% endfor %}
{% endcategoryList %}

In this code, we first usecategoryListGet the top-level categories. Then, in each categoryitemloop, we go through{% if item.ModuleId == 1 %}and{% elif item.ModuleId == 2 %}To determine which content model the current category belongs to.

  • Ifitem.ModuleIdIs1(Article model), we use this block.archiveListTag, and pass in the current category ID (categoryId=item.IdTo get the latest articles in this category, display them briefly with the title, publication date, and reading volume.
  • Ifitem.ModuleIdIs2(Product model), we use the same one as well.archiveListGet the products under this category, but change the display mode to product thumbnails, names, and prices, which is more in line with the product display requirements.
  • The last{% else %}Part can be used as a general or alternative display method, which can be used to handle unknownsmoduleIdOr as a default display.

The advantage of this approach is that it encapsulates the display logic of different content models in their respective conditional branches, making the code structure clear, easy to maintain and expand. When a new content model needs to be added, simply add a newelifBranch it.

**Practice and Precautions

  1. clearmoduleIdCorresponding relationship:In the background "Content Management"->"Content Model", you can view the detailed information of each content model, including its ID. Be sure to remember or record the commonly usedmoduleIdValues, so that they can be accurately referenced in the template.
  2. nestedarchiveList:Generally, we hope to display the specific document content under the category in the category list, rather than just the description of the category itself. Therefore,categoryListnested in the looparchiveListtags and usecategoryId=item.Idis a common and efficient way to associate the current category
  3. Make good use oflimitparameters:to ensure page loading speed and layout aesthetics, inarchiveLista reasonable setting inlimitParameters, limit the number of documents displayed under each category.
  4. Modular template:WhenifWhen the content block within a branch becomes complex, consider using{% include "partial/article_preview.html" with archives=archives %}In this way, split the display logic of each model type into separate template fragments to improve code readability and reusability.
  5. Performance consideration: Although AnQiCMS is developed based on Go language, it performs well, but too many nested loops or complex queries may still affect page loading.Reasonably design data structures, avoid loading too much unnecessary data in a single request.

In this way, we can be on any page of the website (such as the homepage, sidebar

Related articles

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

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

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

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

As an experienced website operations expert, I am well aware 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 into how to assign custom styles to the category names returned by `categoryList`

2025-11-06

`categoryList`Does it support filtering or displaying based on custom fields by category?

HelloAs an experienced website operations expert, I know the importance of a flexible content management system for efficient operations.AnQiCMS stands out among many CMS systems with its excellent customizability, especially in content modeling and category management, providing great convenience.However, many operators may wonder a question when using the `categoryList` tag: does it support filtering or displaying based on custom fields of categories?

2025-11-06

How to implement the display of the 'sibling categories' of the current category in `categoryList`, instead of the subcategories?

AnQi CMS is an efficient and customizable content management system, playing an important role in website operations.Its template system is powerful and flexible, allowing operators to finely control the display of content according to actual needs.Today, let's delve into a common issue in actual operation that beginners may find confusing: how to cleverly display 'sibling categories of the current category' in the `categoryList` tag, rather than its subcategories.It is of great significance to display the brother category list in terms of content organization and user experience.

2025-11-06

Can the `moduleId` parameter of the `categoryList` tag specify multiple model IDs for a union query?

As an experienced website operations expert, I have a deep understanding of the powerful functions and flexible template system of AnQiCMS (AnQiCMS).In daily content operations, we often encounter the need to aggregate and display different types of content.Today, let's delve into the usage of the `categoryList` tag's `moduleId` parameter, especially the issue of whether it can specify multiple model IDs for a joint query.

2025-11-06

How to quickly view all available fields and values of the `categoryList` returned item object during template debugging?

During the development and debugging of Anqi CMS templates, we often need to gain a deep understanding of the data structure returned by template tags in order to control the display of page content more accurately.Especially like the `categoryList` such list tags, it will loop to output multiple `item` objects, each carrying rich data.How can we quickly and comprehensively view the available fields and their corresponding values in the `item` object returned by `categoryList` when debugging?

2025-11-06