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 efficient and customizable features, provides us with a powerful template engine that inherits the essence of Django templates, allowing us to implement complex logical judgments on the front-end page as if we were programming.not inLook at how to cleverly use it in the AnQiCMS template to judge whether a value is not in a set.
Mastering Data: How to cleverly use it in the AnQiCMS templatenot inOperator judgment value is not in the set
In the powerful template engine of AnQiCMS, 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 some valuenotIn a predefined set, perform a specific operation such logic. AnQiCMS's template syntax is similar to Django template engine, which brings us rich logical control capabilities, includinginandnot 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 a mapping key).This judgment is particularly useful in scenarios where content exclusion, permission restriction, or avoiding duplicate display is required.tag-calc.mdIt is explicitly stated,not inIs part of the arithmetic operation tag, specifically used to 'determine if a variable is in another result set'.
not inBasic structure and application of the operator.
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 %}
Here, the "some collection" can be an 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 layer to make them usablenot inOperator recognized correctly.
Actual scenarios and application examples
To better understandnot inUsability, let's take a look at several common application scenarios in website operations.
Example 1: Dynamic content filtering and exclusion
Suppose you are building a news or product list page but want to exclude certain articles with IDs marked as "top" or "expired". Handling this directly in the backend logic is of course possible, but if the frontend template needs more flexible exclusion rules,not init can be very useful.
We can first define a list of article IDs that need to be excluded, then iterate through all articles and only display those articles whose IDs are not on 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 throughlistThe filter converts it into an array that AnQiCMS template can recognize. When traversing the articles,article.Id|string not in excluded_article_idsTo determine if the article ID is in the exclusion list. It is especially important to note that,article.Idis of integer type,excluded_article_idsThe elements are of string type, and type conversion is required (such asarticle.Id|string), to ensure the accuracy of comparison.
Example two: Display of role content based on permissions
In a corporate website or member system, you may want certain content or menu items to be invisible to users of specific permission levels.For example, only users with VIP level 3 or below can see a prompt to upgrade to VIP.
Assuming your template has oneuseran object that includesuser.GroupId(User Group ID), and a VIP level that needs to avoid displaying contentrestricted_vip_groupsList:
”`twig {# Assuming 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 specific content #} {% set restricted_vip_groups = ‘[1, 2, 3]’|list %} {# Assuming 1, 2, 3 are the VIP level IDs to be excluded #}
<h3>欢迎,{{ user.UserName }}!</h3>
{# 只有当用户组ID不在受限列表中