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'?