In AnQi CMS template development, efficiently handling and analyzing content is the key to improving website interactivity and information display capabilities.Sometimes, we need to know how many times a specific word appears in a text or how many times a specific element is included in a data list.countThe filter can be put to good use. It is a very practical tool that can help us quickly count the occurrences of a specific keyword in a string or an array (slice).

Core Function Overview

countThe main function of the filter is to calculate the total number of times a specified keyword appears in a given string or array.It provides a concise and efficient way to perform preliminary quantitative analysis, which is particularly convenient in scenarios where content analysis, data verification, or conditional rendering is required.

Use Case 1: Count the frequency of a keyword in a string

When you have a piece of text and want to know the frequency of a specific word in it,countThe filter can directly process string data.

The usage is very intuitive, just pass the string variable through a pipe symbol.|pass tocountFilter, and specify the keyword you want to count after the colon.:You can specify the keyword you want to count after the colon.

For example, we have a piece of text: “Welcome to AnQiCMS (AnQiCMS)”, now we want to know how many times the word “CMS” appears in this text:

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

Run this code and the output will be2This is becausecountThe filter successfully identified and counted the two occurrences of "CMS" in the string.This feature is very useful for content editing to perform keyword density analysis, for SEO optimization personnel to check keyword layout, or for any scenario that requires quantitative analysis of text.

Use case two: Count the occurrences of a specific element in an array (Slice)

countThe filter can not only handle strings but also be applied to arrays (usually referred to as slice in AnQiCMS templates). However, there is an important detail to pay special attention to when counting arrays:The keyword must match the elements in the array exactly, does not support partial matchingThis means that if you want to count an element in an array, the keyword you provide must match the entire value of the element in the array.

Let us understand this through a specific example. Suppose we have a byfieldsThe filter from the array split from the string "splits the string 安企CMS"valuesIt contains["splits", "the", "string", "安企CMS"].

If we want to count the number of times the element "the" appears:

{% set values = "splits the string 安企CMS"|fields %}
{{values|count:"the"}}

This code will output correctly1because there is an element in the array that matches "the" exactly.

However, if we try to count the occurrences of '安企':

{% set values = "splits the string 安企CMS"|fields %}
{{values|count:"安企"}}

The output result will be:0This is because one element of the array is "AnQi CMS", and the keyword "AnQi" we provided is only a part of this element, not a complete match. When usingcountRemember this 'exact match' rule when filtering and counting an array to avoid unexpected results.

Usage method

Whether you are counting strings or arrays,countThe basic syntax pattern of the filter is consistent:

{{ obj|count:关键词 }}

Among themobjRepresents the string variable or array variable you want to operate on,关键词which is the specific text or number you want to count the occurrences of.

Example Demonstration

Here are some more comprehensive examplescountExample of filter usage to help you better understand and apply:

{# 统计字符串中关键词出现的次数 #}
{{"欢迎使用安企CMS(AnQiCMS)"|count:"CMS"}}
{# 预期输出: 2 #}

{# 统计数组中特定元素出现的次数(完全匹配)#}
{% set values = "splits the string 安企CMS"|fields %}
{{values|count:"the"}}
{# 预期输出: 1 #}

{# 统计数组中特定元素出现的次数(非完全匹配,结果为0)#}
{% set values = "splits the string 安企CMS"|fields %}
{{values|count:"安企"}}
{# 预期输出: 0 #}

{# 另一个数组完全匹配的例子 #}
{% set tags = ["安企CMS", "网站运营", "Go语言", "安企CMS"] %}
{{tags|count:"安企CMS"}}
{# 预期输出: 2 #}

{# 统计字符串中大小写敏感的关键词 #}
{{"AnQiCMS is a great CMS."|count:"cms"}}
{# 预期输出: 1 (安企CMS模板在字符串统计时默认区分大小写) #}

{# 统计数字在数字数组中的出现次数 #}
{% set numbers = "[1, 5, 2, 5, 3]"|list %}
{{numbers|count:"5"}}
{# 预期输出: 2 (这里关键词5需作为字符串传入,或者系统会尝试转换为字符串进行匹配) #}

Through these examples, we can seecountHow to apply the filter in different scenarios and understand its 'perfect match' feature when processing arrays. Flexibly apply it in your Safe CMS project development and content operationcountThe filter can bring you more accurate data insights and more efficient template control.


Frequently Asked Questions (FAQ)

  1. Q:countDoes the filter distinguish between uppercase and lowercase when counting strings?A: Yes,countThe filter is case-sensitive when counting keywords in a string.For example, "CMS" and "cms" are considered different keywords for statistics.countUse it first beforelowerorupperThe filter converts strings to uppercase or lowercase and then performs the count.

  2. Q: Why am I getting a count of 0 for a keyword in an array when I'm sure it exists in the array elements?A: This is likely because the keywords you provided do not match the elements in the array completely.countThe filter requires the keyword to be exactly consistent with the value of the array element when counting in the array.Please check if your keywords include extra spaces, punctuation marks, or if they are just part of the array element value.If the array element is of numeric type, the keyword passed in should also try to convert it to a numeric type, or ensure that both types are consistent.

  3. Q:countCan the filter count keywords in nested arrays or nested objects?A: According to the current document description,countThe filter is mainly used to count keywords in flat strings or one-dimensional arrays.It does not directly support deep recursive statistics of nested arrays or complex objects.forLoops and conditional judgments are used to implement more complex logic in the template.