In the daily work of content operation, we often need to judge and display according to the 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 a product description includes some feature, we want to adjust its display style accordingly.The template engine of AnQiCMS (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 achieve such judgment, Anqi CMS mainly provides two powerful mechanisms:containFilters andinoperators, each of which is suitable for different scenarios.

Core tool:containFilter

containThe filter is a tool specifically used in Anqi CMS templates to check if 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 in arrays, maps (map), and even structs (struct)When it finds a match, it will return a boolean value (TrueorFalseThis makes it very suitable for use with conditional judgments.

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

1. Determine if the string contains the keyword:

This is the most common application scenario. If you have a piece of text, 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 use AnQiCMS (AnQiCMS) to build your website.”, then the above code will output “AnQiCMS is mentioned in the article!”.

2. Check if an element exists in an array:

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

{% 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 includes 'website construction'.". It is noteworthy that,containThe filter performs elementsPerfect match.

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

containFilters can also be used to check if a specific key name (field name) exists in a map or structure. This is very useful for checking if a certain attribute 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 whetherpricethe key name exists.

a more general judgment method:inOperator

exceptcontainFilter, the Anqi CMS template engine also supportsinoperator, which is usually used directly forifIn the statement, determine whether a value is in another collection (such as a string or array).inThe operator provides a more concise syntax, especially for direct condition judgments.

1. Check if a string contains a substring:

withcontainSimilar filter,inThe operator 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 determine whether an element exists in an array.

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

inOperator andcontainFilter selection:

  • If you just want toifMake a simple '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 because it returns a boolean value.

It includes not only: digging deeper into the content

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

1.indexFilter: locate the position of keyword occurrence

If you want to know not only whether it contains but also the position of the first occurrence of the keyword, you can useindexA filter. It returns the index of the first occurrence of the keyword (starting from 0), or returns 0 if not found-1. This can be used as another way to judge whether it contains (i.e.index >= 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 by treating a Chinese character as multiple byte lengths (a Chinese character usually occupies 3 bytes under UTF-8 encoding).

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 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 scenario: Make content smarter

  • Conditionally display a specific block: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 checking for the existence of keywords, special styles can be added to pages or list items that contain specific keywords.
  • Adjust SEO information according to content:Although TDK tags are usually set in the background, in some advanced template scenarios, it can be determined whether to adjust 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 precise location or frequency statistics, it can be flexibly implemented at the template level to help content operators more efficiently control and display website content.


Frequently Asked Questions (FAQ)

Q1:inAnd operatorscontainWhat are the core differences in the usage of filters?

A1:inThe operator is directly used in the Anqi CMS template toifthe logical judgment operator of statements, for example{% if "关键词" in 变量 %}, it cannot be chained, and cannot assign the judgment result to a new variable. AndcontainA filter is a functional operation that returns a Boolean value, which can be chained with other filters or used directly{% set is_contained = 变量 | contain: "关键词" %}This way assigns the result to a new variable for use in subsequent template logic. In simple terms,inis more suitable for direct conditional judgment,containIt is more suitable for scenarios where you need to obtain boolean results and proceed further.

Q2:containCan the filter determine whether an element in the arraysectioncontains? For example, to judge["Apple", "Banana"]if there is an element containing “an”?