In Anqi CMS template development, 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 an article's title contains a certain word, whether a product category is on a certain list, or whether a configuration item exists.The Anqi CMS powerful template engine provides various filters and operators that can help us easily implement these judgments and obtain clear Boolean (True/False) results.
Let's delve deeper into how to implement these flexible judgments in AnQi CMS.
Core function:containFilter
When you need to determine whether a string of text, an array element (or a Go language slice), or a key name in 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 themobjIt is the variable you need to check, while"关键词"It is the specific content you want to find.
In the application of conditional judgment
Generally, we willcontainThe result of the filter is usedifIn the logic judgment tag, thus controlling 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, which helps to enhance 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 shows its flexibility when handling different data types:
Keyword search in a string:When
objWhen it is a string,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 %}Find element in an array (slice):When
objwhen it is an array,containCheck if there is an element 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 %}The key name lookup of (map) or (struct):When
objwhen it is a (map) or (struct),containIt will judge whether there is a specified key name (or field name) present. It is important to note that it judgeskey namenot the key-value pair.{# 假设有一个名为 `contactInfo` 的映射变量 #} {% set contactInfo = {"email": "[email protected]", "phone": "123456789"} %} {% if contactInfo|contain:"phone" %} <p>联系方式中提供了电话号码。</p> {% else %} <p>联系方式中未提供电话号码。</p> {% endif %}
It has a related but slightly different purpose:inoperator
exceptcontainFilter, the Anqi CMS template engine also supportsinOperator, it is used in some scenarios withcontainSimilar, but focuses onExact matchthe existence of elements.inThe operator is mainly used to determine 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:- When used for strings, it performssubstring search.
- Performing when used with an arrayExact element matching.
- Check when used with a map/structureCheck if the key (field name) exists.
inoperator:- Performing when used with an arrayExact element matching.
- Check when used with a mapDoes the exact key name exist.
- Cannot be used directly for substring search in strings.
In simple terms, if you need to perform substring fuzzy matching on strings, or determine if the key name exists in the mapping,containIs a more general choice. If you just need to check if an element exists in an array, or if a key exists in a map,inthe operator is more concise.
Other auxiliary judgment methods
In addition to the methods mentioned above that directly return boolean results, there are some filters that indirectly express the states of 'include' or 'exclude' by returning specific numbers:
indexFilter:Used to find the first occurrence position of a keyword in a string or array. If returned-1means 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 returned0means not found.
These auxiliary methods can also perform boolean judgments through simple comparison operations while obtaining more information (such as position, frequency).{% if "安企CMS"|count:"CMS" > 0 %} <p>“CMS”在字符串中至少出现一次。</p> {% endif %}
By flexible applicationcontainFilters andinCombined with operators,ifLogical tag, you can easily implement various complex conditional judgments in the Anqi CMS template, making your website content display more dynamic and intelligent.
Frequently Asked Questions (FAQ)
containIs the filter case sensitive?Yes,containThe filter performs case-sensitive string matching. For example,"AnQiCMS"|contain:"cms"It will returnFalse. If you need to perform a case-insensitive judgment, you can consider usinglowerorupperThe filter converts the string to be checked and the keyword to the same case (e.g., all to lowercase) before usingcontainFilter. Example:{% if articleTitle|lower|contain:keyword|lower %}How to determine if a string contains multiple keywords (such as both 'CMS' and 'GoLang')?You can
ifUsing in a sentenceandororLogical 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):
containCan the filter determine if the key-value in the map contains a specific keyword?No.containWhen used with a map, the filter will only check if the map contains a specifickey name,