In the daily operation of AnQi CMS, we often encounter scenarios where we need to perform different operations based on the classification of documents.For example, articles in certain specific categories may require unique layout styles, or only the content of certain categories may be accessible to certain user groups.How can we accurately determine whether the category ID of the current document is in a predefined "limiting" or "special processing" category list, which has become a core issue.

Today, let's delve deeply into how to implement this judgment logic elegantly and efficiently in the AnQiCMS template.As an experienced website operation expert, I fully understand the importance of transforming technical details into practical operations, so I will strive to use the most natural and fluent language to reveal the strong capabilities and flexibility of AnQiCMS in dealing with such scenarios.

Why do you need to judge the document category ID?

In the practice of content operation, the judgment of classification ID has a wide range of application value.Imagine that your website may contain multiple categories such as 'News Update', 'Product Introduction', and 'VIP Exclusive Content'.

  • Differentiation display:“VIP exclusive content” category articles, whose detail page layout is completely different from that of ordinary news articles, such as adding purchase/subscription entry or unique interactive modules.
  • Permission control:Only paid members can view the document summary under the "VIP Exclusive Content" category, or even ordinary users may not see these contents on the list page.
  • Function module binding:Some ad slots or sidebar recommendation modules are displayed only on pages under the "Product Introduction" category, and are hidden on pages of other categories.

AnQiCMS provides a flexible template tag system, allowing us to easily implement these complex conditional logic on the front end.

Get the category ID of the current document

To make a judgment, we first need to know which category the document displayed on the current page belongs to. In the AnQiCMS template environment, this can be done byarchiveDetailtags or direct accessarchiveImplement an object.

When you are on a document detail page, you can usually directly go through{{ archive.CategoryId }}to get the current document's category ID. If it is in a loop (for example),archiveListwithin a tag).item.CategoryIdWill provide the category ID of the current loop item.

To ensure we can obtain it accurately, here we take the most general method as an example, obtaining the category ID on the document detail page:

{# 假设我们正在一个文档详情页,archive代表当前文档对象 #}
{% archiveDetail currentCategoryId with name="CategoryId" %}
{# 此时,currentCategoryId 变量就存储了当前文档的分类ID #}

Define your restricted category list

Next, we need a predefined list of category IDs to compare with the category ID of the current document.In AnQiCMS, the most flexible and easy-to-manage way to define this list is through the 'Custom Setting Parameters' in the backend.This way, you do not need to modify the template code and can adjust the restriction list at any time.

  1. Enter the backend:Log in to the AnQiCMS backend management interface.
  2. Navigate to custom settings:Find “Backend Settings” -> “Global Function Settings”.
  3. Add custom parameters:In the "Custom Setting Parameters" area at the bottom of the page, click "Add Parameter".
    • Parameter name (Name):It is recommended to use meaningful English names, such asRestrictedCategoryIds.
    • Parameter value (Value):Enter the category IDs you want to limit, separated by English commas,Separated by. For example:1,5,8,12.
    • Remark (Remark):Enter a clear description, such as "The ID list for the VIP exclusive category."
  4. Save the settings.

After completing the settings, we can use it in the template.systemTag to get the value of this custom parameter:

{# 获取后台自定义设置中名为 RestrictedCategoryIds 的值 #}
{% system restrictedCategoryIdsString with name="RestrictedCategoryIds" %}
{# 此时,restrictedCategoryIdsString 的值将是类似于 "1,5,8,12" 的字符串 #}

Core judgment logic:splitwithinusefulness

We already have the current document's category ID (a number) and the background setting's restricted category ID string (such as"1,5,8,12")。Now, we need to convert this string into a searchable list, then determine if the current category ID is in it.

AnQiCMS's template engine provides a powerful filter for data processing.splitThe filter can split strings into an array according to the specified delimiter, andinThe operator can be used to determine if a value exists in an array.

Combined, the judgment logic is as follows:

  1. Convert a string to an array:UsesplitThe filter willrestrictedCategoryIdsStringComma-separated,Split into an ID string array.
  2. Execute a judgment containing:UseinOperator, check the current document'scurrentCategoryIdWhether it exists in the split array.

Let's look at the complete code example:

{# 1. 获取当前文档的分类ID #}
{% archiveDetail currentCategoryId with name="CategoryId" %}

{# 2. 获取后台设置的限制分类ID字符串 #}
{% system restrictedCategoryIdsString with name="RestrictedCategoryIds" %}

{# 3. 将限制分类ID字符串通过 "," 分割成数组 #}
{% set restrictedCategoryList = restrictedCategoryIdsString|split:"," %}

{# 4. 执行判断并根据结果展示不同内容 #}
{% if currentCategoryId|stringformat:"%d" in restrictedCategoryList %}
    {# 如果当前文档的分类ID在限制列表中,则显示此内容 #}
    <div class="vip-exclusive-content">
        <h3>这是VIP专属文章!</h3>
        <p>您已获得访问权限,欢迎阅读。</p>
        {# 可以在这里插入 VIP 专属的布局、功能等 #}
    </div>
{% else %}
    {# 如果不在限制列表中,则显示普通内容 #}
    <div class="normal-content">
        <h3>普通文章内容</h3>
        <p>欢迎所有访客阅读。</p>
        {# 正常文章的布局和内容 #}
    </div>
{% endif %}

A little tip:While usinginThe operator performs a comparison of numbers (such ascurrentCategoryId) with a string array (restrictedCategoryListwhere the elements are all strings). The most robust approach is to convert the numbers to string type. Here we used|stringformat:"%d"The filter willcurrentCategoryIdConvert to a string to ensure type matching and avoid potential comparison issues.

Application and expansion in practice.

Mastered