In the process of template development for AnQi CMS, we often encounter scenarios where we need to determine whether an array (slice) contains a specific value.For example, you may need to dynamically adjust the display of content based on whether a user tag exists in a predefined tag list; or when handling complex business logic, determine whether a permission ID exists in the current user's permission set.For this requirement, AnQiCMS's template engine provides a concise and powerful solution, allowing developers to handle these logic in an elegant way.
In the AnQiCMS template system, we do not need to write complex loops to compare array elements one by one. Thanks to its built-in rich filters, we can use them directly.containA filter to easily complete this task. This filter is designed to be very intuitive, with its core function being to determine if an object (which can be a string, array, Map, or struct) contains a certain 'keyword', and returns a boolean value (TrueorFalse).
Deep understandingcontainFilter
containThe filter is a very practical tool in the AnQiCMS template engine, which can efficiently check if a collection or string contains the specified content.When applied to an array (slice), it checks if there is an element value in the array that matches the provided 'keyword' exactly.if[Statement), thus achieving dynamic content display.
Actual operation: How to determine if a value exists in an array (slice))
Let's demonstrate through a specific example.containThe usage of the filter to determine if a value exists in an array (slice).
Suppose we have a template context named in AnQiCMS.user_tagsThe array, which contains all the tags owned by the current user, for example:["VIP", "推广大使", "活跃用户"]Now, we want to determine if the user has the "VIP" tag.
{# 假设 user_tags 是一个包含用户标签的数组 #}
{% set user_tags = ["VIP", "推广大使", "活跃用户"] %}
{# 使用 contain 过滤器判断是否存在“VIP”标签 #}
{% if user_tags|contain:"VIP" %}
<p>恭喜您,您是我们的尊贵VIP用户!</p>
<a href="/vip-exclusive-content">查看VIP专属内容</a>
{% else %}
<p>您还不是VIP用户,立即升级享受更多特权!</p>
<a href="/upgrade-to-vip">升级VIP</a>
{% endif %}
In the above code:
- We first go through
{% set user_tags = ["VIP", "推广大使", "活跃用户"] %}Defined a nameduser_tagsthe array. In practical applications,user_tagsit is usually passed from the backend logic to the template. - Next, we use
{% if user_tags|contain:"VIP" %}to make conditional judgments.user_tags|contain:"VIP"This part iscontainthe application of filters, which will checkuser_tagsDoes the array contain a string"VIP". - If it exists, the condition judgment result is
True, it will display content such as 'Congratulations, you are our esteemed VIP user!'. - If not present, the conditional judgment result is
Falseand it will display content such as 'You are not a VIP user...' etc.
This approach avoids the麻烦 of manually traversing the array, making the template code more concise, readable, and efficient.
More than just an array:containBroad application scenarios of filters
containThe filter is not only effective for arrays (slices), but its strength lies in its wide applicability. It can be applied to various data types to meet different judgment needs:
- Check if a string contains a substring:You can check if a longer text string contains a certain keyword.
{% set article_title = "安企CMS:打造高效内容管理体验" %} {% if article_title|contain:"CMS" %} <p>标题中包含关键词“CMS”。</p> {% endif %} - Check if a Map or structure contains a certain key name:
containThe filter can also be used to check if a Map (key-value collection) or a struct contains a specified key name.
It is important to note that when used with Map or struct,{% set product_info = {"name": "安企CMS", "version": "3.0", "price": 0} %} {% if product_info|contain:"version" %} <p>产品信息中包含版本号。</p> {% endif %}containThe check is for the existence of the key name (key), not whether any value exists in the Map under any key.
Summary
AnQiCMS template engine'scontainThe filter brings great convenience to template development.It provides a declarative, efficient, and easy-to-understand way to determine if a specific content exists in an array (slice), string, Map, or struct.containFilter, we can write AnQiCMS templates that are more dynamic and logically strong, thereby enhancing the website's interactive experience and the efficiency of content operations.
Common Questions (FAQ)
Q1: How to determine if an array 'does not' contain a specific value?
A1:If you want to determine if an array 'does not' contain a specific value, just add acontainfilter result before thenotKeyword is enough.
{% set user_roles = ["Admin", "Editor"] %}
{% if not user_roles|contain:"Guest" %}
<p>用户不是访客。</p>
{% endif %}
Q2: Besides checking for existence, can I also get the specific "position" of this value in the array?
A2:It can be. You can use the AnQiCMS template engine providedindexUse a filter to get the first occurrence position of a value in an array (slice) or a string. If not found, it will return-1.
{% set product_ids = [101, 105, 110, 105] %}
{% set position = product_ids|index:105 %}
<p>值105在数组中首次出现的位置是:{{ position }}</p> {# 输出:1 (数组索引从0开始) #}
{% set not_found_position = product_ids|index:999 %}
<p>值999在数组中首次出现的位置是:{{ not_found_position }}</p> {# 输出:-1 #}
Q3: How many times does the value appear if there may be multiple identical values in the array?
A3:You can usecountA filter to count the number of occurrences of a specific value in an array (slice) or a string.
{% set status_list = ["pending", "approved", "rejected", "approved"] %}
{% set approved_count = status_list|count:"approved" %}
<p>“approved”状态出现了:{{ approved_count }} 次。</p> {# 输出:2 #}
{% set article_text = "AnQiCMS 是一个强大的CMS,AnQiCMS让内容管理更简单。" %}
{% set cms_count = article_text|count:"CMS" %}
<p>“CMS”在文本中出现了:{{ cms_count }} 次。</p> {# 输出:2 #}