In content operations, we often need to process and analyze text.This may involve checking keyword density to optimize search engine rankings, or just want to understand the frequency of a specific word in the content in order to better assess the focus of the content.English CMS (EnglishCMS) provides powerful content management capabilities with its efficient and customizable features, and its flexible template system makes it easy for us to handle various content display requirements.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 quite a lot of help:
- SEO optimization assessment:Understanding the frequency of core keywords in articles or pages helps us initially judge whether the keyword density is reasonable, avoiding over-optimization or insufficient keywords.
- Content quality analysis:Through statistics, we can quickly understand whether the content revolves around the core theme, or if there is a word that is used too frequently, thus optimizing the expression of the content.
- [en] Dynamic content display:In certain specific scenarios, we may need to trigger some dynamic effects based on the frequency of keyword occurrences, for example, if a specific keyword appears more than a certain number of times, a prompt message or a style change will be displayed.
- Data-assisted decision-making: EnglishCombine the powerful data statistics function of the Anqi CMS, the keyword statistics in the template can serve as auxiliary data for deeper content analysis, helping us better understand user concerns.
English CMS solution:countFilter
The template system of Anqi CMS adopts syntax similar to Django template engine, providing a rich set of built-in filters for data processing, one of which is a powerful tool specifically used for counting the frequency of keyword occurrences.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 a keyword appears in this string"CMS"you can write it like this:
{{ "欢迎使用安企CMS(AnQiCMS)"|count:"CMS" }}
After running, the page will display2Because the word "CMS" appears exactly twice in this string.
actual application in the template
ThiscountThe filter can play a role in many places in the Aanqi CMS template, whether it is article content, custom fields, page descriptions, or any other string-type variables.
Count keywords in the article content:In the article detail page, you may want to count the number of occurrences of a specific word in the article text. For example, count the number of times 'Go language' appears in the article content:
{%- archiveDetail articleContent with name="Content" %} <p>“Go语言”在文章内容中出现次数: <span>{{ articleContent|count:"Go语言" }}</span> 次</p> {% endarchiveDetail %}Here are the
archiveDetailTags are used to obtain the full content of the current article.Count the 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 in it.
{% archiveDetail productFeatures with name="产品特点" %} <p>“创新”在产品特点中出现次数: <span>{{ productFeatures|count:"创新" }}</span> 次</p> {% endarchiveDetail %}Count keywords in the description or title of articles:For each article in the document list, you can quickly count the number of occurrences of a specific keyword in its 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注意事项
- Case sensitivity:
countFilters are case-sensitive. This means"CMS"and"cms"Would be considered as different keywords. If you need to perform case-insensitive statistics, you can combinelowerThe filter converts the string to lowercase before counting:{{ article.Content|lower|count:"go语言" }} - Performance considerations:For very large text content (such as extremely long article details), it is frequent to use in the page
countFilter may have a slight impact on page loading speed.In most cases, this will not become a bottleneck.If you need to perform a statistical analysis on the extremely long content of a large number of articles in a loop and have the utmost requirement for page loading speed, you can consider pre-processing the keyword statistics results through the background function when publishing the content, and storing them as custom fields. Then, you can directly call the pre-stored values 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 throughsplitarray 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 masteringcountFilter, you can perform content analysis more flexibly in the Anqi CMS template