In the template development of AnQiCMS, we often need to handle the content on the page in various flexible ways.For example, you may need to analyze the frequency of a specific word in an article, or check how many times an element is mentioned in a list of data.The powerful template engine of Anqi CMS provides a variety of practical filters (Filter), which can help you easily achieve these requirements.Among them, the function used to calculate the total number of times a specific keyword or element appears is exactly the focus of our discussion today.

Core Function:countFilter Explanation

In AnQiCMS template, to calculate the total number of times a specific keyword appears in a string or an array in a line, you can directly usecountFilter. This filter is powerful and easy to use, meeting a variety of your needs in content statistics and conditional judgment.

countThe working principle of the filter:

WhencountThe filter applies to a string, it will match exactly and count the number of occurrences of the keyword in the string. If applied to an array (or a slice in Go language)sliceIt will count the number of elements in the array that are "exactly equal" to the specified keyword.

Basic syntax:

{{ obj|count:"关键词" }}

Among them:

  • objRepresents the string variable or array variable you want to count.
  • "关键词"It is the specific string or element whose occurrence frequency you want to count.

It will return an integer value representing the total number of times the keyword appears.


Scene one: Count the number of occurrences of keywords in a string

Imagine you are optimizing the website's SEO and need to understand the density of a core keyword in the article content, or you want to dynamically adjust the page layout or display hints based on the number of times a word appears in the content. At this time,countThe filter can be put to good use.

For example, we have an article title describing an "Enterprise CMS

{# 假设有一个字符串变量 `articleTitle` 的值为 "欢迎使用安企CMS(AnQiCMS)" #}
{% set articleTitle = "欢迎使用安企CMS(AnQiCMS)" %}
<p>在标题 "{{ articleTitle }}" 中,"CMS" 出现了 {{ articleTitle|count:"CMS" }} 次。</p>

Show results:

在标题 "欢迎使用安企CMS(AnQiCMS)" 中,"CMS" 出现了 2 次。

From this example, we can see,countThe filter will find all substrings that match the specified keyword exactly and count them.This is very useful for keyword density analysis, content relevance assessment, or when automated processing is needed in content operation based on the frequency of specific vocabulary mentions.


Scenario two: Count the number of occurrences of a specific element in an array

Except for strings,countThe filter can also handle array data.When dealing with user-submitted tag lists, filter conditions, or when calculating the distribution of a certain attribute in a dataset, counting the occurrences of a specific element in a statistical array is a common requirement.

It should be noted that whencountWhen the filter is applied to an array, it requires the elements in the array to match the "keyword" you specify,Exact match.

For example, suppose we have an array that issplitFilter the words split from the string, we want to know how many times the word 'the' appears:

{# 假设有一个字符串,通过 `fields` 过滤器将其按空格拆分为数组 #}
{% set sentenceWords = "splits the string 安企CMS"|fields %}
<p>在单词数组 "{{ sentenceWords|join:", " }}" 中,"the" 出现了 {{ sentenceWords|count:"the" }} 次。</p>
<p>在单词数组 "{{ sentenceWords|join:", " }}" 中,"安企" 出现了 {{ sentenceWords|count:"安企" }} 次。</p>

Show results:

在单词数组 "splits, the, string, 安企CMS" 中,"the" 出现了 1 次。
在单词数组 "splits, the, string, 安企CMS" 中,"安企" 出现了 0 次。

From the second example, it can be seen that although the original string contains '安企CMS', when it isfieldsFilter split,“AnQi CMS” becomes an independent element.If you only search for 'anqi', since there are no completely matching independent elements, the count result will be 0.countThe 'complete match' principle of the filter in array scenarios.

Points to note

  • Case sensitive: countThe filter performs an exact match, so it is case-sensitive by default.For example, “CMS” and “cms” are considered as two different keywords.If you need to perform case-insensitive statistics, you may need to convert both the source string and the keywords to the same case (such as lowercase) before counting, but this requires combining with other filters to implement, as there is no direct support for such conversion in the default template tags of Anqi CMS.
  • Exact match:Whether it is a string or an array,countthe filter requires an exact match. For strings, it is an exact substring match; for arrays, it is an exact element match.
  • Combine with other filters:As shown in the above example, you cansplitorfieldssuch as other filters andcountThe filter is combined to split a longer string into an array first, and then count the elements in the array.
  • Performance considerations:For frequent statistics of extremely long strings or large arrays, although Go language excels in backend performance, overly complex template logic may still have a slight impact on page rendering speed.In most conventional usage scenarios, there is no need to worry excessively.

By skillfully applyingcountFilter, you will be able to handle and analyze data more efficiently in AnQiCMS templates, providing strong support for the dynamic display and refined operation of website content.


Common Questions (FAQ)

1.countDoes the filter distinguish between uppercase and lowercase letters when counting keywords?Yes,countThe filter is case-sensitive by default.For example, if you search for “CMS”, it will not count the occurrences of “cms” or “Cms”.If you need to perform case-insensitive statistics, you may need to first convert both the text content and the keywords you want to count into a unified case format, which requires custom template functions or backend processing.

2. How to count the total number of occurrences of multiple different keywords in a long string? countThe filter can only count the occurrences of a specific keyword at a time. If you need to count multiple different keywords, you can call for each keyword separately.countThe filter, then add up the results. For example:{{ (articleContent|count:"关键词A") + (articleContent|count:"关键词B") }}.

3. If I only want to check if a keyword exists in a string or array, rather than count the specific number of times, is there a more concise method?Of course. AnQiCMS providescontaina filter to determine if a keyword exists. If you just need to know if a certain keyword exists, use{{ obj|contain:"关键词" }}it will be more efficient and concise, and it will returnTrueorFalse.