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不在受限列表中