In the template development of AnQi CMS,countThe filter is a very practical tool that can help us easily count the number of times a specific value appears.However, when using this filter, many users may be curious: when it calculates the frequency of a value, is it performing an exact match or a more flexible partial match?The answer is not one-size-fits-all, but varies depending on the data type of the operation.


countThe core role of the filter

In simple terms,countThe core function of the filter is to count the frequency of a target value in the given data. Whether you want to know how many times a word appears in a sentence or to understand how many times a specific item appears in a list,countCan be used in many ways. Its basic usage is{{obj|count:关键词}}of whichobjis the data you want to operate (can be a string or an array),关键词then it is the target value you want to count.

String matching behavior: substring matching

When you are going tocountThe filter applies to a string with a 'partial match' method, also known as 'substring match'.This means that as long as the keyword you specify is part of the string, it will be counted in.This is very convenient when counting the frequency of a phrase or word in a text.

For example, if you have a string“欢迎使用安企CMS(AnQiCMS)”and hope to count“CMS”times,countThe filter will also treat“安企CMS”and“AnQiCMS”of“CMS”All of them. Therefore, the expression{{“欢迎使用安企CMS(AnQiCMS)”|count:“CMS”}}the result would be2This behavior is very consistent with our intuitive understanding of text analysis, allowing us to flexibly capture all occurrences of the target vocabulary.

The matching behavior in an array: exact match

However, whencountThe filter's matching strategy becomes stricter when faced with an array (or slice), turning to 'exact match'. This means that only when the specified keyword matches with some element in the arrayAbsolutely consistentOnly when it is included will it be calculated. Any partial match will not be counted.

For example, suppose you have an arrayvalueswhose elements are["splits", "the", "string", "安企CMS"]. If you try to count“the”times,{{values|count:“the”}}It will return1Because it found a completely matching element. But if you try to count“安企”,{{values|count:“安企”}}the result would be0Though“安企”is an array element“安企CMS”One part, but in the context of an array, since it does not completely match an element, it is not counted.This precise matching is crucial for array operations, it ensures that when you are counting the elements of the array, you will not get incorrect results due to unexpected substring inclusion.

Summary

In short, the Anqi CMS'scountThe filter shows intelligent and adaptable matching behavior when processing different data types: strings use flexible substring matching, while arrays adhere to strict exact matching.Mastering this feature will make your content operation and template development work more proficient.


Frequently Asked Questions (FAQ)

Q1: What if I want to count partial matches in an array? A1: countThe filter does not support fuzzy (partial) matching of array elements. If you need to implement this feature in an array, you may need to write it manuallyforLoop to iterate over each element of the array and use a string processing filter (such ascontainOr other conditions can be judged to check whether each element contains the target substring, and then the count is manually added up.

Q2:countDoes the filter support counting the number of times numeric values appear in an array? A2:Support. As long as there is an element that matches the number you specifiedexactlythe filter will count it.countFor example,{{[1, 2, 3, 1]|count:1}}The result will be2.

Q3:countDoes the filter have a difference in the matching rules when counting Chinese and English strings? A3:There is no difference.countThe filter treats Chinese and English equally. Whether it is substring matching in Chinese character strings or exact matching of array elements composed of Chinese characters, the behavior rules are consistent.For example, in a string“你好世界”Chinese count“你好”It is still substring matching.