In the template development of Anqi CMS, we often need to dynamically adjust the display method of the page based on the different characteristics of the data.Among these, determining whether a string or array contains a certain keyword or element is a very basic but extremely common requirement.containFilter has become a powerful tool in our hands, which can help us easily achieve this goal.
containThe filter is used to check for the existence of a specific content within a variable (whether it is a string, array, key-value pair, or structure). Its result will be a boolean value: if it contains, it will returnTrueIf not included, it returnsFalseThis allows us to write smarter and more flexible conditional logic in templates.
Let's understand it through specific examples.containThe application of filters in different scenarios.
Check if a keyword is included in a string
When your variable is a piece of text,containThe filter can quickly check if this text contains a substring.This is very useful in many scenarios, such as determining whether a specific word is mentioned in the article title or content abstract.
Example:
{% set articleTitle = "安企CMS:企业建站好帮手,效率提升秘籍" %}
{% if articleTitle|contain:"CMS" %}
<p>该文章标题中包含了 "CMS",是关于内容管理系统的重要文章。</p>
{% else %}
<p>该文章标题中不包含 "CMS"。</p>
{% endif %}
In the above example,articleTitleThe variable indeed contains “CMS”, so the condition is met, and the first paragraph of the page will be displayed.
Check if an element exists in the array (slice)
If your variable is an array (commonly represented as a slice in Go language templates),containThe filter can determine if there is a completely matching element in this array.This is used to check if a specific tag is included in the article tag list, or if the value of a multi-select field contains a certain option.
Example:
Assumeitem.Tagsis an array containing multiple tags, for example,["网站运营", "SEO优化", "内容营销"].
{% set itemTags = ["网站运营", "SEO优化", "内容营销"] %}
{% if itemTags|contain:"SEO优化" %}
<p>此文章属于 "SEO优化" 类别,内容对搜索引擎优化有帮助。</p>
{% else %}
<p>此文章不属于 "SEO优化" 类别。</p>
{% endif %}
here,itemTagsThe array contains the element “SEO optimization”, so the condition is met, and the first paragraph of the page will be displayed.
Check if a key exists in a key-value pair (map) or a structure (struct)
When your variable is an associative array (such as system settings parameters, custom fields) or a structure,containThe filter can be used to determine if a specific key name (field name) exists.This is very useful for checking the existence of custom fields, to avoid error when rendering templates due to missing fields, or to dynamically display content based on the availability of different fields.
Example:
Assumeproductis a structure or associative array that may contain custom fields.
{% set product = {name:"智能手机", price:2999, color:"黑色"} %}
{% if product|contain:"color" %}
<p>此产品有颜色选项:{{ product.color }}</p>
{% else %}
<p>此产品没有颜色选项。</p>
{% endif %}
In this example,productContains the "color" key, so the paragraph with color information will be displayed.
CombinesetandifTags, to implement more complex logic
containFilters are usually used with{% set %}Label usage is combined, and the judgment result is assigned to a temporary variable, and then through{% if %}Label is used for conditional judgment, thus realizing more complex and clear template logic.
{% set pageContent = "欢迎来到安企CMS的官方网站,这里提供高效、易用的内容管理解决方案。" %}
{% set isOfficialSite = pageContent|contain:"官方网站" %}
{% if isOfficialSite %}
<div class="official-badge">
<img src="/static/images/official.png" alt="官方认证">
<p>此页面为官方内容,请放心浏览。</p>
</div>
{% else %}
<p>此页面可能为用户分享内容,信息仅供参考。</p>
{% endif %}
This writing makes the logic more readable and convenient for reuse in subsequent templatesisOfficialSiteThis judgment result.
Summary of practical application scenarios
- Content dynamic highlighting:If the article content contains specific keywords, you can add highlighting styles to the keywords.
- Personalized recommendations:Based on the tags in the user's browsing history, determine whether the recommended content contains tags of interest to the user.
- Navigation menu activation status:Check if the current URL contains a menu link and automatically add menu items
activeclass. - Field existence check:Use it first before displaying the custom field content,
containCheck if the field exists to avoid displaying unnecessary content due to the field being empty.
Some important notes
- Case sensitive:
containThe filter is case-sensitive when matching. For example,"Apple"|contain:"apple"it will returnFalse. If case-insensitive matching is required, it may be necessary to convert the string to uppercase or lowercase before the judgment. - Array element matches completely:For array type,
containelements must be a complete match, no fuzzy matching will be performed. - Key name/field name matches:For key-value pairs or structures, it checks the key name or field name, not the key value.
containThe filter is a powerful and flexible tool in the Anqi CMS template.Familiarity with its usage can help us write templates that are more dynamic, adaptable, and concise, thereby significantly enhancing the management efficiency of website content and user experience.
Common Questions (FAQ)
containDoes the filter distinguish between uppercase and lowercase?Yes,containThe filter is case-sensitive when matching. For example,{{"AnQiCMS"|contain:"anqicms"}}it will returnFalse。If you need case-insensitive matching, you can consider converting the string to a uniform case (such as all lowercase) before comparisoncontainBefore judgment, convert the string to a uniform case (such as all lowercase) for comparison.Except
containWhat are some similar filters available in the Anqi CMS template that can be used to check content?The Anqi CMS template also providescountfilters andindexFilter.countUsed to calculate the number of occurrences of keywords or elements in a string or array,indexThen return the position of the first occurrence of the keyword or element (if not found, return -1). Both return numbers, unlikecontainreturning boolean values, you can choose to use them according to your specific needs.containCan the filter be used to check if the 'value' of a custom field contains a specific keyword?containThe filter cannot be used directly to check if the 'value' of a custom field contains a specific keyword unless the value itself is a string.The value itself is a string.It is mainly used for:- Check if the string itself contains a substring.
- Check if the array contains a specific element.
- Check if a certain "key name" or "field name" exists in a key-value pair or structure.
If you want to check whether the value of a custom field contains a specific keyword, you need to get the value of the custom field first, and then use that value as
containThe input of the filter. For example,{{ archive.CustomFieldValue | contain:"关键词" }}.