In Anqi CMS template development, we often need to process and count text content, including calculating the length of 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 proficient in practice, writing more efficient and accurate template code.
lengthFilter: The 'ruler' for measuring everything
First let's talk aboutlengthA filter, it is like an accurate ruler, used to measure the 'absolute' length of content.This filter is very general, it can be used for strings, as well as for arrays (slice) and key-value pairs (map).
When you use on a stringlengthWhen the filter is applied, it counts the actual number of UTF-8 characters in the string.This means that whether it is a letter, number, or Chinese character, each one is counted as a unit. For example,"AnQiCMS"Its length is 7, and"安企CMS"The length is also 7 because even Chinese characters are counted as one character in UTF-8 encoding.
For arrays (slices),lengthThe filter will return the total number of elements in the array. If you have a list containing 5 elements,lengthIt will tell you its length is 5. Similarly, when applied to a key-value pair (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 measures the quantity strictly.
For example:
{{ "欢迎使用安企CMS"|length }}It will output.8(Each Chinese character and letter counts as one character).{{ ["文章", "产品", "单页"]|length }}It will output.3(The number of elements in the array).
wordcountFilter: A tool for counting the 'gold content' of text
withlengthis different in precise measurement,wordcountA filter is a tool specifically designed to count the number of 'words' in a string. It focuses more on the semantic analysis of text rather than simple character stacking.
wordcountThe filter mainly uses spaces to distinguish words when counting. This means that any character sequence separated by spaces will bewordcountConsidered as an independent word. Even if the '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"Due to the lack of space separation, it is considered as a whole and counted as 1 word. For Chinese text, since Chinese characters are usually not separated by spaces,wordcountIn default settings, 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 output.6(Number of words separated by spaces).{{ "欢迎使用安QiCMS"|wordcount }}It will output.2(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).
The core difference: character vs. word
UnderstandinglengthandwordcountThe core difference lies in their definition of 'unit'.
lengthFilterFocuses onThe smallest, independent constituent unitFor a string, it is every UTF-8 character, for a collection, every element. It gives you the 'physical size' of the content.wordcountFilterFocuses onLogical units separated by spaces.That is a 'word'. It gives you the 'semantic unit count' of the content.
Therefore, if you need to check if the user input exceeds the limit (such as a 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, thenwordcountWould be a better choice.
In practical applications, these two filters each have irreplaceable roles:
lengthApplication scenarios:- Limit the number of characters in the input field, such as article titles, SEO descriptions, etc.
- Check if the array or list is empty or how many items it contains.
- Perform precise character-level validation on short text generated by the user.
wordcountApplication scenarios:- Estimate the reading time of an article (for example, an average of 200 words per minute).
- Set a word or word count lower limit in the content management system to ensure the richness of the content.
- Make a rough text volume count for English and other languages separated by spaces.
They are all related to 'length', but provide text analysis capabilities from different dimensions, and are indispensable practical tools for AnQiCMS template development.
Frequently Asked Questions (FAQ)
1.wordcountCan the filter accurately count the number of Chinese text characters?Answer:wordcountThe filter mainly identifies 'words' through spaces. Since Chinese text usually does not have spaces to separate characters,wordcountIt treats continuous Chinese characters as a whole '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 uselengthThe filter, because it counts each Chinese character as a character.
2. How to determine if a variable (such as a string or array) is empty?Answer: To determine if a variable is empty, it is usually possible to directly use the AnQiCMS template'sifjudgment statement, for example{% if variable %}If a 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.lengthThe filter calculates Chinese characters by character or by byte?Answer:lengthThe filter counts the actual character count of a string when calculating its length, according to UTF-8 encoding.This means that every English letter, number, or Chinese character counts as a character unit.So, a Chinese character is counted as one character accurately, not as multiple bytes.