Hello! As an experienced website operations expert, I am very happy to share with you how to use the template in AnQiCMS (AnQiCMS) by cleverly utilizingmoduleDetailTag to retrieve the current content model ID and build a dynamic and accurate classification navigation based on it.AnQi CMS provides us content operators with great convenience with its efficient and flexible content model, and a deep understanding of its template tags is the key to bringing these advantages into play.


Enterprise CMS Template Development实战:Skillful UsemoduleDetailGet the current content model ID, build an intelligent classification navigation

In a content management system, clear and intuitive category navigation is the core of user experience, as well as the foundation of content organization and SEO optimization.AnQi CMS, with its high-performance architecture based on the Go language and flexible content model design, provides a solid foundation for us to build various websites.When faced with a website that has multiple content models (such as articles, products, cases, etc.), how to make the navigation bar intelligently display its exclusive categories according to the current page's content model has become a common template development requirement.moduleDetailThe place where tags can fully show their talents.

Insight into AnQi CMS content model: Everything starts with structure

In AnQi CMS, the content model (Content Model) plays the role of the content skeleton.It allows us to define unique fields and structures for different types of content.For example, you may have an "article modelEach category belongs to a specific content model, which means a 'news category' will belong to the 'article model', and a 'phone category' will belong to the 'product model'.Understanding this hierarchical relationship is the first step in building intelligent navigation.

moduleDetailTag: Get the 'ID number' of the current content model

In order to build a classification navigation that matches the current content model, we first need to know which content model the current page is located in. At this time,moduleDetailThe label comes in handy. Its main function is to obtain the detailed information of the content model, including the model ID, title, link, table name, etc.

In the template,moduleDetailTags usually automatically identify the content model of the current page. We most commonly use itsIdfield, which is equivalent to the unique 'ID card number' of the content model.

For example, when you are on an article detail page or an article list page, no additional specification is requiredidortokenparameter,moduleDetailit can automatically obtain information about the current model. We can retrieve the ID of the current content model in this way:

{# 假设我们想获取当前内容模型的ID #}
{% moduleDetail currentModuleId with name="Id" %}

In this concise code,currentModuleIdThis variable will store the numeric ID of the current content model. This ID is the key to connecting the content model with the category list.

From Model ID to Category Navigation: Actual Operation Steps

Obtained the ID of the current content model (i.e.currentModuleIdAfter that, we can use it as a bridge to call the category list associated with the model. Anqi CMS providescategoryListLabel, used specifically to retrieve category data. This label has a very important parametermoduleIdIt allows us to specify which content model's categories we want to retrieve.

At the same time, in order to build the top-level category navigation, we usually want to only display the top-level (first-level) categories without any parent categories.categoryListlabel'sparentId="0"The parameters can help us achieve this goal.

Below, let's build a code example to dynamically display a category navigation based on the current content model:

<nav class="category-navigation">
    <ul>
        {# 首先,获取当前页面的内容模型ID #}
        {% moduleDetail currentModuleId with name="Id" %}

        {# 接着,使用这个模型ID和parentId="0"来获取该模型下的所有顶级分类 #}
        {% categoryList categories with moduleId=currentModuleId parentId="0" %}
            {% for item in categories %}
            <li {% if item.IsCurrent %}class="active"{% endif %}>
                <a href="{{ item.Link }}" title="{{ item.Title }}">
                    {{ item.Title }}
                </a>
                {# 如果需要展示二级分类,可以在这里嵌套另一个categoryList标签 #}
                {% if item.HasChildren %}
                    <ul class="sub-categories">
                        {% categoryList subCategories with parentId=item.Id %}
                            {% for subItem in subCategories %}
                            <li {% if subItem.IsCurrent %}class="active"{% endif %}>
                                <a href="{{ subItem.Link }}" title="{{ subItem.Title }}">
                                    {{ subItem.Title }}
                                </a>
                            </li>
                            {% endfor %}
                        {% endcategoryList %}
                    </ul>
                {% endif %}
            </li>
            {% empty %}
            {# 如果当前模型下没有顶级分类,则显示提示信息 #}
            <li>当前模型暂无可用分类。</li>
            {% endfor %}
        {% endcategoryList %}
    </ul>
</nav>

In this code, we first usemoduleDetailSet the current content model toIdAssign tocurrentModuleIdThen, incategoryListIn the tag, we setmoduleId=currentModuleIdTo limit the scope of the categories, and throughparentId="0"Make sure to only retrieve the top-level categories.forLoop through these categories,item.Linkanditem.TitleUsed separately to generate category links and display names.

To enhance user experience, we also utilizeditem.IsCurrentProperty. When a category is the category of the current page,IsCurrentwill returntruewe can addactivea class name to highlight it. In addition, by judgingitem.HasChildrenwe can further nest.categoryListTags to display multi-level classification, making navigation more flexible and complete.{% empty %}Tags elegantly handle the display situation when there are no categories under a certain content model.

Optimize and advance: Details for improving user experience

Building dynamic navigation is not just about technical implementation, but also about focusing on user experience. In addition to the aforementioned basic functions, you can also consider:

  • Display multi-level classification:As shown in the example, usingitem.HasChildrenproperties配合nestedcategoryListtags, you can easily achieve two-level or even multi-level dropdown menus or sidebar navigation for classification.
  • Breadcrumbs navigation:CombinebreadcrumbTags, provide clear path guidance for users, and improve the ease of use of the website.
  • Caching strategy:For category data that does not change frequently, consider caching at an appropriate level to reduce database queries and improve page loading speed.

In this way, no matter which article page or product page your users visit, the navigation bar can intelligently adjust, only displaying categories related to the current content model, greatly enhancing the website's intelligence and user-friendliness.

Summary

Of Security CMSmoduleDetailLabels may seem simple, but they are the key starting point for building dynamic, intelligent classification navigation. By obtaining the ID of the current content model and passing it as a parameter tocategoryListLabel, we can easily achieve adaptive classification navigation, thereby providing users with more accurate and smooth browsing experience.This modular, tagged development approach is exactly the strength of Anqi CMS as an enterprise-level content management system.


Frequently Asked Questions (FAQ)

  1. Does the AnQi CMS content model have a 'default category ID' configuration item?The AnQi CMS content model does not have a 'default category ID'.