Category detail label `categoryDetail` how to get and display detailed information of a specific category?

In the website content operation of Anqi CMS, we often need to accurately obtain and display detailed information of specific categories. Whether it is to display the name, description, or related pictures and content of the category,categoryDetailTags can provide flexible and powerful support to help us build dynamic and rich classification pages.

categoryDetailCore functions and basic usage of tags

categoryDetailThe tag is used to obtain detailed data of a single category. Its basic structure is concise and clear, usually including the following parts:

{% categoryDetail 变量名称 with name="字段名称" id="分类ID" %}

  • Variable name (optional)You can specify a variable name for the category detail data you get, for examplemyCategoryThis is how you can refer to the data in subsequent template code. If you do not specify a variable name, the tag will directly output the value of the specified field.
  • name="字段名称"This is the most core parameter, used to specify the specific fields of the category data you want to obtain, such as the title of the categoryTitleDescription,Descriptionetc.
  • id="分类ID"(Optional)If you want to get the category information of a specific ID on the website, you can specify its ID here. If the current page itself is a category page (such as a category list page), and you want to get the details of the current category, then you can omit it.idparameter,categoryDetailThe label intelligently retrieves the category information of the current page.
  • token="分类URL别名"(Optional): withidsimilar,tokenThe parameter allows you to specify the category to be retrieved by using the category's URL alias. This is more intuitive in some cases than using the numeric ID.
  • siteId="站点ID"(Optional)For a CMS with multi-site functionality enabled, if you need to retrieve category data from a non-current site, you cansiteIdto specify the target site.

Common fields and their applications

categoryDetailTag throughnameParameters can access many properties of the category, here we list some commonly used fields and their application in the template.

1. Basic information of the category:Id,Title,Link,Description

These are the most basic and commonly used information categories, and the way to obtain them is very direct.

  • Category ID (Id):{% categoryDetail with name="Id" %}Or{{ category.Id }}(可以直接通过 category page)categoryobject access).
  • Category title (Title):{% categoryDetail with name="Title" %}Used to display category names.
  • Category link (Link):{% categoryDetail with name="Link" %}Used to build the jump link of the category.
  • Category description (Description):{% categoryDetail with name="Description" %}Used to display SEO description or brief introduction on the category page.

Example: Display category information at the top of the category page

<div class="category-header">
    <h1>{% categoryDetail with name="Title" %}</h1>
    <p>{% categoryDetail with name="Description" %}</p>
    <a href="{% categoryDetail with name="Link" %}" class="more-link">查看更多</a>
</div>

2. Category content (Content)

If your category has a dedicated introduction content (such as a detailed introduction for the "About Us" category), you can access it throughContentthe field. This field should pay special attention to its rendering method.

Example: Display the detailed content of the category

<div class="category-content">
    {% categoryDetail categoryMainContent with name="Content" %}
    {{ categoryMainContent|safe }}
</div>

Here|safeThe filter is crucial.The template engine of AnQi CMS defaults to escaping HTML content to prevent XSS attacks.|safeFilter. If the category content is written in Markdown, you can add byrender=trueParameters, let the system automatically convert it to HTML:

<div class="category-content">
    {% categoryDetail categoryMainContent with name="Content" render=true %}
    {{ categoryMainContent|safe }}
</div>

3. Category images:Logo,Thumb,Images

Category pages often need to display images, Anqi CMS provides a variety of image fields.

  • Category thumbnail large image (Logo)It is usually the main display image of the category.
  • Category thumbnail (Thumb): A thumbnail compressed or cropped by the system, suitable for use on list pages to improve loading speed.
  • Category Banner group image (Images)If the backend sets multiple Banner images for the category, this field will return an array of image URLs.

Example: Display the category Logo and Banner group images.

<div class="category-banner">
    <img src="{% categoryDetail with name="Logo" %}" alt="{% categoryDetail with name="Title" %}">

    <h3>分类相册</h3>
    {% categoryDetail categoryBanners with name="Images" %}
    {% if categoryBanners %}
        <div class="image-gallery">
            {% for imgUrl in categoryBanners %}
                <img src="{{ imgUrl }}" alt="分类图片 - {% categoryDetail with name="Title" %}">
            {% endfor %}
        </div>
    {% endif %}
</div>

IfImagesIs a set of images, but you only need to display the first image as the cover, you can do it like this:

{% categoryDetail bannerImages with name="Images" %}
{% if bannerImages %}
    {% set firstBanner = bannerImages[0] %} {# 获取数组的第一个元素 #}
    <div class="page-banner" style="background-image: url({{ firstBanner }});">
        {# 其他内容 #}
    </div>
{% endif %}

4. Number of documents in the category (ArchiveCount)

This field is very useful, it can display how many articles or products are included in the current category.

Example: Display the number of articles in the category

<p>本分类共有 {% categoryDetail with name="ArchiveCount" %} 篇文章。</p>

5. Parent category ID (ParentId)

In a multi-level category structure, it is sometimes useful to obtain the parent category ID of the current category, for example, to build breadcrumb navigation or link to the parent category.

Example: Get and display the parent category ID

<p>当前分类的上级分类ID是:{% categoryDetail with name="ParentId" %}</p>

6. Custom Field

The strength of Anqi CMS lies in its flexible content model. If you add custom fields for the category model in the background,categoryDetailtags can also retrieve them.

Example: Retrieve and display all custom fields

<div class="category-custom-fields">
    {% categoryDetail extras with name="Extra" %}
    {% for field in extras %}
        <div>{{ field.Name }}:{{ field.Value }}</div>
    {% endfor %}
</div>

If you know the specific name of the custom field (for exampleauthor/price), you can also access it directly throughnameParameter retrieval:

<p>分类作者:{% categoryDetail with name="author" %}</p>
<p>分类价格区间:{% categoryDetail with name="price" %}</p>

Example of practical application scenarios

Assuming we have a 'News Dynamic' category list page, we want to display the name, description, thumbnail, and know how many articles it has on each news category card.

{# 假设我们正在循环显示顶级新闻分类 #}
{% categoryList newsCategories with moduleId="1" parentId="0" %}
    {% for categoryItem in newsCategories %}
    <div class="news-category-card">
        <a href="{{ categoryItem.Link }}">
            <img src="{% categoryDetail with name="Thumb" id=categoryItem.Id %}" alt="{{ categoryItem.Title }}">
            <h2>{{ categoryItem.Title }}</h2>
            <p>{{ categoryItem.Description|truncatechars:100 }}</p>
            <span>(共 {% categoryDetail with name="ArchiveCount" id=categoryItem.Id %} 篇文章)</span>
        </a>
    </div>
    {% endfor %}
{% endcategoryList %}

In this example, we are incategoryListthe loop, and throughcategoryDetailtags and pass the current loop item'sid(i.e.),categoryItem.Id), to get more detailed thumbnails and article counts for each category.

Summary

categoryDetailTags are an indispensable tool for obtaining and displaying category information in Anqi CMS template development.It provides a series of data interfaces ranging from basic titles, descriptions to complex content, image groups, custom fields, etc.name/id/tokenand parameters, as well asContentfield'srenderprocessing andImagestraversing arrays, we can easily build highly customized, information-rich category pages, greatly enhancing the presentation effect and user experience of website content.


Frequently Asked Questions (FAQ)

How do I get the details of a category on a non-category page?

Answer: On non-category pages (such as the home page, article detail page, or custom page), you can useidortokenSpecify the parameter to clearly specify which category details to retrieve. For example, if you want to display the description of the "About Us" category with ID 5 on the homepage, you can write it like this:{% categoryDetail with name="Description" id="5" %}.

**2. `categoryDetail