In the template development of Anqi CMS, we often need to process and count text content, including calculating the length of the text. The system provideswordcountandlengthThese two filters (filters), they can all help us 'measure' text, but their focus and application scenarios are quite different.Understanding the core differences can make us more skillful in practical use, writing more efficient and accurate template code.
lengthFilter: The 'ruler' for measuring all content
First, let's talk aboutlengthFilter, it is like a precise ruler used to measure the 'absolute' length of content.This filter is very general, and can be used not only for strings, but also for arrays (slices) and key-value pairs (maps).
When you use on a stringlengthThe filter counts the actual number of UTF-8 characters in the string.This means that whether it is English letters, numbers, or Chinese characters, each one is counted as a unit."AnQiCMS"is 7, and"安企CMS"The length is also 7, because even Chinese characters are counted as one character in UTF-8 encoding.
For the array (slice),lengthThe filter will return the total number of elements in the array. If you have a list with 5 elements,lengthIt will tell you its length is 5. Similarly, when applied to key-value pairs (map), it will return the number of key-value pairs in the map.
Therefore,lengthThe filter mainly focuses on the number of constituent units of content, whether it is characters, array elements, or map entries, it is measured strictly by the number.
For example:
{{ "欢迎使用安企CMS"|length }}It will output8(Each Chinese character and letter counts as a character).{{ ["文章", "产品", "单页"]|length }}It will output3(The number of elements in the array).
wordcountFilter: A tool for calculating the "gold content" of text
Withlengthis different from precise measurement,wordcountThe filter is a special tool used to count the number of 'words' in a string. It focuses more on the semantic analysis of text rather than a simple pile of characters.
wordcountThe filter distinguishes words mainly by spaces when counting. This means that any character sequence separated by spaces will bewordcountTreated as a separate word.Even if this 'word' itself may consist of numbers, symbols, or continuous non-space characters, it will be counted as a whole if it is surrounded by spaces.
For example,"Hello AnQiCMS World"counted as 3 words. And"Hello-AnQiCMS-World"Since there are no spaces to separate, it is considered as a whole and counted as 1 word. For Chinese text, as there are usually no spaces between Chinese characters,wordcountIn default, the entire Chinese paragraph may be treated as a long 'word', which needs special attention when counting Chinese text.
For example:
{{ "AnQiCMS is a great CMS"|wordcount }}It will output6(number of words separated by spaces).{{ "欢迎使用安QiCMS"|wordcount }}It will output2(Welcome to An and QiCMS as two words, because there is a space between them).{{ "安企CMS"|wordcount }}it may output1(Because there is no space between them).
Core difference: character vs. word
UnderstandinglengthandwordcountThe core difference lies in their definition of 'unit'.
lengthFilterFocuses on:The smallest, independent constituent unit:For strings, it is each UTF-8 character; for collections, it is each element. It gives you the 'physical size' of the content.wordcountFilterFocuses on:Logical units separated by spaces.It gives you the 'semantic unit quantity' of the content.
Therefore, if you need to check if the user input characters exceed the limit (such as the title limit of 30 characters), you should uselengthIf you need to estimate the reading time of an article or analyze the lexical density of text, thenwordcountthis would be a better choice.
In practical applications, these two filters each have their irreplaceable roles:
lengthThe application scenario:- Limit the number of characters in input fields, such as article titles, SEO descriptions, etc.
- Check if an array or list is empty or how many items of data it contains.
- Perform precise character-level validation on short text generated by users.
wordcountThe application scenario:- Estimate the reading time of an article (for example, an average of 200 characters per minute).
- Set the minimum number of words or characters in the content management system to ensure the richness of the content.
- Perform a rough text volume count for languages such as English that are separated by spaces between words.
They are all related to 'length', but provide the ability to analyze text from different dimensions. They are indispensable practical tools in the development of AnQiCMS templates.
Common Questions (FAQ)
1.wordcountFilter can accurately count the number of Chinese text characters?Answer:wordcountThe filter mainly uses spaces to identify "words". Since Chinese text usually does not use spaces to separate characters,wordcountIt treats continuous Chinese characters as a "word", so it cannot accurately count the number of characters in Chinese text. If you need to count the number of Chinese characters, you should uselengthFilter, because it counts each Chinese character as a character.
2. How to determine if a variable (such as a string or an array) is empty?答:Determine whether a variable is empty, you can usually directly use the AnQiCMS template'sifjudgment statement, for example{% if variable %}If the variable is a non-empty string, non-empty array, or non-zero number, the condition will be true. If you need to check the length more explicitly, you can uselengthFilter, for example{% if myString|length > 0 %}or{% if myArray|length == 0 %}.
3.lengthFilter: when calculating Chinese characters, is it counted as one character per Chinese character or by byte?Answer:lengthThe filter counts the actual character number based on UTF-8 encoding when calculating the string length.This means that whether it is a letter, number, or Chinese character, each one is counted as a character unit.Therefore, a Chinese character is counted as a character accurately, not as multiple bytes.