The `in` operator in the AnQiCMS template's `if` statement, how to judge whether an element exists in an array or set?

Calendar 👁️ 64

When developing AnQiCMS website templates, we often need to dynamically display or hide content based on certain conditions.A common requirement is to determine whether an element exists within a dataset, such as checking if a user has a specific role or if the current article has a specific tag.AnQiCMS's template engine provides a concise and powerfulinAn operator that can easily solve this kind of problem.

Core function analysis:inWhat is an operator?

The design inspiration of AnQiCMS template engine comes from Django template, it provides a powerful and intuitive syntax for handling various data logic. Among them,inAn operator is a very practical tool. It is mainly used to determine whether a specific element exists in a set (such as an array, slice, mappingmapIn, or to determine if a substring exists within another string.

If the judgment result is existence, the conditional judgment is true (true); otherwise, it is false (false) makes it inifConditional content rendering in statements becomes very efficient and intuitive.

Use cases and examples: hands-on practicein

inThe basic syntax of operators is very simple and clear:{% if 元素 in 集合 %}. Next, we will look at how it works in different types of data collections through several specific examples.

Scenario one: Determine whether an element exists in an array or slice.

Assume you have an array containing user rolesactiveUserRolesYou need to display different content based on whether the user is an "admin".

{% set activeUserRoles = ["admin", "editor", "viewer"] %}

{% if "admin" in activeUserRoles %}
    <p>您好,管理员!欢迎来到管理面板。</p>
{% endif %}

{% if "moderator" in activeUserRoles %}
    <p>您拥有版主权限。</p>
{% else %}
    <p>您不具备版主权限。</p>
{% endif %}

In the actual AnQiCMS application, you may obtain a list fromtagListorarchiveListetc. tags. For example, theFlagattribute may be a string containing multiple recommended attributes, such as"h,c,f"Representing headlines, recommendations, and slides. You can useinThe operator to determine whether a document contains a specificFlag:

{# 假设 archive.Flag 的值为 "h,c,f" #}
{% if "h" in archive.Flag %}
    <span class="badge badge-primary">头条</span>
{% endif %}
{% if "c" in archive.Flag %}
    <span class="badge badge-success">推荐</span>
{% endif %}
{% if "j" in archive.Flag %}
    <span class="badge badge-info">跳转</span>
{% endif %}

Herearchive.FlagAlthough it is a string, butinThe operator can effectively determine whether it contains a specific character or substring, which is very flexible.

Scenario two: Determine if the key exists in the Map (dictionary/set)

In AnQiCMS, you may have defined some custom fields or obtained some configurations stored in key-value pairs from the backend. If you need to determine whether a specific key (key) exists in amap,inThe operator applies equally.

For example, assumeuserPermissionsContains specific user permissions.map:

{% set userPermissions = {"create_article": true, "delete_comment": false, "view_dashboard": true} %}

{% if "create_article" in userPermissions %}
    <button class="btn btn-primary">发表新文章</button>
{% endif %}

{% if "edit_settings" in userPermissions %}
    <p>您可以编辑网站设置。</p>
{% else %}
    <p>您没有编辑网站设置的权限。</p>
{% endif %}

It should be noted that,inThe operator is in.mapIt is mainly used for judgment.keyWhether it exists, rather than the value.

Scene three: Determine if a substring exists within another string

Except for arrays andmap,inThe operator can also be used directly for inclusion judgment between strings. This is particularly useful when dealing with text content, keywords, or URLs.

{% set documentContent = "AnQiCMS 是一个基于 Go 语言开发的企业级内容管理系统,致力于提供高效、可定制、易扩展的内容管理解决方案。" %}

{% if "Go 语言" in documentContent %}
    <p>本文内容包含了关于 Go 语言的讨论。</p>
{% endif %}

{% if "数据库优化" in documentContent %}
    <p>本文涉及数据库优化相关话题。</p>
{% else %}
    <p>本文未提及数据库优化。</p>
{% endif %}

CombinenotOperator: reverse judgment

Sometimes, we need to determine if an element does not exist in a set. At this time, we can combinenotoperator to achieve the reverse judgment.

{% set blacklistedIPs = ["192.168.1.1", "10.0.0.5", "172.16.0.1"] %}
{% set currentUserIP = "203.0.113.42" %}

{% if currentUserIP not in blacklistedIPs %}
    <p>您的IP地址未被列入黑名单,可以正常访问本站。</p>
{% endif %}

Another equivalent expression is{% if not (currentUserIP in blacklistedIPs) %}, but usuallynot inThis form is more readable

Related articles

How to use the `not` operator to reverse conditional judgments in AnQiCMS templates?

The AnQiCMS template system is renowned for its flexibility and efficiency, drawing inspiration from Django's template engine syntax, making content presentation and logical control intuitive.In template development, we often need to display or hide content based on different conditions, at this point, mastering the various usages of conditional judgment is particularly important.Today, let's talk about how to cleverly use the `not` operator in AnQiCMS templates to reverse condition judgments, making your page logic clearer and more flexible.What is the `not` operator?

2025-11-09

How to construct complex conditional display logic in AnQiCMS templates using `if`, `elif`, and `else` structures?

In AnQiCMS template design, dynamically displaying content is a key factor in enhancing website interactivity and user experience.When we need to decide what and how to display on the page based on specific data conditions, the `if`, `elif` (short for else if), and `else` conditional tags are particularly important.They have given the template flexible logical control capabilities, allowing our website to meet various complex display needs.The condition judgment syntax of the AnQiCMS template engine is similar to many programming languages, it is very intuitive and easy to understand

2025-11-09

How to use the `if` tag in AnQiCMS templates to determine if a variable is empty, exists, or has a specific value?

In AnQiCMS template development, the `if` tag is the core tool for building dynamic page content.It allows us to flexibly control the display of content based on different conditions, thereby providing users with a more intelligent and personalized browsing experience.Whether you want to judge whether a data exists, whether it is empty, or hope to adjust the layout according to a specific value, the `if` tag can help you easily achieve it.AnQiCMS's template engine syntax is very similar to the Django template engine, therefore, developers familiar with this syntax will feel very亲切。`if`

2025-11-09

How to implement multi-condition logical judgment with the `if` tag in AnQiCMS template (OR/&&, AND/||, NOT/!)?

In AnQiCMS template development, mastering conditional logic judgment is the key to building dynamic and intelligent web pages.Among them, the `if` tag is the core of controlling content display, not only supporting simple boolean judgments, but also being able to flexibly implement logical combinations of multiple conditions, such as "and" (`&&`), "or" (`||`), and "not" (`!`)`etc. Understanding and skillfully using these logical operators can help us control the display and hiding of template elements more finely, thereby creating a more interactive and customizable user experience.### Basic Conditional Judgment: `if`

2025-11-09

How to avoid excessive nesting of `if` statements in AnQiCMS templates to improve code readability?

In AnQiCMS template development, we often encounter situations where we need to display different content based on different conditions.Newcomers may tend to use the `{% if condition %}` statement extensively. As the project requirements grow, these conditional judgments become nested layer by layer, which quickly makes the template difficult to read, maintain, and even hides potential logical errors.Code readability once reduced, not only will the development efficiency be affected, but the future feature iteration and problem troubleshooting will also become extremely difficult.Fortunate is that AnQiCMS is based on Go language Django-like

2025-11-09

How to set default display content for possibly empty variables in the `default` filter of AnQiCMS templates?

In Anqi CMS template design, we often encounter situations where variable values may be empty.For example, an article may not have a thumbnail set or a product field may be temporarily unfilled.If the template directly displays these empty values, ugly blank spaces will appear on the page, affecting user experience, and may even confuse visitors.To solve this problem, Anqi CMS provides a very practical filter mechanism, where the `default` filter is the key tool to ensure the continuity of content display.### `default` Filter

2025-11-09

What is the difference between the `default_if_none` filter and the `default` filter in the AnQiCMS template, and when should the former be preferred?

In web template development, handling the case where variables may not exist or are empty is a common task.The AnQiCMS template system provides a variety of filters to help us elegantly handle such issues, with `default` and `default_if_none` being two commonly used tools.They both provide a default value when the variable is 'no value', but there is a subtle but important difference in the definition of 'no value'.Understanding these differences can help us control the display of template content more accurately.###

2025-11-09

How to elegantly handle null or undefined variables returned by the database in AnQiCMS templates to avoid page errors?

During the development of website templates, we often encounter a headache-inducing problem: when the data obtained from the database is empty (null) or a variable is not defined under certain circumstances, the template rendering will cause an error, resulting in the page not displaying normally and significantly reducing the user experience.AnQiCMS (AnQiCMS) is based on its powerful Go language backend and flexible Django-style template engine, providing us with various elegant solutions for handling such situations, allowing the template to run smoothly even when the data is incomplete.### One, make good use of conditional judgments: `{% if

2025-11-09