In Anqi CMS template development, we often need to dynamically adjust the display of the page based on the different characteristics of the data.Among them, the need to judge whether a string or array contains a certain keyword or element is a very basic but extremely common requirement. At this time,containThe filter has become a powerful tool in our hands, it can help us easily achieve this goal.
containThe filter is mainly used to check if a variable (whether it is a string, array, or key-value pair and structure) contains a specific content. Its result will be a boolean value: if it contains, it will returnTrue; if it does not contain, it will returnFalseThis enables us to write smarter, more flexible conditional logic in the template.
Let's understand through specific examples.containThe application of filters in different scenarios.
Determine if the string contains a keyword
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 the title or abstract of an article mentions a specific word.
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 an array (slice)
If your variable is an array (often represented as a slice in Go 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 list of tags of an article, 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, becauseitemTagsThe array contains the element 'SEO optimization', so the condition is met, and the first paragraph of text will be displayed on the page.
Determine whether 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 if custom fields exist, to avoid errors during template rendering 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 page will display a paragraph with color information.
Combinesetandiftags to implement more complex logic
containFilters are usually used with{% set %}Use the tag in conjunction, which assigns the judgment result to a temporary variable and then passes it through{% if %}Use the tag 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 style not only makes the logic more readable, but also 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 highlight the keywords.
- Personalized recommendation: Based on the user's browsing history tags, judge whether the recommended content contains tags that the user is interested in.
- Navigation menu activation state:Determine if the current URL contains a menu link, and automatically add to the menu item
activeClass. - Field existence check:Before displaying custom field content, use
containCheck if the field exists to avoid displaying unnecessary content due to an empty field.
Some important points to note.
- Case sensitive:
containThe filter is case-sensitive when matching. For example,"Apple"|contain:"apple"It will returnFalseIf a case-insensitive match is required, it may be necessary to convert the string to uppercase or lowercase before the judgment. - Array elements match exactly:For array types,
containThe elements must be an exact match, no fuzzy matching will be performed. - Key name/field name matching:For key-value pairs or structures, it checks the key name or field name, not the value.
containThe filter is a powerful and flexible tool in the Anqi CMS template.Mastery of its usage can help us write more dynamic, adaptable, and concise templates, thereby significantly improving the management efficiency and user experience of website content.
Frequently Asked Questions (FAQ)
containIs the filter case sensitive?Yes,containThe filter is case-sensitive when matching. For example,{{"AnQiCMS"|contain:"anqicms"}}It will returnFalseIf you need a case-insensitive match, consider performingcontaina comparison after converting the string to a uniform case (for example, all to lowercase) before comparing.except
containWhat similar filter functions can be used to check the content of Anqi CMS template?Anqi CMS template also providescountFilters andindexfilter.countUsed to calculate the number of times a keyword or element appears in a string or array, andindexReturns the first occurrence position of a keyword or element (if not found, returns -1). Both return numbers, different fromcontainReturning a boolean value, you can choose to use it according to your specific needs.containCan the filter be used to check if the 'value' of a custom field contains a certain keyword?containThe filter cannot be used directly to check if the value of a custom field contains a specific keyword unless thevalue itself is a string. It is mainly used for:- Check if the string itself contains a substring.
- Check if an element exists in the array.
- Check if a key-value pair or structure contains a certain "key name" or "field name". If you want to check if the value of a custom field contains a specific keyword, you need to first get the value of that custom field and then use that value as
containThe input of the filter. For example,{{ archive.CustomFieldValue | contain:"关键词" }}.