How to determine whether a line of text string contains a certain keyword in Anqi CMS template?
contain
The 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
contain
How to use filters:
{{obj|contain:keyword}}
For example, judgment欢迎使用安企CMS(AnQiCMS)
Is it included?CMS
, you can write it like this:
{{"Welcome to use AnQiCMS (AnQiCMS)"|contain:"CMS"}} # Display results True
contain
Filters also support use in assignment labels (sets), storing the judgment results into a variable, such as:
{% set source = "Welcome to AnQiCMS (AnQiCMS)" %} {% set isContain = source|contain:"CMS" %} {% if isContain %} {{source}} contains "CMS" {% endif %} # Show results Welcome to AnQiCMS (AnQiCMS) contains "CMS"
Sample Demo
Determine whether a line of text contains a certain keyword
Direct output result:
{{"Welcome to use AnQiCMS (AnQiCMS)"|contain:"CMS"}} # Display results True
Assign the result to a variable for other conditions to judge:
{% set source = "Welcome to AnQiCMS (AnQiCMS)" %} {% set isContain = source|contain:"CMS" %} {% if isContain %} {{source}} contains "CMS" {% endif %} # Show results Welcome to AnQiCMS (AnQiCMS) contains "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:"Anqi CMS"}} # Show results True
Assign the result to a variable for other conditions to judge:
{% set isContain = values|contain:"Anqi CMS" %} {% if isContain %} {{values|join:","}} contains "Anqi CMS" {% endif %} # Display results Anqi CMS, AnQiCMS, content management system, free website building system, free template contains the value of "Anqi 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"}} # Show result True
Assign the result to a variable for other conditions to judge:
{% set isContain = values|contain:"Title" %} {% if isContain %} {{values|stringformat:"%#v"}} contains a key name called "Title" {% endif %} # Display result {Title:"AnQiCMS",Keyword:"AnQiCMS",Description:"Free website building system"} contains a key name called "Title".