In AnQi CMS, effectively managing and displaying website content is the key to successful operation.The sidebar as an important navigation area of the website often needs to display a clear classification structure to guide users to quickly find the information they need.categoryListTagsall=trueWhen in doubt about the parameters, this is exactly the topic we will delve into today.
Category Management in Anqi CMS: The Skeleton of Content
In AutoCMS, categories are the framework for organizing website content.Whether it is an article, product, or other custom content model, they are all classified and managed through categories.This structure not only helps to present the 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 Classification
To flexibly display these categories on the website front-end, Anqi CMS provides a powerfulcategoryListTemplate tag.This tag allows you to call the category list under a specific model and parent based on different needs.Its syntax is concise and expressive, similar to the Django template engine, allowing template developers to get started easily.
In most cases, we might use it like thiscategoryListLabel to get the top-level categories under a certain 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"The article model is specified,parentId="0"Then it means to get only the top-level categories without any parent category.But sometimes, our needs are not just top-level categories, but to get all categories, regardless of their level or which parent category they belong to.all=trueThe parameter becomes particularly important.
all=trueUnlock to display all categories
all=trueParameter iscategoryListA powerful modifier of the label, the core function of which isignoreparentIdThe limit of properties, get all classification lists that meet the conditions.
Imagine if your website has a complex classification hierarchy, such as:
- News Center
- Domestic News
- International News
- Product Display
- Electronics
- Mobile Phone
- Computer
- Home Goods
- Electronics
If you useparentId="0"Only "auto" can be obtained for the top-level categories of "News Center" and "Product Display". If you wish to display a flattened list of all categories in the sidebar, or need to retrieve all categories for custom hierarchical construction in the template,all=trueIt is your best choice.
Whenall=trueWhen set:
- It will get all categories under the specified model (if
moduleIdalso set)This means that, regardless of the category being top-level, second-level, or third-level, it will be included in the returned results.categoriesthe variable. - If not specified
moduleId, it will attempt to retrieve all categories under all content models.In this case, you will get a massive list containing all the categories of the website.
Sidebar Practice: Display all categories
Now, let's look at a real example of how to use 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, which you can customize as you like.moduleId="1"Here we explicitly specify that only to be obtained文章模型All categories below (typically the article model ID is 1, if your system settings are different, please adjust accordingly). If you want to get all categories under all content models, you can omit it.moduleIdParameter.all=trueThis parameter tells the CMS that all categories matching the conditions should be returned, not just the top-level categories or the subcategories of the current category.
{% for item in allCategories %}:“We traverse”}]allCategoriesEach category obtained from the variable.{{ item.Link }}This will output the link to 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 information “No categories available.” to avoid an empty page.
Through this code, your sidebar will display a list of all article categories. Each category will be a clickable link, directly jumping to the content list page under the category.
Deep understandingall=trueThe combination usage
With
moduleIdPrecise control of combination:As shown in practical examples,all=trueWithmoduleIdParameters combined use can help you accurately obtain all categories under a specific content model.This is very useful when the content structure of your website is complex and requires the display of different model classifications separately, for example, showing all article categories in one sidebar and all product categories in another sidebar.For
parentIdThe 'covering' effect:Whenall=trueenabled,parentIdThe parameter will no longer be applicable toInitial data acquisitionIt is effective.It will pull all (or all under the specified model) categories into the template variables at once.item.ParentIdManually construct the level relationship of properties, or call it again in a loopcategoryList(but not usingall=true) to get the subcategories. But for just showing the list of all categories,all=trueis the most direct and efficient way.Consideration of potential performance:Although
all=trueProvided great convenience, but if your website has thousands of categories, fetching all categories at once might 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 Anqi CMS can usually handle it easily.Even so, when designing large-scale websites, it is still recommended to weigh the convenience and performance according to actual needs.
###