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.

###