The `count` filter calculates the number of times a keyword appears in an array, is it a partial match or a complete match?

Calendar 👁️ 59

In the template development of AnQi CMS,countThe filter is undoubtedly a very useful tool when processing data, as it helps us quickly count the frequency of a specific keyword in the data.However, when using this filter, many users may be confused: when it processes string and array data of different types, is the keyword matching method 'partial match' or 'full match'?This is crucial for accurately presenting content data.

Get to knowcountFilter and its matching mechanism

countThe filter is mainly used to calculate the number of times a keyword appears in a specified data source. Its basic usage is intuitive and usually written as{{ obj|count:关键词 }}.objrepresents the data source you want to count.关键词It is the target text you want to count the number of occurrences. But as we will see next, the matching logic of this 'keyword' will be different in front of different data types.

Keyword statistics in a string: Flexible partial matching

WhencountThe filter applies to a single string and its behavior pattern is "partial match".This means that as long as the string contains the keyword as a substring, regardless of whether there are other characters before or after it, it will be recognized and counted in the total.

Let's look at an example provided in the document:

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

The output result of this code is2It is obvious that the "CMS" in the string and the "CMS" in "AnQiCMS" are recognized and counted. This indicates that in the context of strings,countThe filter will match the substring as you expect.This mechanism is very convenient when it is necessary to count the frequency of a word in the text of an article, or to detect whether a specific phrase is included in the title or description.

Array keyword statistics: strict 'exact match'

However, whencountThe filter becomes more strict when faced with array (or Go language slice) data types, turning into 'exact match'. This means that only when an element in the arraycompletely, accuratelyWill be counted only when it is equal to the keywords you provide. Any partial match will be ignored.

The official documentation states this clearly: The keyword in the array must be equal to the value of the array, it must be completely equal and cannot be a partial match."

We can usefieldsThe filter to split the string into an array to simulate this scenario:

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

In this example,valuesThe elements contained in the array are["splits", "the", "string", "安企CMS"]. When the keyword is"the", due to the existence of a completely matching element in the array"the", the result is1.

But if we try to match a keyword that only partially exists:

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

At this moment, although there is one element in the array is"安企CMS"which contains the words 'Anqi', but since 'Anqi' does not match any array element, the counting result is0This example clearly demonstratescountThe strict 'exact match' feature of the filter in array context

Summary and insights for content operation

In summary, it is about AnQi CMS'scountThe filter, based on the different data types, has a completely different keyword matching logic: when processingstringit usespartial matching; while processingarrayIf, then strictly followPerfect matchPrinciple.

This design reminds us in use, when doing data statistics and content presentation, it is necessary to judge according to the type of data source.countThe behavior of the filter.For example, if you want to count the number of tags (usually stored in an array) in an article that contain the word "hot", then the tag must be "hot" itself, not "hot news" or "Today's hot".countThe filter is used for statistics, or use other methods that are more suitable for fuzzy matching array elements.

Accurately understand these subtle details will help you more accurately use the Anqi CMS template function to create logical and accurate website content.


Frequently Asked Questions (FAQ)

1. What are the alternative methods if I need to perform fuzzy matching in an array instead of exact matching?IfcountThe filter in the array may not meet your fuzzy matching needs, you can consider first passing all the string elements throughjoinFilter concatenating into a long string, then usingcountThe filter performs fuzzy matching. Or, you can also write a loop in the template logic, traversing each element of the array, and then using it for each element.containThe filter makes a judgment and manually adds the count.

2.countDoes the filter distinguish between uppercase and lowercase when matching?Yes,countThe filter is case-sensitive when matching string and array elements.For example, searching for 'cms' in a string will not match 'CMS', and searching for 'tag' in an array will not match 'Tag'.If you need to match case-insensitively, you may need to convert the data source and keywords to a uniform case (such as lowercase) before comparison.

3. BesidescountFilter, does AnQi CMS have other filters that can determine if keywords are present in the data?Yes.containThe filter is used to determine whether a line of text string or an array (slice) contains a certain keyword.It returns a boolean value (True or False) indicating whether it exists.countdifferent,containOnly concerned with existence, not counting the number of times.

