In Anqi CMS template development, we often need to process and analyze various data of page content.Among them, counting the number of times a specific keyword appears in a string is a very practical need.This not only helps content operators understand the density of keywords, thus optimizing SEO, but also provides data support in some function displays, such as showing how many times a certain feature has been mentioned.
The AnqiCMS template engine provides a rich set of filter (Filters) functions, which act like data processing tools, allowing variables to be formatted, converted, or calculated. When dealing with strings, there is a filter namedcountThe filter, which can accurately meet our needs for counting the frequency of keyword occurrences.
Core tool:countDetailed analysis of the filter
countThe filter is a functional and powerful tool that can calculate the number of times a specified keyword appears in the target string or array.
Basic syntax:
{{ 目标对象 | count:"关键词" }}
Here目标对象Can be a string (such as article content, title, description), or an array (such as a list of words generated bysplita filter). And关键词The text or words you want to count.
Parameter description:
目标对象:The original text or data set to be searched."关键词":The string you want to search and count in the target object. Please note that keywords must be enclosed in double quotes.
Next, let's look at through specific practical scenarios.countHow filters are applied.
Practical Application One: Counting keywords in article content.
Suppose we are editing an article detail page about “AnQiCMS”, and we hope to count how many times the word “AnQiCMS” appears in the article body.
Get article content:In the template of the article detail page, we can use
archiveDetailtags to get various fields of the current article, including the main content (Content)apply
countto count with filters:The article content obtained ascountThe target object of the filter, and specify the keywords to be counted.
Code example:
{# 假设我们正在一个文章详情页的模板中 #}
{# 首先,获取文章正文内容。这里假设文章内容变量为 articleContent #}
{% archiveDetail articleContent with name="Content" %}
<p>文章内容中 "AnQiCMS" 出现的次数:
<strong>{{ articleContent | count:"AnQiCMS" }}</strong>
</p>
{# 如果需要统计其他关键词,如“Go语言” #}
<p>文章内容中 "Go语言" 出现的次数:
<strong>{{ articleContent | count:"Go语言" }}</strong>
</p>
Effect description:When the page is rendered,{{ articleContent | count:"AnQiCMS" }}it will traversearticleContentVariable text, and calculate how many times the keyword "AnQiCMS" appears, then display the specific number.
Practical Application Two: Counting Keywords in a Data List
Sometimes, our data may not be a whole paragraph of text, but a list of multiple words, such as bysplitThe filter splits a sentence into multiple words. In this case,countThe filter is also applicable, but it is necessary to pay attention to its matching mechanism.
Prepare the data list:We use
splitThe filter splits a string into an array using a specified delimiter (such as a space or comma).apply
countto count with filters:Use the generated array ascountthe target object of the filter. It should be noted that,countThe filter will match the keyword with the elements in the array "exactly equal" and cannot be a partial match.
Code example:
{# 假设有一个包含多个词语的字符串 #}
{% set textString = "AnQiCMS 是一个内容管理系统 AnQiCMS 简单高效 AnQiCMS" %}
{# 使用 split 过滤器将其按空格拆分成数组 #}
{% set wordList = textString | split:" " %}
<p>在单词列表中,"AnQiCMS" 出现的次数:
<strong>{{ wordList | count:"AnQiCMS" }}</strong>
</p>
{# 尝试统计“系统”,注意这里将“管理系统”拆分成了“管理”和“系统” #}
<p>在单词列表中,"系统" 出现的次数:
<strong>{{ wordList | count:"系统" }}</strong>
</p>
{# 如果是中文词语,直接作为字符串统计会更灵活,因为 split 按空格分割中文词语不总是理想 #}
<p>直接在原始字符串中统计“管理系统”的次数:
<strong>{{ textString | count:"管理系统" }}</strong>
</p>
Effect description:The first count will accurately give the number of times “AnQiCMS” appears as an independent element inwordListthe array. The second count of “system” may be based onsplitThe way to give the result. The last example illustrates that when dealing with Chinese phrases, using the original string directlycountmay be more in line with expectations becausesplitThe filter is set to split by spaces by default, while Chinese words are usually not separated by spaces.
Tip: Handle case sensitivity issues
countThe filter is case-sensitive when performing keyword statistics.This means that “AnQiCMS” and “anqicms” are considered two different keywords.If your statistical needs do not distinguish between case, you can use the applicationcountbefore the filter.lowerorupperThe filter converts the target string to lowercase or uppercase uniformly.
Code example:
{% set content = "AnQiCMS 是一个强大的 CMS,anqicms 值得体验。" %}
<p>区分大小写时,"AnQiCMS" 出现的次数:
<strong>{{ content | count:"AnQiCMS" }}</strong>
</p>
<p>不区分大小写时,统计 "anqicms" 出现的总次数:
<strong>{{ content | lower | count:"anqicms" }}</strong>
</p>
By flexible applicationcountFilter, you can easily implement various keyword statistics requirements in the AnQiCMS template, providing more possibilities for website content display and analysis.
Frequently Asked Questions (FAQ)
1.countDoes the filter support regular expression matching?
Answer: Currently, in the AnQiCMS template,countThe filter does not support regular expression matching. It will directly match the 'keyword' you provide as a plain string.If you need more complex pattern matching, you may consider processing in the backend (such as the logic layer of Go language) before the data enters the template.
How to count the total number of times a keyword appears in all articles on a website?
Answer:countThe filter is mainly used to count keywords in a single string or array.To count the total number of keyword occurrences in all articles on the entire website, this is not a task that can be completed directly and efficiently at the template level.The template is mainly used for data display and rendering. This kind of global data aggregation and statistics usually needs to be completed in the AnQiCMS backend logic or through other data analysis tools.You can consider developing a custom backend interface, or using the AnQiCMS data export function, and then analyzing it with an external tool.
3. What is a more efficient way to count the occurrences of multiple different keywords?
Answer: In the template, if you need to count the occurrences of multiple different keywords, the most direct method is to call for each keyword separatelycounta filter. For example:{{ content | count:"关键词A" }}and{{ content | count:"关键词B" }}The template engine will execute these calculations separately. Although this will increase the burden on template parsing, it usually will not cause obvious performance problems for the limited keyword statistics on a single page.If you need to count a large number of keywords, and these keywords are dynamically changing, it is also recommended to preprocess them on the backend and pass the statistics directly to the template.