During the process of building a website with AnQiCMS, flexibly displaying information based on content characteristics is the key to enhancing user experience and operational efficiency. To achieve this, the system incorporates many practical template filters, includingcontainThe filter is a powerful tool for determining whether content contains specific keywords.
It can help you match content directly at the template level without modifying the backend code, thus making your website content display more intelligent and personalized.
What iscontainFilter?
containThe filter is a very practical built-in feature in the AnQiCMS template system, which can judge whether a string, array (usually slice in Go language), key-value pair (Map), or structure (Struct) contains the specific keyword you are looking for.
Its core function is to perform an 'inclusiveness' check. When the target content indeed contains the keywords you specify,containthe filter will return a boolean valueTrueThe opposite is, if the keyword is not found, it returnsFalse. This simple and clear return mechanism allows you to easily make various conditional judgments and logical controls in the template.
Basic Usage: Check if a string contains a keyword
The most common scenario is to determine whether a keyword exists in a text string. For example, you may want to check if a hot topic is mentioned in the article title or abstract.
Do we have a description of an enterprise CMS, and do we want to know if it mentions 'CMS':
{# 直接判断字符串中是否包含“CMS” #}
<p>这段文字是否包含“CMS”?
{% if "欢迎使用安企CMS(AnQiCMS)"|contain:"CMS" %}
<span>是,包含!</span>
{% else %}
<span>否,不包含。</span>
{% endif %}
</p>
This code executes, and the page will display 'Yes, it includes!' because the string indeed contains the word 'CMS'. It is worth noting that, containThe filter is case-sensitive. If you search 'cms', the results may beFalsedependent on the specific wording of the original text.
Combinesettags, to achieve more flexible logical control
In practical applications, we usually do not write long strings directly in conditional statements. It is more common to put the content of the variable to be checked orcontainThe results of the filter are stored so that they can be used in complex template logic. At this point,setthe tags come into play.
For example, we want to display a special tag based on whether the article title contains 'tutorial':
{# 假设archive.Title是当前文章的标题 #}
{% set article_title = archive.Title %}
{% set is_tutorial = article_title|contain:"教程" %}
<h1 class="article-title">{{ article_title }}</h1>
{% if is_tutorial %}
<span class="tag-tutorial">【教程】</span>
{% endif %}
<p>文章内容...</p>
Passsetthe tag, we willcontainassign the judgment result tois_tutorialVariables, so that this judgment result can be reused anywhere in the template without having to recalculate it each time.
Advanced Application: Determine whether an array or slice contains a specific value
containThe power of the filter is not limited to strings.When you need to determine whether a collection (such as the array or slice commonly used in AnQiCMS) contains a specific element, it can also be effective.
Suppose your article has multiple tags (Tag) stored in an array, and you want to check if it contains the tag 'SEO optimization':
{# 假设tags_list是通过|list过滤器从字符串转换而来的数组,或者直接是后端传来的数组 #}
{% set tags_list = '["安企CMS", "内容管理", "SEO优化", "多站点管理"]'|list %}
<p>文章标签列表:
{% for tag in tags_list %}
<span class="article-tag">{{ tag }}</span>
{% endfor %}
</p>
{% if tags_list|contain:"SEO优化" %}
<p class="seo-info">这篇文章与SEO优化相关,快来学习吧!</p>
{% else %}
<p>这篇文章不包含SEO优化标签。</p>
{% endif %}
In this case,containThe filter will check each element in the array one by one to see if there is a value that matches "SEO optimization" exactly.
Further: Determine if a specific key name exists in the key-value pair or structure.
In addition to strings and arrays,containThe filter can also handle more complex data types, such as key-value pairs (map) or structures (struct) in the Go language. However, there is an important detail to note in this scenario:containFilter is to determine whether these data structures containa key (key) with a specified name instead of determining whetherValueit exists in the corresponding values of these keys.
For example, you have a product detail key-value pair and you want to check if it contains the key "price" to decide whether to display price information:
{# 假设product_details是一个键值对或结构体 #}
{% set product_details = {'name': '安企CMS企业版', 'price': 999, 'features': ['多站点', 'SEO']} %}
<p>产品名称:{{ product_details.name }}</p>
{% if product_details|contain:"price" %}
<p>产品价格:<strong>¥{{ product_details.price }}</strong></p>
{% else %}
<p>该产品暂无公开价格。</p>
{% endif %}
Here,product_details|contain:"price"will checkproduct_detailsif there is a key namedpriceThe key. If it exists, it is returned.TrueDisplays the price. This is very useful for dynamically displaying different attributes of different types of products or content.
Practical application examples: make website content smarter
containThe flexible use of filters can bring many practical benefits:
- Dynamic recommendations and identificationIf the article title contains keywords such as "hot
- Personalized Content DisplayAccording to the name of the article category, dynamically load different advertisements, recommendation modules, or styles.
- User permission control (combined with user group tags):If you use AnQiCMS user group management, you can judge whether the current user's tag (Tag) contains 'VIP', so as to decide whether to display VIP exclusive content or download links.
- Highlight and filter contentIn the search results page, if the search term appears in the article abstract, it can be highlighted; or judge whether the content attribute contains the corresponding value according to the user's selected filter conditions.
- Template style dynamic switching:Some page elements may need to adjust their CSS styles based on whether their title contains specific words, such as navigation items with the title containing 'contact', for which a special icon is added.
Whether it is simple text matching, or complex array and data structure judgment,containThe filters, with their intuitive usage, bring great flexibility and control to the presentation of AnQiCMS content. Mastering it will make your website operation more efficient and intelligent.
Common Questions (FAQ)
1.containDoes the filter distinguish between uppercase and lowercase?Yes,containThe filter is case-sensitive when performing keyword matching.For example, if you search for “CMS”, then “cms” or “Cms” will not be matched.lowerorupperThe filter converts the string to uppercase or lowercase before matching.
2.containCan the filter determine if a key-value pair (map) or a structValueExists?cannot.containThe filter is used when dealing with key-value pairs or structures, it will only judge whether the specifiedkey name (key)And it will not go to check whether the values corresponding to these keys contain a specific content. If you need to check whether a value exists in a structure or a field of a key-value pair, you need to get the value of the field first, and then use that valuecontainfilter. For example:{{ product_details.name|contain:"企业" }}.
3. If I want to judge multiple keywords,containHow to use the filter?If you need to determine whether the target content contains multiple keywords ofany of the following(OR logic), orallKeywords (AND logic), can be combined with multiplecontainfilters andifLogical judgments can be achieved.
- Example of OR logic:
{% if article.Title|contain:"教程" or article.Title|contain:"指南" %} - Example of AND logic:
{% if article.Title|contain:"安企" and article.Title|contain:"教程" %}You can apply them flexibly according to your actual needsand/orThese logical operators are used to build more complex judgment conditions.