In the daily work of content operation, we often need to make judgments and displays based on specific information of the content.For example, if a blog post mentions a hot keyword, we may want to give it a special tag; or if a product description includes certain features, we want to adjust its display style accordingly.The template engine of AnQiCMS provides a flexible and intuitive way to determine whether a string or array contains specific content, making dynamic content display and personalized processing very convenient.

To implement such judgment, AQ CMS mainly provides two powerful mechanisms:containfilters andinoperators, each suitable for different scenarios.

core tools:containFilter

containFilter is a tool specifically used in the Anqi CMS template to check if the content contains specific keywords.Its strength lies in its ability to be used not only for strings, but also to effectively check for the existence of specified content within arrays, maps (map), and even structs (struct).TrueorFalseThis makes it very suitable for use with conditional judgments.

containThe basic usage of the filter is{{对象 | contain: "关键词"}}.

1. Determine if the string contains a keyword:

This is the most common application scenario. If you have a piece of text content, you want to know if it mentions "AnQi CMS":

{% set article_content = "欢迎使用安企CMS(AnQiCMS)来搭建您的网站。" %}
{% if article_content | contain: "安企CMS" %}
    <p>文章中提到了安企CMS!</p>
{% else %}
    <p>文章中没有提及安企CMS。</p>
{% endif %}

Ifarticle_contentThe value of the variable is 'Welcome to AnQiCMS (AnQiCMS) to build your website.' Then the above code will output 'AnQiCMS was mentioned in the article!'.

2. Check if an element exists in an array:

If your variable is an array (usually in Go language templates,}]}slice)containThe filter can check if there is an element that matches the "keyword" exactly in the array.

{% set keywords_list = ["安企CMS", "网站建设", "内容管理"] %}
{% if keywords_list | contain: "网站建设" %}
    <p>关键词列表中包含“网站建设”。</p>
{% else %}
    <p>关键词列表中不包含“网站建设”。</p>
{% endif %}

here, "website construction" is an element in the array, so it will output "The keyword list contains 'website construction'.". It is worth noting that,containThe filter performs an element check when checking the array,Exact match.

3. Determine if a key exists in a map (Map) or a struct (Struct):

containThe filter can also be used to check if a specific key name (field name) exists in a map or a struct. This is very useful for checking if a certain property exists in a data structure.

{% set product_info = {"name": "安企CMS", "version": "3.0", "price": 0} %}
{% if product_info | contain: "price" %}
    <p>产品信息中包含价格字段。</p>
{% else %}
    <p>产品信息中不包含价格字段。</p>
{% endif %}

The code will output “The product information contains a price field.” because it checks whetherpricethis key name exists.

A more general judgment method:inOperator

ExceptcontainFilter, the template engine of the Anzhi CMS also supports:inoperator, which is usually used directly forifIn the statement, judge whether a value is in another collection (such as a string or an array).inThe operator provides a more concise syntax, especially for direct conditional judgments.

1. Check if a string contains a substring:

WithcontainFilter similar,inOperators can directly check if one string contains another substring.

{% set page_title = "关于安企CMS的优势" %}
{% if "CMS" in page_title %}
    <p>页面标题中包含“CMS”。</p>
{% else %}
    <p>页面标题中不包含“CMS”。</p>
{% endif %}

2. Check if an element is in an array:

inThe operator can also be used to check if an element exists in an array.

{% set tag_names = ["建站", "营销", "优化"] %}
{% if "优化" in tag_names %}
    <p>这个标签在列表中。</p>
{% else %}
    <p>这个标签不在列表中。</p>
{% endif %}

inThe operator withcontainFilter selection:

  • If you just want toifPerform a simple "whether contains" judgment in the statement.inOperators are usually more concise and intuitive.
  • If you need to store the result of a judgment in a variable, or chain it with other filters, thencontainthe filter will be a better choice, as it returns a boolean value.

Not only includes: Deeply挖掘内容

In some cases, you may not only want to know if it contains, but also need more detailed information, such as the position or frequency of the keyword.Auto CMS also provides the corresponding filters to meet these needs.

1.indexFilter: Locate the position of keyword occurrence

If you not only want to know if it includes, but also want to know the position of the first occurrence of the keyword, you can useindexFilter. It will return the index position of the first occurrence of the keyword (starting from 0), or return -1 if not found-1. This can be used as another way to determine if it containsindex >= 0).

{% set document_text = "安企CMS是一个高效的内容管理系统,CMS功能强大。" %}
{% set first_cms_pos = document_text | index: "CMS" %}
{% if first_cms_pos >= 0 %}
    <p>“CMS”首次出现在位置:{{ first_cms_pos }}</p>
{% else %}
    <p>未找到“CMS”。</p>
{% endif %}

It should be noted that for strings containing Chinese,indexThe filter calculates the position, and a Chinese character is usually counted as multiple byte lengths (under UTF-8 encoding, a Chinese character usually occupies 3 bytes).

2.countFilter: Count the number of times a keyword appears

If you want to know how many times a keyword appears in a string or an array, you can usecountFilter.

{% set long_text = "安企CMS提供卓越的用户体验,安企CMS致力于服务中小企业。" %}
{% set cms_count = long_text | count: "安企CMS" %}
<p>“安企CMS”在文本中出现了 {{ cms_count }} 次。</p>

Similarly, for arrays,countThe filter counts the number of times elements match completely.

Actual application scenarios: Make content more intelligent

  • Conditionally display specific blocks:If the article content contains the word 'video', a video player area will be displayed.
  • Highlight keywords:Although highlighting directly requires more complex string replacement logic, by determining the existence of keywords, special styles can be added to pages or list items containing specific keywords.
  • Adjust SEO information based on content:Although TDK tags are usually set in the background, in certain advanced template scenarios, you can determine whether it is necessary to adjust the auxiliary Meta tags based on the dynamic content of the page.
  • Form validation information:In some custom forms, you can check if the user input contains sensitive words or other specific patterns.

In summary, Anqi CMS provides a variety of easy-to-use template functions for determining whether a string or array contains specific content.Whether it is a simple boolean judgment or the need for more refined position or frequency statistics, it can be flexibly implemented at the template level, helping content operators to control and display website content more efficiently.


Common Questions (FAQ)

Q1:inOperators andcontainWhat are the core differences in the usage of filters?

A1:in运算符是安企CMS模板中直接用于 Englishif语句的逻辑判断操作符,例如 English{% if "关键词" in 变量 %},它不能被链式调用,也无法将判断结果赋值给新的变量。而 EnglishcontainThe filter is a functional operation, it returns a Boolean value, which can be chained with other filters or called separately.{% set is_contained = 变量 | contain: "关键词" %}This method assigns the result to a new variable for use in subsequent template logic. In simple terms,inIt is more suitable for direct conditional judgment,contain更适合需要获取布尔结果并进行进一步处理的场景。

Q2:contain过滤器能否判断数组中某个元素的Part内容?例如,判断["Apple", "Banana"]中是否有元素包含 “an”?