When developing AnQiCMS website templates, we often need to dynamically display or hide content based on certain conditions.An 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 certain tag.inOperator, capable of easily solving such problems.

Core Function Analysis:inWhat is an operator?

AnQiCMS template engine design inspiration comes from Django template, it provides powerful and intuitive syntax to handle various data logic. Among them,inOperators are a very practical tool. They are mainly used to determine whether a specific element exists in a collection (such as an array, slice, or map)map)中,or to determine whether one substring exists within another string.

If the judgment result is that it exists, the condition judgment is true.true);otherwise it is false.false)。This makes it inifThe conditional 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 several specific examples to see how it works in different types of data collections.

Scenario one: Determine if 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 practical AnQiCMS applications, you may receive a list fromtagListorarchiveListsuch tags. For example, theFlagattribute of a document may be a string containing multiple recommended properties, such as"h,c,f"(Representing headlines, recommendations, and slides). You can useinoperator 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,inThe operator can effectively determine whether it contains a specific character or substring, 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 amapin,inOperators also apply.

For example, supposeuserPermissionscontains 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 is worth noting that,inOperators are used inmapmainly for judgment purposes.keyDoes it exist, rather than the value.

Scenario three: Determine if the substring exists within another string.

Except for arrays,map,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 whether an element does not exist in a set. At this time, we can use the operator to achieve the reverse judgment.notTo implement 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 way is{% if not (currentUserIP in blacklistedIPs) %}, but usuallynot inThis form has better readability