In the template development of Anqi CMS, handling string length is a common requirement.When dealing with multilingual content, especially containing Chinese characters, whether the length of a string is calculated by bytes (byte) or characters (character) will directly affect the accuracy of template output and the presentation effect of the content.

Auto CMS template usedlengthThe filter is designed to solve this problem. It counts the length of a string bycharacters rather than bytes.This means, whether it is a letter, number, or a Chinese character,lengththe filter will treat it as a separate 'character' for counting.

The principle behind this, thanks to the unified adoption of the Anqi CMS templateUTF-8 encoding.English is a language that typically uses 1 byte per character for English characters, while Chinese characters may use 2 to 4 bytes.lengthlengthThe filter can accurately identify and count each independent character, whether it is English letters, numbers, or complex Chinese characters.

lengthThe actual application and impact of the filter

UnderstandlengthThe feature of counting characters in the filter is of great guiding significance to our content operation and template design:

  1. Content truncation and abstract generationIn displaying article lists or summaries, we often need to truncate long titles or content to a fixed length. For example, if the requirement is to keep the title within 20 characters, use{{ item.Title|length }}Can ensure that whether the title is Chinese or English, it can be controlled precisely within 20 characters to avoid the situation where Chinese titles are incorrectly truncated halfway or English titles are too long and overflow.truncatecharsEnglish filters, which can achieve more intelligent text truncation.
  2. Form input validationIn the process of users submitting comments, messages, or registration information, the front end often needs to limit the length of the input content.lengthFilter, which can pre-validate user input on the client (or during backend template rendering) to ensure it meets database fields or design specifications, enhancing user experience and reducing invalid submissions.
  3. Dynamic layout and style adjustmentIn some complex layouts, it may be necessary to dynamically adjust the width, font size, or display mode of elements based on the text length.For example, if the text of a navigation menu item is too long, you can apply a different CSS style to ensure the page is beautiful.
  4. List and array processingOther than strings,length过滤器同样适用于获取数组和键值对(map)的元素数量,这在遍历数据列表时非常实用,例如判断一个图片列表是否为空,或者循环显示指定数量的标签。

How to uselengthFilter

lengthThe use of the filter is very intuitive and simple, just pass it through the pipe character after the variable that needs to be calculated in length|Add it.length.

Example: Calculate the length of a stringSuppose there is a variablearchive.Title, its value may be"安企CMS是一个强大的系统"or"AnQiCMS is powerful".

{{ archive.Title|length }}
  • Ifarchive.TitleYes"安企CMS是一个强大的系统"The output will be11.
  • Ifarchive.TitleYes"AnQiCMS is powerful"The output will be19.

It can be seen that Chinese characters and English characters are both counted as 1.

Example: Calculate the length of an array or listAssumetagsIt is an array containing multiple tags, such as['SEO', '模板', 'Go语言'].

{{ tags|length }}

The output will be3.

In addition, the Aqy CMS also provideslength_isFilter used to determine if the length is equal to a specified value while calculating the length and return a boolean result.

{{ "你好世界"|length_is:4 }} {# 输出 True #}
{{ "你好世界"|length_is:5 }} {# 输出 False #}

Summary of the impact on content output.

lengthThe design of the filter counting characters greatly simplifies the length processing logic of multi-language content, especially Chinese characters.It ensures that the character length displayed in the AnQi CMS template is consistent with the user's intuitive perception and actual display length, regardless of the content language.This avoids display anomalies caused by underlying encoding differences, allowing content operators to focus more on the content itself rather than the technical details at the bottom, effectively improving the management efficiency and user experience of the website content.


Common Questions (FAQ)

  1. 问:If my string contains spaces and punctuation marks,lengthdoes the filter calculate them?答:Yes,lengthThe filter counts all visible characters in a string, including spaces, punctuation marks, numbers, English letters, and Chinese characters, as 1 character.

  2. Q:lengthCan the filter be used to calculate the length of numeric or boolean values?Answer:lengthThe filter is mainly designed to calculate the length of strings, arrays (slices), and key-value pairs (maps).If applied to numeric or boolean values, unexpected results may occur, even errors, because these types themselves do not have the concept of 'length'.ifcondition judgment.

  3. Question: If I need to handle strings by byte length, does the SafeCMS template have a corresponding filter?Answer: Aqin CMSlengthThe filter counts characters by Unicode rather than by bytes.Currently, the official documentation does not directly provide a filter for calculating the length of a string by bytes.Under most website content display scenarios, character counting is more in line with visual and layout requirements.If there is indeed a special requirement for byte-wise processing, it may be necessary to preprocess the data in the backend logic of the Go language, or consult more advanced template extension methods.