Check if text or array contains a specified keyword

How to judge whether a line of text string contains a certain keyword in the Anqi CMS template?

containThe filter can determine if a keyword is included in a line of string, array (slice), key-value pair (map), or structure (struct), and the result will return a boolean value (bool).

Usage Instructions

containHow to use the filter:

{{obj|contain:关键词}}

For example, to judge欢迎使用安企CMS(AnQiCMS)Whether it containsCMSthen it can be written like this:

{{"欢迎使用安企CMS(AnQiCMS)"|contain:"CMS"}}
# 显示结果
True

containThe filter also supports using it in assignment tags (set), storing the judgment result in a variable, such as:

{% set source = "欢迎使用安企CMS(AnQiCMS)" %}
{% set isContain = source|contain:"CMS" %}
{% if isContain %}
{{source}}中包含"CMS"
{% endif %}
# 显示结果
欢迎使用安企CMS(AnQiCMS)中包含"CMS"

Here is an example demonstration

Determine whether a line of text contains a certain keyword

Directly output the result:

{{"欢迎使用安企CMS(AnQiCMS)"|contain:"CMS"}}
# 显示结果
True

Assign the result to a variable for other conditional judgments:

{% set source = "欢迎使用安企CMS(AnQiCMS)" %}
{% set isContain = source|contain:"CMS" %}
{% if isContain %}
{{source}}中包含"CMS"
{% endif %}
# 显示结果
欢迎使用安企CMS(AnQiCMS)中包含"CMS"。

Determine whether a keyword value exists in an array

Assume an array is:values = ["安企CMS","AnQiCMS","内容管理系统","免费建站系统","免费模板"].

Directly output the result:

{{values|contain:"安企CMS"}}
# 显示结果
True

Assign the result to a variable for other conditional judgments:

{% set isContain = values|contain:"安企CMS" %}
{% if isContain %}
{{values|join:","}}中包含"安企CMS"
{% endif %}
# 显示结果
安企CMS,AnQiCMS,内容管理系统,免费建站系统,免费模板中包含"安企CMS"的值。

Check if a key-value pair (map), structure (struct) contains a keyword as a key name

Assume a key-value pair is:webInfo = {Title:"安企CMS",Keyword:"AnQiCMS",Description:"免费建站系统"}.

Directly output the result:

{{values|contain:"Title"}}
# 显示结果
True

Assign the result to a variable for other conditional judgments:

{% set isContain = values|contain:"Title" %}
{% if isContain %}
{{values|stringformat:"%#v"}}中包含一个叫"Title"的键名
{% endif %}
# 显示结果
 {Title:"安企CMS",Keyword:"AnQiCMS",Description:"免费建站系统"}中包含一个叫"Title"的键名。