In AnQiCMS template development, acquiring and displaying detailed information of categories is a key part in building rich and user-friendly pages.categoryDetailTags are born for this, they can help you easily retrieve specific category descriptions, Logo, custom fields, and other detailed contents at any location on the page.
categoryDetailThe core function of the tag
categoryDetailThe main function of the label is to obtain detailed data of a single category.Whether you are on the category list page and want to display additional information about the current category, or on the article detail page and need to show the Logo and introduction of the category, or even want to reference specific category detailed data in any corner of the website, this tag can come in handy.
Its basic usage form is usually:{% categoryDetail 变量名称 with name="字段名称" %}.变量名称Is an optional parameter, if you want to assign the obtained data to a variable for subsequent use or for more complex logic processing, you can specify it. If you just want to directly output the value of a field, you can omit it.变量名称.nameThe parameter is the core, it tells the tag which specific field's information you want to get from the category.
How to get detailed information of a specific category
When usingcategoryDetailWhen labeling, you first need to clarify which category of information you want to obtain. AnQiCMS provides several flexible ways to specify the target category:
Automatically identify the current category:If you are on the category details page (such as
article/list-{分类ID}.htmlorproduct/list-{分类ID}.html)categoryDetailtags will intelligently identify the category corresponding to the current page and automatically obtain its information. At this time, you usually do not need to specify the category ID separately.Specify through category ID:You can use
idParameters to precisely specify the category to be retrieved. For example,{% categoryDetail with name="Title" id="10" %}The title of the category with ID 10 will be retrieved.Specify through category URL alias (token):If you prefer to use category URL aliases to identify, you can use
tokenparameters. For example,{% categoryDetail with name="Description" token="about-us" %}It will get the description of the category with the alias “about-us”.Specifying in a multi-site scenario:For AnQiCMS with multiple sites deployed, if you need to get category information for a non-current site, you can use
siteIdto specify the site ID. For example,{% categoryDetail with name="Title" id="10" siteId="2" %}.
name参数可用的字段详解
name参数允许你选择分类的各种详细信息。以下是常用的字段及其使用示例:
Category ID (
Id) 分类标题 (Title) 分类链接 (Link) Category Description (Description):These are the most basic category information, commonly used to build navigation, breadcrumbs, or simple category introductions.<div>当前分类ID:{% categoryDetail with name="Id" %}</div> <h2><a href="{% categoryDetail with name="Link" %}">{% categoryDetail with name="Title" %}</a></h2> <p>{% categoryDetail with name="Description" %}</p>(Classified content (
Content):If your category includes detailed content edited with a rich text editor, you can useContentField acquisition. It should be noted that if the content contains HTML tags, in order to render them correctly on the page rather than displaying the original code, it usually needs to be paired with|safeFilter. If the Markdown editor is enabled on the backend, the content is in Markdown format, you canrender=trueThe parameter allows the label to automatically convert it to HTML.<div> {% categoryDetail categoryContent with name="Content" render=true %} {{ categoryContent|safe }} </div>Category thumbnail large image (
Logo), Category thumbnail (Thumb):These fields are used to obtain the representative image of the category.LogoIt is usually the larger main image,ThumbIt is automatically generated thumbnail.<img src="{% categoryDetail with name="Logo" %}" alt="{% categoryDetail with name="Title" %}" />In practical use, in order to avoid page errors caused by empty image path, it is recommended to make a judgment:
{% set categoryLogo = categoryDetail with name="Logo" %} {% if categoryLogo %} <img src="{{ categoryLogo }}" alt="{% categoryDetail with name="Title" %}" /> {% endif %}Category Banner Set (
Images):If the category supports uploading multiple images as a Banner or slideshow,Imagesthe field will return an array of image URLs. You need to usefora loop to iterate through and display these images.{% categoryDetail categoryBanners with name="Images" %} <div class="category-banner-slider"> {% for imgUrl in categoryBanners %} <img src="{{ imgUrl }}" alt="{% categoryDetail with name="Title" %}" /> {% endfor %} </div>the parent category ID (
ParentId):This field is very useful for building multi-level classification navigation or judging the classification level relationship.<span>上级分类ID:{% categoryDetail with name="ParentId" %}</span>分类的文档数量 (English)
ArchiveCount):Displays the number of articles or products under the current category.<span>该分类下共有文档:{% categoryDetail with name="ArchiveCount" %} 篇</span>Document model setting classification other field parameters (custom fields):AnQiCMS Powerful content model functionality allows you to add various custom fields to categories. These custom fields can also be
categoryDetailLabel acquisition. You can directly obtain it through custom fields.调用字段The value by name, or obtain an object containing all custom fields.ExtraThen loop through them.{# 获取名为 "custom_field_name" 的自定义字段值 #} <span>自定义字段值:{% categoryDetail with name="custom_field_name" %}</span> {# 遍历所有自定义字段 #} {% categoryDetail categoryExtraFields with name="Extra" %} {% for field in categoryExtraFields %} <div>{{ field.Name }}:{{ field.Value }}</div> {% endfor %}
Practical Example: Building a Banner and Introduction for a Category Page
Assuming we are building a product category page, we hope to display the category Banner image, title, and detailed description at the top of the page, and list the custom properties under the category.
<div class="category-header">
{# 获取并显示分类的 Banner 图,并判断是否存在 #}
{% categoryDetail bannerImages with name="Images" %}
{% if bannerImages %}
{# 假设我们只取第一张图作为 Banner #}
{% set categoryBanner = bannerImages[0] %}
<div class="category-banner" style="background-image: url('{{ categoryBanner }}');">
<h1>{% categoryDetail with name="Title" %}</h1>
</div>
{% else %}
{# 如果没有 Banner 图,只显示标题 #}
<h1>{% categoryDetail with name="Title" %}</h1>
{% endif %}
<div class="category-description">
<p>{% categoryDetail with name="Description" %}</p>
{# 显示分类的详细内容,包含HTML #}
{% categoryDetail categoryRichContent with name="Content" render=true %}
{{ categoryRichContent|safe }}
</div>
{# 显示自定义属性,例如“适用人群”、“特点”等 #}
<div class="category-attributes">
<h3>分类特色</h3>
<ul>
{% categoryDetail categoryCustomFields with name="Extra" %}
{% for field in categoryCustomFields %}
{# 排除不想显示的字段,例如“内部备注” #}
{% if field.Name != '内部备注' %}
<li><strong>{{ field.Name }}:</strong>{{ field.Value }}</li>
{% endif %}
{% endfor %}
</ul>
</div>
</div>
By these methods, you can make full use ofcategoryDetailTags, categorize data in a rich form on your AnQiCMS website, thereby enhancing user experience and content expressiveness.
Common Questions (FAQ)
1. Why do I only show a few tags instead of all the tags I expected after using the tag?{% categoryDetail with name="Content" %}Is the content obtained the original HTML or Markdown code, not the rendered style?
This is because AnQiCMS defaults to escaping output to prevent XSS attacks. If your category content contains HTML tags or Markdown format,|safeFilter to tell the template that this part of the content is safe and should be rendered directly as HTML. If the content is in Markdown format, you still need to addrender=trueParameter, for example: {% categoryDetail categoryContent with name="Content" render=true %}{{ categoryContent|safe }}.
2. How to get a category's Logo or Banner, and not display a broken image icon when there is no image?
You can first assign the image path to a variable, thenifuse the statement to judge whether the variable exists<img>the image path. If it exists, render the tag, otherwise