In content operations, we often need to process and analyze various texts.This may involve checking keyword density to optimize search engine rankings, or it may just be to understand the frequency of a specific word appearing in the content in order to better assess the focus of the content.AnQiCMS, with its efficient and customizable features, provides users with powerful content management capabilities, and its flexible template system allows us to easily handle various content display needs.Today, let's talk about a very practical feature: how to count the number of times a keyword appears in a string in a template.
Why do we need to count the number of times a keyword appears?
Counting the number of times keywords appear in a template may sound like a detailed requirement, but in actual operation, it can provide a lot of help:
- SEO optimization assessment:Understanding the frequency of the appearance of core keywords in an article or page helps us initially judge whether the keyword density is reasonable, avoiding over-optimization or insufficient keywords.
- Content quality analysis:By statistics, we can quickly understand whether the content is focused on the core theme or if there is a word used too frequently, thus optimizing the expression of the content.
- Dynamic content display:Under certain specific scenarios, we may need to trigger some dynamic effects based on the number of times a keyword appears, for example, if a certain keyword appears more than a certain number of times, a prompt message or a style change will be displayed.
- Data assists in decision-making:Combining the powerful data statistics function of AnQi CMS, the keyword statistics in the template can be used as auxiliary data for deeper content analysis, helping us better understand user concerns.
The solution of AnQi CMS:countFilter
The AnqiCMS template system adopts a syntax similar to the Django template engine, providing a rich set of built-in filters to process data, one of which is a powerful tool specifically used to count the frequency of keywords.countFilter. Use it to easily calculate the frequency of a keyword in a string.
Its basic usage is very intuitive:
{{ 您的字符串变量|count:"您要统计的关键词" }}
For example, if you want to know"欢迎使用安企CMS(AnQiCMS)"how many times this string contains"CMS"you can write it like this:
{{ "欢迎使用安企CMS(AnQiCMS)"|count:"CMS" }}
After running, the page will display2Because the string "CMS" appears exactly twice in this string.
Actual application in template
ThiscountThe filter can be used in many places in the Anqi CMS template, whether it is article content, custom fields, page descriptions, or any other string variable.
Count keywords in the article content:On the article detail page, you may want to count the number of times a specific word appears in the article text. For example, count the number of times the word "Go language" appears in the article content:
{%- archiveDetail articleContent with name="Content" %} <p>“Go语言”在文章内容中出现次数: <span>{{ articleContent|count:"Go语言" }}</span> 次</p> {% endarchiveDetail %}Here
archiveDetailThe tag is used to get the full content of the current article.Count keywords in the custom field:If your content model defines custom fields, such as a field named "Product Features", you can count the occurrences of specific words within it.
{% archiveDetail productFeatures with name="产品特点" %} <p>“创新”在产品特点中出现次数: <span>{{ productFeatures|count:"创新" }}</span> 次</p> {% endarchiveDetail %}Count keywords in article description or title:For each article in the document list, you can quickly count the number of times a keyword appears in the description or title.
{% archiveList articles with type="list" limit="5" %} {% for article in articles %} <div> <h3>{{ article.Title }}</h3> <p>描述:“安企CMS”出现次数:<span>{{ article.Description|count:"安企CMS" }}</span> 次</p> <p>标题:“安企CMS”出现次数:<span>{{ article.Title|count:"安企CMS" }}</span> 次</p> </div> {% endfor %} {% endarchiveList %}
Some practical tips and precautions
- Case sensitivity:
countThe filter is case-sensitive. This means"CMS"and"cms"It is considered as different keywords. If you need to perform a case-insensitive count, you can combinelowerThe filter converts the string to lowercase before counting:{{ article.Content|lower|count:"go语言" }} - Performance consideration: For very long text content (such as a detailed article), it is frequently used on the page
countThe filter may have a slight impact on page loading speed.In most cases, this will not become a bottleneck. But if you are counting the ultra-long content of a large number of articles in a loop and have an extreme requirement for page loading speed, you can consider pre-processing the keyword statistics results through the background feature when publishing content, and storing them as custom fields, which can be directly called in the template. - Exact match:
countThe filter will search for string fragments that match the specified keyword exactly.It does not perform fuzzy matching or lemmatization. For example, counting 'apple' in the string 'Apple is an apple' will return 2. - Array statistics:
countThe filter can also be used to count the number of completely matching elements in an array. For example, if you have a throughsplitThe array split from the string:
This code will return:{% set tagsArray = "SEO,Go语言,CMS,Go语言"|split:"," %} <p>“Go语言”在标签数组中出现次数:<span>{{ tagsArray|count:"Go语言" }}</span> 次</p>2.
by masteringcountThe filter allows you to perform content analysis more flexibly in the Anqi CMS template