How to determine if the current document's category ID is included in the preset restricted category list?

Calendar 👁️ 63

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

Related articles

How to check if a label is in a specific list when displaying different content based on user tags?

As an experienced website operation expert, I am well aware that personalization and dynamic display are the keys to improving user experience and increasing content conversion rates.AnQiCMS (AnQiCMS) has provided great convenience for us to achieve these goals with its flexible and powerful template engine.Today, let's delve into a very practical scenario in template creation: **How to check if a user tag is in a specific list when dynamically displaying different content?In Anqi CMS, we often tag articles, products, and other content with various labels, such as "New Product Recommendation"

2025-11-06

How to use the `not in` operator in the AnQi CMS template to determine if a value is not in a set?

As an experienced website operations expert, I know how important it is to flexibly control the display logic of content in daily content management.AnQiCMS with its high efficiency and customizable features provides us with a powerful template engine, which draws on the essence of Django templates, allowing us to implement complex logical judgments on the front-end page like programming.Today, let's delve into a very practical operator in content filtering and permission control - `not in`, and see how to use it in AnQiCMS

2025-11-06

Can I use the `in` operator to check if a key exists in a map (key-value pair) or a structure?

## AnQi CMS template: Exploring the way to check the existence of key-value pairs and members in structures As an experienced website operations expert, I know how important it is to flexibly and effectively operate data in daily content management and website maintenance.In such a powerful content management system as AnQiCMS, which is developed based on Go language, the data processing capability at the template level directly affects the display effect and development efficiency of the front-end page.Today, let's delve into a common template operation requirement

2025-11-06

How to determine if a value exists in an array or list in Anqi CMS template (using the `in` operator)?

As an experienced website operation expert, I know that the flexibility of template logic is crucial when building and maintaining an efficient, user-friendly website.AnQiCMS (AnQiCMS) with its powerful template engine and good support for Django template syntax, provides us with many conveniences.Today, let's delve deeply into a very common and practical scenario in template development: **How to determine if a value exists in an array or list in AnQiCMS templates (i.e., the functionality of the `in` operator)?In the display of dynamic content

2025-11-06

How to convert a string number to an integer for calculation in AnQi CMS template (integer)

## Taming Data: The Mystery of Converting String Numbers to Integers in AnQi CMS Templates (Deta...AnQiCMS (AnQiCMS) boasts a high-performance architecture based on the Go language and a flexible Django-style template engine, providing powerful tools for content operators.However, data is often stored in databases or entered by users in string format.

2025-11-06

How to correctly add captcha functionality to the AnQiCMS comment form?

As an experienced website operations expert, I am willing to elaborate in detail on how to correctly add captcha functionality to the AnQiCMS comment form.This feature is crucial for maintaining the healthy ecosystem of the website, resisting spam invasion, and effectively improving user experience, ensuring that the messages you receive are real and effective.AnQiCMS as an efficient and flexible content management system provides a clear and straightforward path for captcha integration.

2025-11-06

Where in the Anqi CMS backend can I set the on/off for留言 and 评论verification code?

As an experienced website operations expert, I fully understand the importance of balancing website security and user experience.AnQiCMS provides efficient content management while also offering flexible security measures.Today, let's delve into how to enable and disable the captcha in the Anqi CMS backend for comments and message features, which is very helpful for resisting spam and maintaining website order.

2025-11-06

How to enable captcha for AnQiCMS comment form when submitting comments and encountering spam?

## Say goodbye to spam: How to enable captcha in AnQiCMS comment form?In the daily operation of website management, spam comments are often a headache.These comments posted by robots or malicious users not only fill up your comment section but also lower the overall quality of the website's content, may contain malicious links, affect user experience, and even negatively impact the website's SEO performance.In order to effectively control this phenomenon, it is particularly important to enable captcha verification for the comment form. AnQiCMS

2025-11-06