Determine if the text, 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 whether a keyword is included in a line of string, array (slice), key-value pair (map), or struct, and the result will return a boolean value (bool).

Usage method

containHow to use the filter:

{{obj|contain:关键词}}

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

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

containThe filter also supports using assignment tags (set) to store judgment results in a variable, such as:

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

Example Demonstration

Determine if a line of text contains a specific 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 if 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"的值。

Determine if a key-value pair (map), structure (struct) contains a specific keyword 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"的键名。