Determine whether text and array contain specified keywords

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

containThe filter can determine whether a keyword is contained in a line of strings, slices, key-value pairs, and structures. The result will return a boolean value.

How to use

containHow to use filters:

{{obj|contain:关键词}}

For example, judgment欢迎使用安企CMS(AnQiCMS)Is it included?CMS, you can write it like this:

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

containFilters also support use in assignment labels (sets), storing the judgment results into a variable, such as:

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

Sample Demo

Determine whether a line of text contains a certain keyword

Direct output result:

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

Assign the result to a variable for other conditions to judge:

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

Determine whether there is a value of a keyword in an array

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

Direct output result:

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

Assign the result to a variable for other conditions to judge:

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

Determine whether there is a key name for a keyword in a key-value pair (map) or struct (struct)

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

Direct output result:

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

Assign the result to a variable for other conditions to judge:

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