In the template development of AnQi CMS, we often need to dynamically adjust the display of the page based on the specific attributes or keywords of the content.For example, determine whether a title of an article contains a certain word, whether a product category is in a certain list, or whether a certain configuration option exists.The powerful template engine of Anqi CMS provides a variety of filters and operators, which can help us easily achieve these judgments and obtain clear boolean (True/False) results.
Let's delve deeper into how to implement these flexible judgments in Safe CMS.
Core Function:containFilter
When you need to judge whether a string of text, an array (or a slice in Go language), or the key name of a map/struct contains a specific keyword, containThe filter is your preferred tool. It returns a boolean value, directly telling you the result.True(including) orFalse(excluding).
Basic Usage
containThe usage of the filter is very intuitive:
{{ obj|contain:"关键词" }}
Among themobjis the variable you want to check, while"关键词"It is the specific content you want to search for.
Application in conditional judgment
Usually, we will containThe result of the filter is used forifIn the logical judgment tag, to control the display logic of the template.
{% if "欢迎使用安企CMS(AnQiCMS)"|contain:"CMS" %}
<p>这段文字中包含了"CMS"!</p>
{% else %}
<p>这段文字中没有找到"CMS"。</p>
{% endif %}
You can also firstcontainThe result of the filter is stored in a variable for further judgment, which helps to improve the readability and reusability of the code:
{% set sourceText = "安企CMS,高效内容管理" %}
{% set isFound = sourceText|contain:"内容管理" %}
{% if isFound %}
<p>内容中提到了“内容管理”这个关键词。</p>
{% else %}
<p>内容中未提及“内容管理”。</p>
{% endif %}
Judgment for different data types
containThe filter demonstrates its flexibility when handling different data types:
Keyword search in strings:When
objis a string when,containIt will check if the string contains the specified keyword as a substring.{% set articleTitle = "安企CMS:打造企业级内容管理平台" %} {% if articleTitle|contain:"企业级" %} <p>文章标题强调了“企业级”特性。</p> {% else %} <p>文章标题未突出“企业级”特性。</p> {% endif %}Element search in the array (slice):When
objWhen it is an array,containWill check if an element exists in the array that matches the specified keyword exactly.{% set tags = ["CMS", "GoLang", "企业", "效率"] %} {% if tags|contain:"GoLang" %} <p>这篇文章的标签包含了“GoLang”</p> {% else %} <p>这篇文章的标签不包含“GoLang”</p> {% endif %}Key name lookup for a map (map) or struct (struct):When
objis a mapping or struct,containit will check if the specified key name (or field name) exists in it. It should be noted that it checks forkey name, not the key value.{# 假设有一个名为 `contactInfo` 的映射变量 #} {% set contactInfo = {"email": "[email protected]", "phone": "123456789"} %} {% if contactInfo|contain:"phone" %} <p>联系方式中提供了电话号码。</p> {% else %} <p>联系方式中未提供电话号码。</p> {% endif %}
The use is somewhat different but related:inOperator
ExceptcontainFilter, the template engine of the Anzhi CMS also supports:inOperator, which is similar to but has different uses in some scenarios:containSimilar but focuses on:Exact matchThe existence of an element.inThe operator is mainly used to judge whether an element exactly exists in an array (slice) or a mapkey.
{# 判断数字 5 是否存在于数字列表 simple.intmap 中 #}
{% if 5 in simple.intmap %}
<p>数字 5 存在于列表中。</p>
{% else %}
<p>数字 5 不存在于列表中。</p>
{% endif %}
{# 判断字符串 "Hello" 是否存在于字符串列表 simple.misc_list 中 #}
{% if "Hello" in simple.misc_list %}
<p>“Hello”存在于列表中。</p>
{% else %}
<p>“Hello”不存在于列表中。</p>
{% endif %}
containWithinSummary of the differences:
containFilter:- Used for strings, performsubstring search.
- Used for arrays, performexact element matching.
- Used for maps/structs, checkField name exists.
inOperator:- Used for arrays, performexact element matching.
- Check when mappingExact field name exists.
- Cannot be used directly for substring search in strings.
In simple terms, if you need to perform substring fuzzy matching on strings, or check if the key name of the mapping exists,containIs a more general choice. If you only need to check if a specific element exists in an array, or if a specific key exists in a map,inthe operator is more concise.
Other auxiliary judgment methods
In addition to the above methods that directly return boolean results, there are some filters that indirectly express the state of 'contains' or 'does not contain' by returning specific values:
indexFilter:Used to find the first occurrence position of a keyword in a string or array. If returned-1,then it means not found.{% if "安企CMS"|index:"CMS" != -1 %} <p>“CMS”在字符串中出现。</p> {% endif %}countFilter:Used to calculate the number of times a keyword appears in a string or array. If returned0,then it means not found.
These auxiliary methods can also achieve boolean judgments through simple comparison operations while obtaining more information (such as position, frequency).{% if "安企CMS"|count:"CMS" > 0 %} <p>“CMS”在字符串中至少出现一次。</p> {% endif %}
By using flexibilitycontainfilters andinOperators, combinedifLogic label, you can easily implement various complex condition judgments in the AnQi CMS template, making your website content display more dynamic and intelligent.
Common Questions (FAQ)
containDoes the filter distinguish between uppercase and lowercase?Yes,containThe filter is case-sensitive when performing string matching. For example,"AnQiCMS"|contain:"cms"it will returnFalse. If you need to perform case-insensitive judgment, you can consider using first.lowerorupperThe filter converts both the string to be checked and the keywords to the same case (e.g., all to lowercase) before usingcontainThe filter. Example:{% if articleTitle|lower|contain:keyword|lower %}How to judge if a string contains multiple keywords (such as "CMS" and "GoLang")?You can in
ifUsing statements in combinationandororLogical operators to determine if multiple keywords are included.- Include both (and):
{% if articleTitle|contain:"CMS" and articleTitle|contain:"GoLang" %} - Include any of the following (or):
{% if articleTitle|contain:"CMS" or articleTitle|contain:"GoLang" %}
- Include both (and):
containThe filter can determine whether the key-value in the map contains a specific keyword?Cannot.containWhen the filter is used with a map, it will only check for the existence of a specifickey name,