countfilter.

Get to knowcountFilter

countThe filter is a practical feature provided by Anqi CMS template engine, which is specifically used to calculate the number of times a keyword appears in a given string or array (also known as a slice) and returns an integer value directly.This process is very intuitive, no complex programming logic is required, just apply it in the template.

Its basic usage is:{{ 变量 | count:"关键词" }}.

For example, if we want to know how many times the word 'AnQiCMS' appears in a text, we can test it in this way:

{% set slogan = "安企CMS是一个强大的内容管理系统,安企CMS致力于提供高效解决方案。" %}
<p>关键词“安企CMS”在这段文本中出现了 {{ slogan | count:"安企CMS" }} 次。</p>

After running, the page will display that the keyword 'AnQi CMS' appears 2 times in this text, which clearly showscountthe function of the filter.

Apply flexibly in different types of content

countThe filter can be applied to various content scenarios on the website, whether it is fixed text, dynamic article content, or custom fields, it can handle it easily.

Find in normal text or custom single-line field

Apply directly to some fixed text content or custom single-line text fieldcountThe filter is the most direct way. Suppose you have a custom field on the back end that stores a brief introduction, and you want to count the number of times a word appears in it, you can directly call the field:

{% archiveDetail introduction with name="introduction" %}
{% set targetKeyword = "安企" %}
<p>文章简介中“{{ targetKeyword }}”出现了:{{ introduction | count:targetKeyword }} 次。</p>

IfintroductionThe content of the field is "AnQi CMS is excellent, AnQi CMS is trustworthy", then the output will be2.

Search within the article or page content

The main content of an article or page is usually the most abundant part of the website, and it is also the focus of keyword density analysis. The content of the articles or single pages of AnQi CMS can be accessed througharchiveDetailorpageDetailLabel retrieval and its content as inputcountInput to the filter.

Please note that the main text of the article often contains HTML tags.countThe filter will directly match and count within the original string containing HTML. If your goal is to count keywords in the visible text of the user, rather than text within tags or attributes, you can usestriptagsThe filter removes all HTML tags and then appliescountfilter.

For example, count the occurrences of "Go language" in the article body:

{% archiveDetail articleContent with name="Content" %}
{% set searchKeyword = "Go语言" %}
<p>关键词“{{ searchKeyword }}”在文章正文中出现了 <b>{{ articleContent | count:searchKeyword }}</b> 次。</p>

{# 如果只想统计纯文本中的关键词,可以先去除HTML标签 #}
<p>(纯文本统计)关键词“{{ searchKeyword }}”在文章纯文本中出现了 <b>{{ articleContent | striptags | count:searchKeyword }}</b> 次。</p>

Thus, you can choose according to your actual needs, whether to count in the complete content containing HTML or in plain text content.

Search in an array or list.

Besides strings,countThe filter also supports finding the number of occurrences of a keyword in an array or list. It is important to note that when applied to an array,countThe filter performs a 'full match' on elements, not a substring match.That is to say, an element of the array must match the keyword you provide exactly in order to be counted.

If you have a passsplitorfieldsA filter that splits the words from a string or is a custom multi-select field (such asarchiveParamsIf a field is multi-select, it can be applied to itcount.

For example, we have a list of article keywords and we want to know how many times the label “CMS” appears:

{% set articleKeywords = "安企CMS,CMS系统,Go语言CMS,CMS解决方案" | split:"," %} {# 假设这是一个由逗号分隔的关键词列表 #}
{% set keywordToCount = "CMS系统" %}
<p>关键词“{{ keywordToCount }}”在标签列表中出现了 {{ articleKeywords | count:keywordToCount }} 次。</p>

Heresplit:","Split the string into an array["安企CMS", "CMS系统", "Go语言CMS", "CMS解决方案"]Then,countThe filter will match elements in the array precisely. In this example, 'CMS system' will match 1 time.

Points to note

While usingcountWhen filtering, there are several minor details to pay attention to in order to ensure you get the expected results:

  • Case sensitive: countThe filter performs case-sensitive matching when matching. For example, "CMS" and "cms" are considered as