In the template development of Anqi CMS, we often need to perform various data processing and analysis on page content.Among them, counting the number of occurrences of a specific keyword in a string is a very practical need.This not only helps content operators understand the keyword density, thus optimizing SEO, but also provides data support for certain feature displays, such as showing how many times a certain feature is mentioned.
The template engine of AnQi CMS provides rich filter (Filters) functions, which act like mini data processing tools, capable of formatting, transforming, or calculating variables. When dealing with strings, there is a filter namedcountThe filter, which can accurately meet our needs to count the frequency of keywords.
core tools:countDetailed Analysis of the Filter
countThe filter is a functionally intuitive and powerful tool that can calculate the number of times a specified keyword appears in the target string or array.
Basic syntax:
{{ 目标对象 | count:"关键词" }}
Here are the目标对象Can be a string (such as article content, title, description), or an array (for example, a list of words generated by a filter). Andsplita filter). And关键词It is the specific text or word you want to count.
Parameter description:
目标对象:The original text or dataset to be searched."关键词":You expect to find and count the string in the target object. Please note that keywords need to be enclosed in double quotes.
Next, let's see through specific practical scenarios,countHow filters are applied.
Practical Application One: Counting keywords in article content.
Assuming we are editing a detail page of an article about "AnQiCMS", 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
countStatistics with the filter:The article content obtained will becountthe 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 explanation:when the page is rendered,{{ articleContent | count:"AnQiCMS" }}It will traversearticleContentall the text in the variable, 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 applies similarly, but attention should be paid to its matching mechanism.
Prepare the data list:We use
splitThe filter splits a string into an array according to a specified delimiter (such as a space or comma).Apply
countStatistics with the filter:Use the generated array ascountthe target object of the filter. It should be noted that,countThe filter performs a match in the array and requires the keyword to be "exactly equal" to the elements in the array, and not 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 explanation:The first count will accurately give the number of times “AnQiCMS” appears.wordListThe second count for “system” may vary depending on the situation.splitresults are presented in the way. The last example illustrates that using the original string on Chinese phrasescountmay be more in line with expectations,splitThe filter defaults to splitting by spaces, while Chinese words are usually not separated by spaces.
Tip: Handle case sensitivity issues
countThe filter distinguishes between uppercase and lowercase letters when performing keyword statistics.This means that "AnQiCMS" and "anqicms" are considered two different keywords.countUse it before the filterlowerorupperThe filter converts the target string to uppercase or lowercase.
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 using flexibilitycountFilter, you can easily implement various keyword statistics requirements in the AnQiCMS template, providing more possibilities for the display and analysis of website content.
Common Questions (FAQ)
1.countDoes the filter support regular expression matching?
Answer: Currently in the AnQiCMS template,countFilter 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 need to consider processing it on the backend (such as the logic layer in 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 across 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 global data aggregation and statistics usually need to be completed in the backend logic of AnQiCMS or through other data analysis tools.You can consider developing a custom backend interface, or use the data export function of AnQiCMS, and then analyze it with an external tool.
3. If I need to count the occurrences of multiple different keywords, what is a more efficient method?
Answer: The most direct method to count the occurrences of multiple different keywords in a template is to call for each keyword separately.countfilter. For example:{{ content | count:"关键词A" }}and{{ content | count:"关键词B" }}.The template engine will execute these calculations separately.Although this may increase the burden of template parsing, for the limited keyword statistics on a single page, it usually does not cause obvious performance issues.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 results directly to the template.