In AnQiCMS template design, we often encounter the need to analyze content data or perform specific displays, such as counting the number of occurrences of a keyword in an article.This has practical significance for content operation, SEO optimization, and improving user experience.AnQiCMS provides a flexible and feature-rich template engine, allowing 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.countFilter.countFilter is a very practical tool that can calculate the number of times a specified keyword appears in a line of string or array, thus helping us quickly obtain the desired data.

Core Implementation: UtilizecountFilter

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

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 like this:

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

This code will return2It is because 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.ContentThis variable contains.

However, these content variables often contain HTML tags (for example<p>/<strong>/<a>etc.), if used directlycountFilter that may also include text within HTML tags or be affected by tag structure. To obtain more accurate pure text statistical results, we need to incountUse it before the filterstriptagsFilterRemove all HTML tags.

Additionally, in most cases, we want to count without distinguishing between uppercase and lowercase, for example, 'CMS' and 'cms' should be considered the same word. In this case, we also need to introduce anotherlowerFilterConvert the text content and the keywords to lowercase to achieve case-insensitive matching.

Considering all factors, the complete process for counting the occurrences of a specific word in the content of an article is as follows:

  1. Obtain the original content:Firstly, from the article detail page,archive.Content(or other content variables such as)page.Content、Custom fields, etc.) Get the original HTML content.
  2. Remove HTML tags:UsestriptagsThe filter converts HTML content to plain text.
  3. Uniform case:UselowerThe filter converts plain text content to lowercase while ensuring that your target keywords are also converted to lowercase.
  4. Execution count:Finally, usecountThe filter counts the occurrences of specific keywords.

Let's take a look at a specific template code example, assuming we want to count the number of times the word 'AnQiCMS' appears in 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 keyword and article content to lowercase, then removes the HTML tags, and finally performs an accurate count.Thus, whether it is "AnQiCMS", "anqicms", or any other combination of uppercase and lowercase, it can be counted correctly.

If your content paragraph is stored in a custom field, for example, you have defined 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>

Through this way, AnQiCMS's template engine provides powerful text processing capabilities, allowing content operators to perform in-depth analysis and customized display of content without modifying the core code. Whether it is for keyword density analysis in SEO strategies or a simple content overview,countFilters can help you out a lot.

Common Questions (FAQ)

1.countAre filters case-sensitive during statistics?

Yes,countFilter is case-sensitive by default. If you need to perform case-insensitive statistics,countUse it before the filterlowerorupperThe filter converts both the text content and the keywords you want to count to the same case. For example,{{ content|lower|count:"keyword" }}.

2.countDoes the filter also count the text within HTML tags?

Yes,countFilter defaults to treat 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 that incountUse it before the filterstriptagsFilter removes all HTML tags.

**3.countFilter is for counting "substring" or "whole word"