How to dynamically display the name, description, and link of content categories in a template?

Calendar 81

In website operation, clearly displaying content categories not only helps users quickly find the information they need, but is also a key factor in improving website usability and search engine optimization (SEO).AnQiCMS (AnQiCMS) with its flexible template engine allows us to easily dynamically retrieve and display the names, descriptions, and links of various content categories in website templates.Today, let's delve deeper into how to achieve this goal in templates, making your website content more vivid and efficient.

Understanding content category data in Anqi CMS

In the Anqi CMS backend management system, we have created corresponding categories for each article or product.These categories are not just simple names; they also contain rich metadata: such as user-friendly category names (Title), detailed category descriptions (Description), keywords and aliases for SEO optimization, as well as system-generated access links (Link).All this information is stored in the database and can be dynamically extracted and displayed on the website front-end through the template tag mechanism of AnQiCMS.

AnQiCMS template system adopts syntax similar to Django, using double curly braces{{变量}}to output variable values, as well as{% 标签 %}A structure is used to call functions or perform logical control. This allows even operators without a deep programming background to achieve highly customized content display by learning simple tag usage.

core tags: dynamically retrieve single category information

To display the name, description, and link of a specific category in a template, the most commonly used tool iscategoryDetaila tag. This tag is specifically used to retrieve the details of a single content category.

Basic usage

