How do I correctly use the `all=true` parameter of `categoryList` to display all categories in the sidebar?

Calendar 👁️ 59

In Anqi CMS, efficiently managing and displaying website content is the key to successful operation.The sidebar acts as an important navigation area for a website, often requiring a clear classification structure to guide users quickly to the desired information.When you want to display all categories in the sidebar and forcategoryListlabel'sall=trueWhen parameters are confusing, this is exactly the topic we will delve into today.

Category management in Anqi CMS: the skeleton of content.

In Anqi CMS, categories are the framework for organizing website content.Whether it is articles, products, or other custom content models, they are all categorized and managed through classification.This structure not only helps to present website content clearly, but also lays a good foundation for search engine optimization (SEO).Each category can have its own name, description, image, and even support multi-level nesting to form a rich hierarchy.

categoryListExploring tags: a flexible tool for category calls.

To flexibly display these categories on the website front-end, Anqi CMS provides powerfulcategoryListTemplate tag. This tag allows you to call the category list under a specific model and parent according to different needs.Its syntax is concise and expressive, similar to the Django template engine, making it easy for template developers to get started.

In most cases, we might use it like thiscategoryListTag to get the top-level category under a content model:

{% categoryList categories with moduleId="1" parentId="0" %}
    {# 循环输出顶级文章分类 #}
    {% for item in categories %}
        <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
    {% endfor %}
{% endcategoryList %}

HeremoduleId="1"指定的文章模型,parentId="0"This indicates that only the top-level categories without parent categories are retrieved. But sometimes, our needs are not just the top-level categories, but to retrieve all categories, regardless of their level or parent category. At this point,all=trueParameters are particularly important.

all=true: Unlock the display of all categories

all=trueThe parameter iscategoryListA powerful modifier of the tag, its core function isto ignoreparentIdAttribute limitation, get the list of all categories that meet the conditions.

Imagine if your website has a complex category hierarchy, for example:

  • News Center
    • Domestic news
    • International news
  • Product Display
    • Electronics
      • Mobile phone
      • computer
    • home supplies

If you useparentId="0"Only the top-level categories "News Center" and "Product Display" can be accessed. But if you want to display a flat list of all categories in the sidebar, or need to get all categories for custom hierarchical construction in the template, thenall=trueIt is your best choice.

Whenall=trueWhen set:

  1. It will get all categories under the specified model (ifmoduleIdalso set)This means that, regardless of whether the category is top-level, second-level, or third-level, it will be included in the return.categoriesthe variable.
  2. If not specifiedmoduleIdIt will try to get all categories under all content models.In this case, you will get a large list containing all the categories of the website.

Sidebar实战:Display all categories

Now, let's look at a real example to see how to use it in the sidebarall=trueShow all categories. Suppose we want to display all article categories in the sidebar, forming a simple, clickable list.

You can find the sidebar section in the template file (for examplepartial/sidebar.htmlOr you can add the following code to your custom sidebar template:

<div class="sidebar-categories">
    <h3>所有分类</h3>
    <ul>
        {% categoryList allCategories with moduleId="1" all=true %}
            {% for item in allCategories %}
                <li class="category-item-{{ item.Id }}">
                    <a href="{{ item.Link }}" title="{{ item.Title }}">
                        {{ item.Title }}
                    </a>
                </li>
            {% empty %}
                <li>暂无分类。</li>
            {% endfor %}
        {% endcategoryList %}
    </ul>
</div>

Code analysis:

  • {% categoryList allCategories with moduleId="1" all=true %}: This is the core part.
    • allCategories: We have specified a variable name for the returned category list, you can customize it as you like.
    • moduleId="1"Here we explicitly specify that we only obtain文章模型All categories below (usually the article model ID is 1, if your system settings are different, please adjust accordingly). You can omit if you want to get all categories under all content modelsmoduleIdParameter.
    • all=true: This parameter tells AnQi CMS to return all categories that meet the conditions, not just the top-level categories or the subcategories of the current category.
  • {% for item in allCategories %}: We traverseallCategoriesEach category obtained in the variable.
  • {{ item.Link }}This will output the access link of the category.
  • {{ item.Title }}This will output the name of the category.
  • {% empty %}This is a very practical feature, whenallCategoriesWhen the list is empty, it will display the message "No categories available." to avoid a blank page.

Through this code, your sidebar will display a list of all article categories. Each category will be a clickable link that directly jumps to the content list page under the category.

Deep understandingall=truethe combination use

  • withmoduleIdexact control combination:As shown in the practical example,all=truewithmoduleIdWhen parameters are combined, it can help you accurately obtain all categories under a specific content model.This is very useful when the structure of your website's content is complex and you need to display different model categories separately, for example, showing all article categories in one sidebar and all product categories in another sidebar.

  • YesparentIdthe 'overlay' effect:Whenall=trueenabled,parentIdthe parameter will no longer beInitial data acquisitionIt takes effect. It will pull all (or all categories under a specified model) into the template variables at once.If you need to build a multi-level nested sidebar on this basis, you will need to write more complex template logic to according toitem.ParentIdManually construct the hierarchical relationship of properties, or call it again in a loopcategoryList(but not usingall=true) to get the subcategories. But for just displaying a list of all categories,all=trueThe most direct and efficient way.

  • Potential performance considerations:Althoughall=trueProvided great convenience, but if your website has tens of thousands of categories, fetching all categories at once may have a slight impact on performance.However, for most small and medium-sized enterprise websites, this impact is negligible, and the efficient Go language backend of Anq CMS can usually handle it easily.Even so, when designing large-scale websites, it is still recommended to weigh the convenience and performance based on actual needs.

###

Related articles

Does the `categoryList` tag support limiting the number of categories returned? How to set the `limit` parameter?

As an experienced website operations expert, I know how important it is to have precise control over content display when building and optimizing a website.AnQiCMS as an efficient and flexible content management system, its powerful template tag system provides us with abundant operating space.Today, let's delve into the function of the `categoryList` tag in limiting the number of returned categories, as well as how to巧妙ly use the `limit` parameter.

2025-11-06

How to get the list of all direct subcategories of the current page category?

As an experienced website operations expert, I am well aware that how to efficiently organize and display the classification structure is crucial when managing a content-rich website.AnQiCMS (AnQiCMS) with its powerful template tag system provides us with great flexibility, making it easy to achieve various complex classification display needs.Today, let's delve into a common and practical scenario: how to obtain and display the list of all direct subcategories under the current page category.

2025-11-06

What is the specific function and application scenario when the `parentId` parameter of `categoryList` is set to "0"?

AnQiCMS (AnQiCMS) is an efficient and flexible content management system, whose powerful template tag system is the key to content operators achieving personalized display.Among many practical tags, the `categoryList` tag is undoubtedly an important tool for building the framework of a website.Today, let's delve into the `categoryList` tag and its seemingly simple yet extraordinary parameter `parentId="0"`. What specific role and application scenarios does it have?

2025-11-06

How to implement nested display of multi-level categories in AnQiCMS templates, such as a three-level menu?

In modern web design, a clear and intuitive navigation system not only greatly enhances user experience but is also an indispensable part of search engine optimization (SEO).A multi-level classification menu, especially a three-level nested menu, can effectively organize a large amount of content, allowing users to quickly locate the information they need, and also helps search engines understand the hierarchical structure of the website.As an experienced website operations expert, I am well aware that implementing such a feature in AnQiCMS (AnQi CMS) is both simple and efficient.

2025-11-06

What do `categoryList` loop's `item.Title` and `item.Link` represent, and how to output?

As an experienced website operations expert, I know that how to efficiently and accurately extract and display data in a content management system is the cornerstone of website success.AnQiCMS is an enterprise-level content management system based on the Go language, and its simple and efficient template engine is very appealing to me.Today, let's delve deeply into what `categoryList` loop's `item.Title` and `item.Link` represent in AnQiCMS template development, and how we can elegantly present them in the template.### Reveal

2025-11-06

How can you determine if a category has a subcategory? How can the `item.HasChildren` field be applied to conditional judgments?

As an experienced website operations expert, I deeply understand the importance of efficiently using template tags in the daily application and content strategy formulation of AnQiCMS (AnQiCMS).Today, let's delve deeply into a very practical topic in website structure construction: how to determine whether a category (Category) contains subcategories, and how to cleverly use the `item.HasChildren` field in templates to achieve intelligent content display.

2025-11-06

Can the `categoryList` tag display the number of documents under each category? How can `item.ArchiveCount` be retrieved and displayed?

As an experienced website operation expert, I know that how to efficiently and intuitively display content data in a content management system is the key to improving user experience and content operation efficiency.AnQiCMS (AnQiCMS) offers us great flexibility with its simple and efficient architecture.Today, let's delve deeply into a question that both operators and template developers are very concerned about: Can the `categoryList` tag display the number of documents under each category?How to get and display `item.ArchiveCount`?” --- ##

2025-11-06

How to display a category thumbnail (`item.Thumb`) or Banner image (`item.Logo`) next to the category list?

As an experienced website operation expert, I am well aware of the importance of the visual presentation of website content in terms of user experience and information delivery.In a powerful and flexible system like AnQiCMS, we have multiple ways to achieve fine-grained content control, especially in key navigation areas like category lists. How to effectively display category thumbnails or Banner images is an important aspect to enhance the professionalism and attractiveness of a website.Today, let us delve into how to cleverly integrate these visual elements into the category list in Anqi CMS.--- ###

2025-11-06