In AnQiCMS template development, we often need to flexibly handle content on the page.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.The powerful template engine of AnQi CMS provides a variety of practical filters (Filter) that can help you easily meet these needs.The function to calculate the total number of times a specific keyword or element appears is exactly the focus of our discussion today.

Core function:countFilter Details

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

countThe principle of operation of the filter:

WhencountThe filter is applied to a string, it will precisely match and count the number of times the keyword appears 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.
  • "关键词"Is the specific string or element you want to count the occurrence of.

It will return an integer value indicating the total number of times the keyword occurs.


Scene one: Count the occurrence of keywords in a string

Imagine you are optimizing the SEO of a website, and you 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 point,countThe filter can be put to good use.

For example, we have an article title describing an enterprise CMS and we want to count how many times the word “CMS” appears:

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

Display result:

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

This example shows thatcountThe 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 content operation requires automated processing based on the frequency of specific vocabulary mentions.


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

Besides strings,countThe filter can also handle array data. When dealing with user-submitted tag lists, filter condition selections, or when calculating the distribution of a certain attribute in a dataset, counting the frequency of specific elements in the array is a common requirement.

It should be noted that whencountThe filter is applied to the array, it requires the elements of the array to match the specified “keyword”Perfect match.

For example, assume we have a throughsplitThe filter splits the string into an array of words, how many times does the word 'the' appear:

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

Display result:

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

It can be seen from the second example that although the original string contains "安企CMS", when it isfieldsAfter splitting the filter, "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.This emphasizescountThe "exact match" principle of the filter under the array scenario.

Precautions for use

  • Case Sensitive: countThe filter performs an exact match, so it is case-sensitive by default.For example, "CMS" and "cms" are considered two different keywords.If you need to perform a case-insensitive count, you may need to convert the source string and keywords to a uniform case (such as lowercase) before counting, but this requires combining with other filters to implement, and there is no direct support for this conversion filter 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 can use:splitorfieldsAnd other filters with:countCombine filters to split a long string into an array first, and then count the elements in the array.
  • Performance consideration: For frequent statistics of extremely long strings or large arrays, although Go language is excellent in backend performance, but overly complex template logic may still have a slight impact on the speed of page rendering.In most routine usage scenarios, there is no need to worry too much.

By mastering the use ofcountFilter, you will be able to process and analyze data more efficiently in the AnQiCMS template, providing strong support for the dynamic display and refined operation of website content.


Frequently Asked 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 convert both the text content and the keywords you want to count to a uniform case format, but this requires a custom template function or backend processing.

How to count the total 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.countThen add the results of them. For example:{{ (articleContent|count:"关键词A") + (articleContent|count:"关键词B") }}.

3. If I only want to check if a keyword exists in a string or array instead of counting the specific number of times, is there a more concise way?Of course, AnQiCMS provides.containThe filter is used to determine if it exists. If you just need to know if a certain keyword exists, use.{{ obj|contain:"关键词" }}It will be more efficient and concise, it will return.TrueorFalse.