How to get the position of a keyword in a line string or array in the Anqi CMS template?
index
The filter can calculate the position of a keyword in a string or array (array/slice).If a string contains multiple keywords to search for, the index returns the position of the first occurrence.If not found, return -1.Note: If there is Chinese in the string, one Chinese character counts as 3 positions when calculating the position.
Usage Instructions
index
How to use the filter:
{{obj|index:关键词}}
For example, to judge欢迎使用安企CMS(AnQiCMS)
in the calculationCMS
where the keyword appears, you can write it like this:
{{"欢迎使用安企CMS(AnQiCMS)"|index:"CMS"}}
# 显示结果
18
Here is an example demonstration
Calculate the position of a keyword in a line of text
Directly output the result:
{{"欢迎使用安企CMS(AnQiCMS)"|index:"CMS"}}
# 显示结果
18
Calculate the position of a keyword in an array
The value in the array needs to be equal to the key or index of the array, they must match exactly, not partially.
{% set values = "splits the string 安企CMS"|fields %}
{{values|index:"the"}}
# 显示结果
1
{% set values = "splits the string 安企CMS"|fields %}
{{values|index:"安企"}}
# 显示结果
-1