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