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

Calendar 👁️ 64

As an experienced website operation expert, I know how important it is to flexibly control the display logic of content in daily content management.AnQiCMS with its efficient 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 in the front-end page as if we were programming.Today, let's delve into a very practical operator used in content filtering and permission control -not inHow to cleverly use it in the AnQiCMS template to judge whether a value is not in a set.

Mastering Data: How to cleverly use in the AnQiCMS templatenot inThe operator's judgment value is not in the set

In AnQiCMS's powerful template engine, we often need to decide whether to display content based on specific conditions. This is not just a simple 'if the condition is met then display', more often, we also need to handle 'if a certain valueNotThe logic of executing a specific operation when in a predefined set. The AnQiCMS template syntax is similar to the Django template engine, which brings us rich logic control capabilities, including such asinandnot insuch member detection operator.

In simple terms,not inoperator allows you to check whether a specific value isNotexists within a given set (such as a list, array, or map key).This judgment is particularly useful in scenarios where content exclusion, permission restriction, or avoiding duplicate display is required.According to AnQiCMS template tag documenttag-calc.mdpointed out explicitly,not inIt is a part of arithmetic operation tags, specifically used to 'determine if a variable is in another result set'.

not inBasic structure and application of operators

not inThe use of operators is very intuitive, it is usually combined with{% if %}tags to form conditional judgment statements. Its basic form can be summarized as:

{% if 某个值 not in 某个集合 %}
    <!-- 当该值不在集合中时,这里的内容会被渲染 -->
{% endif %}

This "some collection" can be the array, list, or even the key set of a map that you define in the template. The AnQiCMS template engine will handle these data structures at the bottom level to make them availablenot inOperator correctly recognized.

Actual scenarios and application examples

In order to understand betternot inUsability, let's look at some common application scenarios in website operations.

Example one: Dynamic content filtering and exclusion

Assuming you are building a news or product list page, but you want to exclude some articles with IDs marked as "top" or "expired". Of course, handling it directly in the backend logic is fine, but if the frontend template needs more flexible exclusion rules,not init can be put to use.

We can first define a list of article IDs that need to be excluded, and then iterate through all articles to only display those articles whose IDs are not in the exclusion list.

{# 1. 定义一个需要排除的文章ID集合。这里使用list过滤器将字符串转换为数组。 #}
{% set excluded_article_ids = '["1001", "1005", "1012", "1015"]'|list %}

<h2>最新文章</h2>
<ul>
    {# 2. 使用archiveList标签获取所有文章列表,type="list"表示非分页列表 #}
    {% archiveList archives with type="list" limit="20" %}
        {% for article in archives %}
            {# 3. 使用if...not in进行判断:如果当前文章ID不在excluded_article_ids集合中,则显示 #}
            {% if article.Id|string not in excluded_article_ids %} {# 注意这里article.Id转换为string,以匹配集合中的字符串类型 #}
                <li>
                    <a href="{{ article.Link }}">{{ article.Title }}</a> - 发布时间:{{ stampToDate(article.CreatedTime, "2006-01-02") }}
                </li>
            {% endif %}
        {% endfor %}
        {% empty %}
            <li>暂无文章内容。</li>
    {% endarchiveList %}
</ul>

In this example, we first created a string-based ID list, then bylistThe filter converts it into an array that AnQiCMS template can recognize. While iterating through articles, througharticle.Id|string not in excluded_article_idsTo determine if the article ID is in the exclusion list. It is especially important to note that ifarticle.Idis of integer type, whileexcluded_article_idsThe element is a string type, so a type conversion is required (such asarticle.Id|string), to ensure the accuracy of comparison.

Example two: Display of role content based on permission

In a corporate website or member system, you may want some content or menu items to be visible only to users of specific permission levels.For example, only users with VIP level 3 or below can see a prompt to upgrade to VIP.

Assume your template has oneuserobject that includesuser.GroupId(User Group ID), and a VIP level should avoid displaying content of therestricted_vip_groupsList:

”`twig {# Assume the current user object is user, and user.GroupId represents its user group ID #} {# Define a set of VIP group IDs that should not see certain content #} {% set restricted_vip_groups = '[1, 2, 3]'|list %} {# Assume 1, 2, 3 are the VIP level IDs that need to be excluded #}

<h3>欢迎,{{ user.UserName }}!</h3>

{# 只有当用户组ID不在受限列表中

Related articles

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

What are some practical applications of modulo operations when implementing alternating row coloring or cyclic display in templates?

In the Anqi CMS template world, we often need to make page elements move to be more visually vibrant and organized.This is not just for beauty, but also to enhance the user's reading experience and information acquisition efficiency.In this multitude of means to achieve dynamic effects, the modulo operator (Modulo Operator) plays a seemingly basic but extremely practical role.As an experienced website operations expert, I am well aware of how to transform these technical details into operational strategies that can directly improve website performance.

2025-11-06

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 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