How to count the occurrences of a specific word in a paragraph in AnQiCMS template?

Calendar 👁️ 75

In AnQiCMS template design, we often encounter the need to analyze content or display it in a specific way, such as counting the frequency of a keyword in an article.This has practical significance for content operations, SEO optimization, or improving user experience.AnQiCMS provides a flexible and feature-rich template engine that allows us to easily implement such operations.

To count the number of occurrences of a specific word in a paragraph, we mainly use the content variables in the AnQiCMS template andcountFilter.countThe filter is a very practical tool, it can calculate the number of times a specified keyword appears in a line of string or array, thereby helping us quickly obtain the data we need.

Core implementation: utilizecountFilter

countbasic usage of the filter is very intuitive:{{ obj|count:关键词 }}.objRepresents the text content variable you need to count关键词which is the specific word or phrase you want to count the occurrences of.

Let's take a simple example, if we have a string"欢迎使用安企CMS(AnQiCMS)"And want to count the occurrences of 'CMS', you can write it directly:

{{ "欢迎使用安企CMS(AnQiCMS)"|count:"CMS" }}

This code will return2Because it found 'CMS' in 'AnQiCMS' and 'CMS' in 'AnQiCMS'.

Combine the content of the article for statistics

In the operation of actual websites, we usually need to count the keywords in the main content of articles or pages. In AnQiCMS, the main content of articles or single pages is usually stored inarchive.Contentorpage.Contentsuch variables.

However, these content variables often contain HTML tags (for example<p>/<strong>/<a>etc.), if used directly.countA filter that may include the text within HTML tags or be affected by the tag structure. To obtain more accurate pure text statistics, we need tocountbefore the filter.striptagsFilterRemove all HTML tags.

In addition, in most cases, we want the statistics to be case-insensitive, for example, "CMS" and "cms" should be considered the same word. In this case, we need to introducelowerFilterConvert the text content and the keywords to lowercase to achieve case-insensitive matching.