Related articles

How to calculate the number of times a keyword appears in a string or array in the Anqi CMS template?

In content operation, precisely mastering the use of keywords on the page plays a crucial role in SEO strategy, content quality assessment, and user experience optimization.AnQiCMS (AnQiCMS) is a flexible and efficient content management system with a powerful template engine built-in with many practical features, including a filter that can help us easily calculate the number of times keywords appear.Next, we will introduce how to use these features to count the frequency of keywords in the Anqi CMS template.### Deeply understand `count`

2025-11-08

Can the `contain` filter be used to check if a Map (key-value pair) contains a specific key?

AnQiCMS with its efficient and customizable features brings many conveniences to content management.In the daily operation of websites, we often need to dynamically adjust the page content in templates based on the existence of data.Among them, it is a common requirement to judge whether a specific key exists in the data of key-value pair (Map) type.Today, let's delve into whether and how the `contain` filter in AnQiCMS templates can be used to check for the existence of keys in a Map.

2025-11-08

How to determine whether an array or string in an Anqicms template contains a specific keyword?

In AnQi CMS template development, we often need to dynamically display information based on specific attributes or keywords of the content.For example, determine whether an article title contains a certain product name, or check whether a document's tag list contains a hot keyword.This can not only make the website content more targeted, but also improve user experience and SEO effect.The AnQi CMS template engine provides powerful and flexible features to meet these needs.Among them, it is easy to determine whether an array or string contains a specific keyword by using the built-in `contain` filter.

2025-11-08

How does the `slice` filter implement the function of extracting elements from the end of an array?

In AnQiCMS template development, the `slice` filter is a very practical tool that allows us to flexibly extract a part of the string or array content.But do you know? It can also realize a particularly clever function - to extract elements from the end of an array or list, which is very convenient when it comes to displaying the latest data or removing old data.Today, let's delve into how the `slice` filter achieves this functionality through negative indices.Understanding the basic usage of the `slice` filter As the name implies

2025-11-08

How to randomly select an element or character from an array or string in Anqi CMS template?

In Anqi CMS template, adding dynamism and fun to the content is an important aspect of improving user experience.Sometimes, we hope to display a random element on the page, such as a random slogan, a random recommendation tag, or randomly select a picture from a group of images.AnQi CMS relies on its flexible Django template engine syntax to provide a simple yet powerful way to meet this requirement, with the core tool being the `random` filter.

2025-11-08

How to get the first element/character of an array or string in AnQi CMS template?

During the development of website templates, you often encounter the need to extract the first element or character from a data collection (whether it is an array, slice, or string).AnQiCMS uses syntax similar to the Django template engine, providing rich tags (Tags) and filters (Filters), allowing us to flexibly and efficiently handle data.This article will introduce in detail how to use these powerful tools in the AnQiCMS template to easily achieve the operation of getting the first element/character of an array or string.--- ### One

2025-11-08

How to get the last element/character of an array or string in Anqi CMS template?

In Anqi CMS template development, we often encounter the need to obtain the last element or character from a set of data (whether it is an array, list, or string).No matter whether it is to display the latest comments, the last item in the list, or extract the end information of specific text, understanding how to efficiently implement this at the template level will greatly enhance the flexibility and development efficiency of the template.The AnQi CMS template engine provides a simple and powerful filter function that can help us easily achieve this goal.

2025-11-08

How to perform addition operations on numbers in AnQi CMS template, including integers and floating-point numbers?

In AnQiCMS template design, sometimes we need to perform dynamic calculations on the numbers displayed on the page, such as calculating the total price of goods, displaying the cumulative points of users, or adjusting certain values.Whether it is a simple integer addition or involves decimal floating-point arithmetic, AnQiCMS provides intuitive and powerful methods to meet these needs.This article will delve into how to implement addition operations in the AnQiCMS template, making your content display more dynamic and practical.

2025-11-08