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.