Consider all the above, the complete process of counting the occurrences of a specific word in the article content is as follows:

  1. Get the original content:First, from the article detail page,archive.Content(or other content variables such aspage.ContentRetrieve the original HTML content with customized fields.
  2. Remove HTML tags:UsestriptagsThe filter converts HTML content to plain text.
  3. Unify case:UselowerThe filter converts plain text content to lowercase, ensuring that the target keywords are also converted to lowercase.
  4. Execution count:Finally, usecountThe filter counts the occurrences of specific keywords.

Let's look at a specific template code example, assuming we want to count the number of times the word 'AnQiCMS' appears on the article detail page:

{# 定义我们要统计的目标关键词,并将其转换为小写,以确保不区分大小写匹配 #}
{% set targetWord = "anqicms" %}

{# 获取当前文章的原始内容,这通常是包含HTML标签的 #}
{% set rawContent = archive.Content %}

{# 使用 striptags 过滤器移除所有HTML标签,得到纯文本内容 #}
{% set textOnlyContent = rawContent|striptags %}

{# 使用 lower 过滤器将纯文本内容转换为小写,以便进行不区分大小写的匹配 #}
{% set normalizedContent = textOnlyContent|lower %}

{# 最后,使用 count 过滤器统计目标关键词在处理后的内容中出现的次数 #}
{% set occurrenceCount = normalizedContent|count:targetWord %}

<p>在当前文章内容中,关键词“<strong>{{ targetWord }}</strong>”共出现了 <strong>{{ occurrenceCount }}</strong> 次。</p>

This code first standardizes the target keywords and article content to lowercase, then removes the HTML tags, and finally counts them accurately.This, whether it is "AnQiCMS", "anqicms", or any other case combination, can be correctly counted.

If your content paragraph is stored in a custom field, for example, if you define a field namedintroductionto store the paragraph, then you can retrieve and count it like this:

{% archiveDetail introduction with name="introduction" %}
{% set targetWord = "介绍" %}
{% set normalizedIntro = introduction|striptags|lower %}
{% set countInIntro = normalizedIntro|count:targetWord %}

<p>在自定义介绍段落中,“{{ targetWord }}”出现了 <strong>{{ countInIntro }}</strong> 次。</p>

In this way, AnQiCMS's template engine provides powerful text processing capabilities, allowing content operators to perform in-depth analysis and customized display according to specific needs without modifying the core code. Whether it is for keyword density analysis in SEO strategy or a simple content overview,countAll filters can help you a hand.

Frequently Asked Questions (FAQ)

1.countAre filters case sensitive when counting?

Yes,countThe filter is case sensitive by default. If you need to perform a case-insensitive count, you shouldcountbefore the filter.lowerorupperThe filter will convert both the text content and the keywords you want to count to the same case. For example,{{ content|lower|count:"keyword" }}.

2.countWill the filter also count the text inside HTML tags?

Yes,countThe filter will default to treating HTML tags (such as<p>/<a>) and the attribute values of tags as plain text for calculation. To only count visible plain text content, it is recommended tocountbefore the filter.striptagsThe filter removes all HTML tags.

**3.countThe filter is counting 'substring' or 'whole word'?

Related articles

What is the difference between the `count` filter and the `wordcount` filter in counting specific elements in a string?

In Anqi CMS template design, we often need to process various text content on the page, among which counting the elements in a string is a common requirement.Anqi CMS provides two practical filters `count` and `wordcount`, although they are all related to "counting", they have clear distinctions and their own preferred scenarios in practical applications.Understanding these distinctions can help us manage and display website content more accurately and efficiently.

2025-11-09

How to combine the `split` filter to split a string into an array of words for counting or traversal?

In the daily content operation of Anqi CMS, we often encounter scenarios where we need to handle string data, such as keyword lists in articles, content tags, or some multi-value custom fields stored in a specific format.These data often exist in the form of a single string, but when displayed on the front end, we hope they can be presented in a more flexible and structured manner, such as independent label blocks, clickable links, or the need to count the number of elements within them.

2025-11-09

Does AnQiCMS's backend content editing feature provide real-time word count similar to `wordcount`?

AnQiCMS (AnQi Content Management System) is an enterprise-level content management system developed based on the Go language, which performs excellently in providing efficient and customizable content management solutions.For content operators, the convenience of the backend editing experience is crucial, and one of the details often paid attention to is the real-time statistics of the number of words or characters in the content.Many content operators often need to pay attention to word count when writing articles, in order to control the length of the article, meet SEO requirements, or comply with specific publishing standards.

2025-11-09

`wordcount` filter can identify and count consecutive non-space characters (such as URLs) as a single "word"?

During the content management process of AnQiCMS (AnQiCMS), we often need to count the number of words in articles in order to better plan content, estimate reading time, or optimize SEO.This is when the `wordcount` filter becomes a very practical tool.

2025-11-09

How does the `wordcount` filter count text that contains HTML entities (such as `&nbsp;`)?

When using AnQi CMS to manage website content, we often need to perform word counts on articles, whether it is for content planning, SEO optimization, or simply to meet publishing requirements, the `wordcount` filter is a very practical tool.It can quickly calculate the number of words in text, providing intuitive data support for content operation.

2025-11-09

How to estimate the reading time required by users on the article detail page of AnQiCMS based on the `wordcount` result?

In website operation, we all hope to provide visitors with the best browsing experience possible.One feature that seems trivial but can significantly improve user satisfaction is to estimate the time required for users to read the article details page.This can not only help visitors quickly judge whether there is enough time to read the entire content, but also effectively improve the reading completion rate of the article, thereby indirectly optimizing the user engagement of the website.AnQi CMS is a comprehensive and highly flexible content management system that provides powerful template tags and filters, making it very easy to implement this feature.

2025-11-09

What are the common reasons and troubleshooting methods when the `wordcount` filter results in 0?

When using AnQi CMS for content operations, we often use various template filters to process and display data.Among them, the `wordcount` filter is a very practical feature that can help us count the number of words in a text, which is very helpful for article summaries, SEO word count, or content length limits.However, sometimes we may encounter a confusing situation: the `wordcount` filter is applied, but the count always shows as 0.This is usually not a system failure, but caused by some common reasons

2025-11-09

In AnQiCMS multilingual site, is the `wordcount` filter accurate and consistent in word counting for different languages?

When building a multilingual website, the various functions of the Content Management System (CMS) in handling different language content are a common concern for operators.AnQiCMS is a system focused on enterprise-level content management and provides a solid foundation in multilingual support.

2025-11-09