Count the occurrences of a specific keyword in a line string and in an array

How to calculate the number of times a keyword appears in a string or array in AnQi CMS template?

countThe filter can calculate the number of times a keyword appears in a line of a string or an array (array/slice).

Usage method

countHow to use the filter:

{{obj|count:关键词}}

For example, calculate欢迎使用安企CMS(AnQiCMS)in the calculationCMSThe count appears, it can be written like this:

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

Example Demonstration

Get the count of a keyword in a line of text

Directly output the result:

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

Get the count of a keyword in an array

The array needs the keyword to be equal to the value of the array or the index, it cannot be a partial match.

{% set values = "splits the string 安企CMS"|fields %}
{{values|count:"the"}}
# 显示结果
1
{% set values = "splits the string 安企CMS"|fields %}
{{values|count:"安企"}}
# 显示结果
0