AnQiCMS (AnQiCMS) is an efficient and flexible content management system with powerful content model functions that many operators and developers love to use.When we operate websites on a daily basis, we often need to conduct detailed statistics and displays of content.For example, under a specific content model, we may want to know how many categories there are in order to display them on the homepage, sidebar, or statistics page.
Today, let's delve deeply into a question that everyone is concerned about: 'How do we get the total number of categories under the content model?'moduleDetailDoes the tag directly support this feature?
Understand the content model of AnQiCMS andmoduleDetailTag
In AnQiCMS,Content modelIt is a core concept that allows us to customize content structure based on business requirements.Whether it is articles, products, news, or cases, they can all be managed through the creation of different content models to manage their unique fields and properties.This provides a solid foundation for flexible organization and display of content.For example, you can create a model for "article" and another for "product", each with its own classification system.
To obtain detailed information about these content models, AnQiCMS provides powerful features,moduleDetailTag. This tag is designed to help us obtain metadata for a specific content model, such as the model ID(Id),Title(Title), internal name(Name), link (Link), introduction(Description), and the corresponding database table name(TableName) and so on. It allows us to easily refer to the model name in templates or build links to model pages, greatly simplifying template development work.
However, when we look at it carefully,moduleDetailWhen you support the parameter list of the label, you will find that it mainly focuses on the properties of the model itself. Unfortunately,moduleDetailTagit does not provide directlyA parameter to obtain the total number of categories under the content model. Its original intention is to obtain detailed information of a single model, rather than aggregated statistical data.
So, sincemoduleDetailTags are not supported directly, so how can we achieve this goal?
How to get the total number of categories under the content model?
AlthoughmoduleDetailThe tag cannot directly complete this task, but the AnQiCMS tag system is complementary and flexible. We can cleverly combine other tags to achieve the purpose. Here,categoryListTagIt came in handy.
categoryListThe tag is used to retrieve the list of categories of articles or products.It can not only list the categories under the specified model but also finely control it through some parameters, such as getting top-level categories, subcategories, or even all categories.
The key is in.categoryListThe parameters are:
moduleId: Specify the content model category we want to query. For example,moduleId="1"means to get the category of the article model.all=trueThis parameter is very important, it tells the system that we want to get all categories under a specified model, not just the child categories under a parent category.
WhencategoryListLabel combinationmoduleIdandall=trueWhen used, it returns an array containing all categories under the model (or what is called a slice). Once we have obtained this category array, we can use the built-in AnQiCMS template engine'slengthFilterCalculate the number of elements in the array to get the total number of categories.
Let's look at a real template code example, assuming we want to get the total number of categories under the article model with ID 1:
{# 假设文章模型的ID是1 #}
{% categoryList allCategories with moduleId="1" all=true %}
<p>文章模型下的分类总数:{{ allCategories|length }}</p>
{% endcategoryList %}
In the code above:
- We use
{% categoryList allCategories with moduleId="1" all=true %}To retrieve the categories under the content model with ID 1 (usually an article model). These category data will be stored inallCategoriesthis variable. allCategoriesNow it is an array containing multiple category objects.- We continue to use
{{ allCategories|length }}of which|lengthIt is a filter that calculatesallCategoriesThe length of the array, which is the total number of categories, and outputs it.
By using this combination method, we can not only accurately obtain the total number of all categories under a specific content model, but we can also make use ofallCategoriesEach category object's specific information in the variable, for further display or logical judgment, such as looping to output each category's name, link, etc.
Why not integrate it directly in?moduleDetail?
You might ask, since it can ultimately be achieved through combining tags, why not add amoduleDetaildirectly incategoryCountsuch field? This actually involves some considerations of system design:
- The Principle of Separation of Concerns:
moduleDetailThe tag's responsibility is to provide metadata for the content model itself.And the acquisition and statistics of the classification list are the responsibilities of the classification management module.Separating the two can make the code structure clearer, each module performs its duties, making it easier to maintain and extend. - Performance optimization: Directly in
moduleDetailthe integrated classification total, meaning each time a call is mademoduleDetailWhen, the system needs to execute an additional query to count the number of categories.For certain scenarios, users may only be concerned with the model name and not the total number of categories.To isolate the statistical logic can avoid unnecessary performance overhead, allowing the system to maintain high efficiency in different scenarios. - Flexibility: Pass
categoryListLabel, we can not only get the total number, but also flexibly filter the categories (such as, only get categories with content, only get categories of a certain level, etc.) before counting. If directly integrated intomoduleDetailThis flexibility will be limited.
Therefore, the design philosophy of AnQiCMS tends to provide atomized, clear-label responsibilities, allowing operators and developers to combine these basic labels to achieve various complex functional requirements, ensuring system performance while enhancing flexibility.
Frequently Asked Questions (FAQ)
Can I count the number of child categories under a specific parent category, rather than all categories under the entire content model?Of course you can.
categoryListTag supportparentIdParameter. If you want to count the number of child categories under the parent category with ID 10, you can use it like this:{% categoryList subCategories with parentId="10" %} <p>ID为10的父分类下的子分类总数:{{ subCategories|length }}</p> {% endcategoryList %}How can I count the number of categories under a specific content model, and only the categories that have published documents?
categoryListThe category object obtained by the tag contains oneArchiveCountField, it indicates how many documents are under this category. You can first get all categories, then iterate through these categories, and only countArchiveCountcategories with more than 0.{% set activeCategoriesCount = 0 %} {% categoryList allCategories with moduleId="1" all=true %} {% for category in allCategories %} {% if category.ArchiveCount > 0 %} {% set activeCategoriesCount = activeCategoriesCount|add:1 %} {% endif %} {% endfor %} <p>文章模型下有文档的分类总数:{{ activeCategoriesCount }}</p> {% endcategoryList %}Does this method of obtaining the total number of categories have a significant impact on website performance?For most small and medium-sized enterprises and self-media sites, this method of going through
categoryListuse to get all categories firstlengthThe way to calculate, usually the performance impact can be ignored.AnQiCMS is developed based on the Go language at its core, and its performance is naturally very excellent, with database queries also optimized.Unless your category count reaches tens of thousands or even hundreds of thousands, and you frequently perform this operation on the same page, there is no need to worry excessively.If you really encounter a performance bottleneck, you can consider caching this type of data on the server, or taking advantage of the more advanced aggregation query features that AnQiCMS may launch in the future.