How to count the frequency of a keyword in a string in the AnQiCMS template?

Calendar 👁️ 53

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.

  1. 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 %}
    

    HerearchiveDetailThe tag is used to get the full content of the current article.

  2. 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 %}
    
  3. 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 pagecountThe 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:
    
    {% set tagsArray = "SEO,Go语言,CMS,Go语言"|split:"," %}
    <p>“Go语言”在标签数组中出现次数:<span>{{ tagsArray|count:"Go语言" }}</span> 次</p>
    
    This code will return:2.

by masteringcountThe filter allows you to perform content analysis more flexibly in the Anqi CMS template

Related articles

How to utilize the multi-site feature of Anqi CMS to achieve unified or differentiated display of content between different sites?

The multi-site feature of AnQi CMS is an extremely powerful feature, which provides unprecedented flexibility for website operators, whether it is managing multiple independent brand websites or building a content-sharing matrix, they can handle it with ease.The core of this feature lies in the fact that it allows you to efficiently manage and display multiple independently operated websites under a single secure CMS system, and cleverly balances content differentiation and unity.### Understanding the multi-site capability of AnQi CMS Firstly, we need to clarify the operation mode of the multi-site function of AnQi CMS

2025-11-08

How can you implement a unique display style for content under a specific category in AnQi CMS?

In website operation, we often encounter such needs: a particular content category needs a unique display style to highlight the importance, uniqueness, or brand character of its content.For example, your 'news center' may require a concise list layout, while 'selected cases' may require a more visually striking grid layout or slideshow display.AnQiCMS provides powerful template customization capabilities, making it intuitive and efficient to achieve this goal.Why do you need to customize the display style for a specific category?A unified website style is indeed important

2025-11-08

What ways does AnQi CMS support to individually present the detailed content of a single document?

When using AnQi CMS to manage website content, if you want each document's detailed content to be presented in a unique and eye-catching way, the system provides very flexible customization settings.This is not just about changing the text color or font size, but also about deep customization from content structure to display form.Firstly, the foundation of personalized display lies in **content model and custom fields**.Our AnQi CMS allows us to create different content models based on actual business needs, such as 'articles', 'products', 'news', and even 'cases'.

2025-11-08

How can I design a dedicated display template for the article list in AnQi CMS?

In AnQi CMS, designing a dedicated display template for the article list is a key step to creating a unique website style and optimizing the user experience.AnQi CMS provides us with a great degree of freedom with its flexible content model and powerful template engine (similar to Django syntax), allowing us to present diverse article lists based on different content types, marketing objectives, or user needs.The website's article list page is like a showcase of content, how to arrange and highlight the key points directly affects visitors' browsing interest and conversion effect.A well-designed list template

2025-11-08

How to calculate the total number of occurrences of a specific value in an array in AnQiCMS?

In website operation, we often need to handle various data, one of the common needs is to count the number of times a specific value appears in a data set.For example, you may want to know how many times a keyword appears in an article, or how many colors of a product are marked as 'hot'.For users of AnQiCMS, the system's powerful template engine provides a very convenient tool to complete this task.The AnQi CMS template engine is powerful and flexible, it comes with a variety of filters (Filters)

2025-11-08

What are the practices when using the `count` filter to calculate keyword density?

In website content operation, the reasonable use of keywords is an important factor in improving search engine visibility.We not only need to ensure that the core keywords are reflected in the content, but also need to pay attention to their frequency of occurrence, avoiding overloading or underusing.AnQiCMS provides a variety of powerful content management tools, among which the `count` filter is a very practical feature that can help us effectively count the number of occurrences of keywords in articles.###

2025-11-08

How to judge whether a line of text in the AnQiCMS template contains a certain keyword?

In website operation, we often encounter such needs: to decide how to display page elements based on whether the content contains a specific word, such as highlighting some text, or only displaying a module when certain conditions are met.The AnQiCMS template system provides flexible filter functions to help us easily implement these keyword-based judgments.This article will delve into how to efficiently and accurately determine whether a line of text in the AnQiCMS template contains a certain keyword.### Use `contain`

2025-11-08

How to check if a specific element exists in an array within a template

In the daily operation and template customization of AnQi CMS, we often encounter situations where we need to dynamically adjust the display of the page according to the data content.One common and practical requirement is to determine whether a specific element exists in an array (or list) within a template.This is crucial for realizing personalized recommendations, conditional content display, and even for functions such as permission control.The Anqi CMS adopts a template engine syntax similar to Django, providing rich tags and filters to handle data.When we get some data set from the backend, such as the list of article tags

2025-11-08