When you are on the category list page (for examplecategory/list.htmlorarticle/list-{分类ID}.htmlUsed incategoryDetailWhen labeling, it will intelligently identify the classification ID of the current page and automatically extract the detailed information of the classification. You just need to specify the field name you need to retrieve:

{# 动态显示当前分类的名称 #}
<h1>{{% categoryDetail with name="Title" %}}</h1>

{# 动态显示当前分类的描述,注意使用 |safe 过滤器以解析HTML内容 #}
<div class="category-description">{{% categoryDetail with name="Description" %}|safe}}</div>

{# 动态显示当前分类的链接 #}
<a href="{{% categoryDetail with name="Link" %}}">查看更多</a>

here,name="Title"The category name will be output,name="Description"And the category description will be output,name="Link"The link to the category will be output. It should be noted that if your category description may contain HTML rich text content (such as bold, images, etc.), then in the outputDescriptionEnsure that it is added when the field|safeFilter. This is to inform the template engine that this content is safe and does not need to be HTML escaped, so that it can render rich text correctly.

Get information for a specified category

If you want to display information about a specific category on any page of the website (such as the homepage, article detail page, or sidebar), instead of the current page's category, you can do so byidortokenParameters to specify the category clearly.

Suppose you want to display the ID of10The name and link of the category:

{# 获取ID为10的分类名称 #}
<p>我们的推荐分类:{{% categoryDetail with name="Title" id="10" %}}</p>

{# 获取ID为10的分类链接 #}
<a href="{{% categoryDetail with name="Link" id="10" %}}">进入推荐分类</a>

Similarly, if you know the URL alias of the category (for exampleabout-us), you can also usetokenparameters:

{# 通过URL别名获取分类名称 #}
<p>关于我们分类名称:{{% categoryDetail with name="Title" token="about-us" %}}</p>

Build category list: loop to display multiple categories

In many cases, we need to display a category list, such as the website navigation menu, the subcategories in the sidebar, or the category block on the homepage. At this point,categoryListthe label comes into play.

categoryListTags are usually withforCombine loops to iterate and display multiple categories.

{# 获取所有顶级分类(moduleId="1"代表文章模型,parentId="0"代表顶级分类) #}
<ul>
    {{% categoryList categories with moduleId="1" parentId="0" %}}
        {{% for item in categories %}}
            <li>
                <a href="{{ item.Link }}">{{ item.Title }}</a>
                <p>{{ item.Description|truncatechars:50 }}</p> {# 截取描述前50个字符 #}
            </li>
        {{% endfor %}}
    {{% endcategoryList %}}
</ul>

In this example, we define a macro namedcategoriesvariable to receive the top-level categories under all article models.forthe loop,itemrepresented by each category object currently being cycled through, we canitem.Link/item.Titleanditem.Descriptionto dynamically display this information. Here, we also used|truncatechars:50A filter to limit the display length of descriptions to avoid overly long text affecting the layout.

Display multi-level classification

Of Security CMScategoryListTags also support displaying multi-level categories through nested loops. This is very useful when building complex navigation menus or hierarchical structures.

Suppose you want to display the top-level categories under the article model, as well as their respective second-level subcategories:

<nav>
    <ul>
        {{% categoryList topCategories with moduleId="1" parentId="0" %}}
            {{% for topCat in topCategories %}}
                <li>
                    <a href="{{ topCat.Link }}">{{ topCat.Title }}</a>
                    {# 判断是否有子分类,然后进行嵌套循环 #}
                    {{% if topCat.HasChildren %}}
                        <ul class="sub-category">
                            {{% categoryList subCategories with parentId=topCat.Id %}}
                                {{% for subCat in subCategories %}}
                                    <li><a href="{{ subCat.Link }}">{{ subCat.Title }}</a></li>
                                {{% endfor %}}
                            {{% endcategoryList %}}
                        </ul>
                    {{% endif %}}
                </li>
            {{% endfor %}}
        {{% endcategoryList %}}
    </ul>
</nav>

In the code above,topCat.HasChildrenIt is a very useful boolean value that tells us whether the current category has subordinate subcategories, thus deciding whether to render nested onesulList. In this way, you can build a classification hierarchy of any depth.

Practical application scenarios

  1. Website main navigation menu:UtilizecategoryListLabel, you can easily build a primary navigation bar that automatically updates according to the background categories.When the backend adds or modifies categories, the front-end navigation will automatically synchronize the update without the need to manually modify the code.
  2. Classification page top information:Inarticle/list.htmlorproduct/list.htmlUse in the classification list template,categoryDetailTag dynamically displays the name of the current classification as,<h1>Title, and place the category description below the title, which is very helpful for page SEO and user understanding.
  3. Sidebar category tree: CombinecategoryListofparentIdParameter, you can create a dynamic sidebar category tree, displaying subcategories under the current category, or all top-level categories and some of their subcategories, making it convenient for users to switch between different categories quickly.
  4. Home content block:On the homepage, you can usecategoryListTags to display several core categories in a loop, and through each categoryarchiveListTag to further display the latest articles or recommended products under this category, forming a content aggregation block.

Points to note

  • HTML content escaping:Again emphasize, when you output fields that may contain HTML tags (such asDescription/ContentPlease use this when}|safeFilters to ensure that HTML content is rendered correctly rather than being escaped as plain text.
  • **fields**

Related articles

How to retrieve and display custom parameters of content models such as articles, products, etc. in a template?

In Anqi CMS, when managing website content, we often encounter the need to display specific information in addition to standard fields such as title and text.For example, when operating an e-commerce website, in addition to the name and description of the product, it may also be necessary to display 'material', 'origin', 'warranty period', and so on;Or you may be running an information blog, where articles may need to display 'author source', 'publisher' and other information.The flexible content model design of AnQi CMS is to meet this diverse content display requirement.Today, let's delve deeper into the topic

2025-11-08

How does Anqi CMS handle images in content to optimize loading speed and display quality (WebP, compression)?

In the content of the website, images play a crucial role, they can enrich the visual effects and convey complex information.However, unoptimized images are often the culprit for slow website loading and poor user experience.As content operators, we fully understand the value of image optimization in improving website performance and user retention.AnQiCMS provides a series of powerful and intelligent functions in image processing, aiming to help us easily achieve a balance between image loading speed and display quality.

2025-11-08

How to embed and display mathematical formulas or flowcharts from the Markdown editor in the content?

In modern content operation, the professionalism and readability of the content are equally important.Especially when our website involves fields such as science, education, engineering, and so on, how to clearly and beautifully present complex mathematical formulas or logically rigorous flowcharts in articles often becomes the key to improving user experience.AnQi CMS understands this need, integrating a powerful Markdown editor and combining it with simple frontend configuration, allowing your website to easily present these professional contents.### First Step

2025-11-08

How to display system-level configurations such as the website logo, filing number, copyright information, etc.

The brand identity, legal statements, and core contact information, such as Logo, filing number, and copyright information, are an indispensable part of the professional image of a website.They not only convey trust and standardization to visitors, but are also important aspects of search engine optimization and legal compliance.In Anqi CMS, managing and displaying these system-level configurations becomes extremely intuitive and efficient. ### Backend Settings: The "Control Center" of Website Information AnQi CMS concentrates these core website information in the "Global Settings" of the backend, making management clear at a glance.

2025-11-08

How does AnQi CMS display detailed content of a single page (such as About Us, Contact Information)?

In website operation, pages such as "About Us", "Contact Information", and "Privacy Policy" play a crucial role. They are the basis for visitors to show corporate information and establish trust.AnQiCMS as an efficient content management system provides a very flexible and intuitive way to create and display the detailed content of these single pages.

2025-11-08

How to display the list of article tags (Tag) and the related articles under each Tag?

AnQi CMS provides very flexible and powerful tools for content organization, among which tags (Tag) are a very intuitive and efficient tool.By using labels reasonably, not only can it help us better manage website content, but also significantly improve user experience and search engine optimization (SEO) effects.Next, let's take a look at how to display the list of article tags in Anqi CMS, as well as the related articles under each tag.### First step: Manage and create tags in the background Before using tags, make sure that our website content has been properly tagged

2025-11-08

How to implement pagination control for content list display in Anqi CMS?

In website operation, how to efficiently display and manage a large amount of content while ensuring a smooth browsing experience is a challenge that every webmaster needs to face.AnQiCMS provides an intuitive and powerful control capability for pagination display of content lists, allowing you to easily achieve an orderly presentation and efficient navigation of content.To implement the pagination display of content lists, we mainly use two core tags in Anqi CMS templates: content list tags (such as `archiveList`, `tagDataList`

2025-11-08

How to display custom contact information (such as phone, address, social media) in the template?

The contact information on the website is the key bridge between you and the visitors for establishing communication and trust.Whether seeking services, consulting products, or simply learning more information, clearly and accurately displaying contact information is crucial for improving user experience.AnQi CMS is an efficient and convenient content management system that provides an intuitive backend management interface and flexible template calling function, allowing you to easily manage these information and seamlessly present them on every corner of the website.This article will guide you in setting up these key contact information in Anqi CMS

2025-11-08