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

Calendar 👁️ 62

As an experienced website operations expert, I know that personalization and dynamic display are the key 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 making:How to check if a label is in a specific list when displaying different content dynamically based on user tags?

In AnQi CMS, we often label articles, products, and other content with various tags, such as "New Product Recommendation", "Limited Time Promotion", "VIP Exclusive", "Hot Selling Bestsellers", and so on.When we need to display different prompts, styles, or functional modules on the front-end page, such as an article list page or a product details page, we need a set of effective judgment mechanisms.This is like setting a 'qualification standard' for content: only content that matches a specific tag combination can receive special display treatment.

Understanding the template mechanism and tag acquisition in Anqi CMS

In Anqi CMS templates, we use a template engine syntax similar to Django. This means variables are enclosed in double curly braces{{变量}}However, logical control and loops use single curly braces with a percentage sign.{% 标签 %}To achieve our goal, we first need to be able to obtain the tags associated with the current document (or content).

Assuming we are on a document list page or detail page, document data and its associated tags can be obtained in the following way:

  1. Get the document list or details:

    • For lists, we usually use{% archiveList archives ... %}tags to iterate over the document.
    • For detail pages, the current document data is usually available directly, or use{% archiveDetail archive with ... %}to get the details of a specific document.
  2. Retrieve the associated tags of the document:InarchiveListorarchiveDetailEach returned document (oritem), contains itsIdunique identifier. We can use this ID, through{% tagList tags with itemId=item.Id %}Tag to get the list of all tags associated with the document. ThistagsThe variable will be an array containing multiple tag objects, each withTitle(label name),Link(Tag link) and other properties.

Here, we already have two sets of data: one is the content itself, and the other is all the tags carried by the content.The key is how to determine whether one or more of these tags exist in the predefined 'specific list'.

Build a "specific list" and perform label attribution check

The Anqi CMS template engine provides powerful filter (Filter) functions that can process and convert variables. Among them,|listFilters and|containThe filter is exactly the tool to solve our current problem.

  1. Define your "specific list":We can go through the template by{% set %}Label collaboration|listA filter to define a string array, this is what we expect as a "specific list". For example, we want to identify the tags "VIP Exclusive" and "New Product Recommendation":

    {% set specialTags = '["VIP专属", "新品推荐"]'|list %}
    

    here,specialTagsIt becomes an array containing the strings “VIP Exclusive” and “New Product Recommendation”.

  2. Utilize|containThe filter checks the tag affiliation: |containA filter is used to determine whether a collection (such as an array) or a string contains a specific element or substring. It is used in the following way:集合变量|contain:要查找的元素.

    Now, we will integrate the previous steps:

    ”`twig {# Assume we are in a loop of document lists #} {% archiveList archives with type=“page” limit=“10” %}

    {% for archive_item in archives %}
    <div class="article-card">
        <h3><a href="{{archive_item.Link}}">{{archive_item.Title}}</a></h3>
    
        {# 定义我们想要检查的特定标签列表 #}
        {% set targetTags = '["VIP专属", "新品推荐", "独家"]'|list %}
    
        {# 标记,用于判断当前文档是否包含特定标签 #}
        {% set hasSpecialTag = false %}
    
        {# 获取当前文档的所有标签 #}
        {% tagList current_archive_tags with itemId=archive_item.Id %}
            {% for tag in current_archive_tags %}
                {# 检查当前标签的Title是否在targetTags列表中 #}
                {% if targetTags|contain:tag.Title %}
                    {% set hasSpecialTag = true %} {# 如果找到任意一个,就设置为true #}
                {% endif %}
            {% endfor %}
        {% endtagList %}
    
        {# 根据hasSpecialTag的值,显示不同的内容 #}
        {% if hasSpecialTag %}
            <span style="color: red; font-weight: bold;">【{{ archive_item.Title }} - 专属推荐!】</span>
        {% else %}
            <span>{{ archive_item.Description|truncatechars:100 }}</span>
        {% endif %}
    
        {# 也可以在这里展示所有标签,辅助调试或作为内容的一部分 #}
        <div class="tags">
            {% tagList archive_tags_display with itemId=archive_item.Id %}
                {% for tag_display in archive_tags_display %}
                    <a href="{{tag_display.Link}}" class="tag-badge">{{tag_display.Title}}</a>
                {% endfor %}
            {% endtagList %}
        </div>
    </div>
    {% empty %}
        <p>暂无内容。</p>
    {% end
    

Related articles

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 use modulo operation to insert a specific HTML structure after every N elements in an article list?

Good, as an experienced website operation expert, I am very willing to deeply analyze how to use modulo operation in AnQiCMS to inject more vitality and functions into your article list. --- ## Advanced Anqi CMS Operations: How to skillfully use modulo operations to insert specific HTML structures after every N elements in an article list?

2025-11-06

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

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 under certain specific categories may require unique layout styles, or only content from certain categories may be accessible to certain user groups.How can we accurately determine whether the classification ID of the current document is in a predefined 'restriction' or 'special processing' classification list, which has become a core issue.

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