In website content operation, we often encounter scenarios where we need to obtain or judge the length of data.For example, display the number of words in an article, the number of items in a list, or decide whether to display an element based on the content length.The template engine of AnQiCMS (AnQiCMS) provides a series of practical filters to help us meet these needs, among whichlengthandlength_isThe filter is among the best. It helps us easily implement these features, making the dynamic display and interaction logic of the website more flexible.
lengthFilter: Core tool for getting data length
As the name implies,lengthThe filter is used to obtain the "length" of a data item.It is very flexible, whether it is a string, an array (often referred to as the Go language's slice type in Anqicms templates), or a key-value pair (map), it can accurately give the number of elements or characters contained in it.
Specifically, whenlengthThe filter applies to a string and it calculates the actual Unicode character count of the string. This means that both a Chinese character and an English character are counted as1The length, not the original byte count. This is particularly friendly for operators of Chinese websites, as it conforms to our intuitive understanding of 'word count'.
When it is applied to an array (such as a list of articles, a collection of images) or a key-value pair (such as a custom parameter collection),lengththe filter will directly return the number of elements or key-value pairs it contains.
Usage example:
Assuming we have an article objectarchiveof whichTitleIs a string,Imagesis an image array, whereasParamsis a set of key-value pairs:
To get the length of a string:If you want to know the length of the article title, you can use it directly like this:
<p>文章标题长度:{{ archive.Title | length }}</p>If
archive.TitleThe value is"安企CMS让内容管理更简单"the output will be12.Get the length of the array:To display the number of images in an article, you can do it like this:
<p>文章配图数量:{{ archive.Images | length }}</p>If
archive.ImagesIf it contains 3 images, the output will be:3.Get the length of the key-value pair (Map):If you want to know how many custom parameters an article has, you can do it like this:
<p>自定义参数数量:{{ archive.Params | length }}</p>If
archive.ParamsIf 5 custom parameters are defined, the output will be:5.
length_isFilter: a powerful tool for length judgment
In addition to simply getting the length, sometimes we need to make judgments based on the length, such as whether the length of a string is exactly equal to some value, or whether a list is exactly empty. At this time,length_isThe filter comes into play. It receives an expected length value as a parameter, compares it with the actual length, and returnsTrueorFalse(a boolean value).
This filter is very useful when it is necessary to control the page display logic according to the length of the data.For example, when the list is empty, display a prompt such as 'No data available', or display supplementary information when a certain description field is too short.
Usage example:
Check if the length of the string is equal to the specified value:If you need to check if the user's input nickname is exactly 6 characters:
{% set username = "张三丰" %} {% if username | length_is: 6 %} <p>昵称长度符合要求。</p> {% else %} <p>昵称长度不符合要求。</p> {% endif %}Because the length of “张三丰” is 3, the output will be:
昵称长度不符合要求。.Check if the list is empty:Before displaying the comment list, we usually check if there are any comments.
{% if comments | length_is: 0 %} <p>暂时还没有评论,快来发表您的看法吧!</p> {% else %} {# 显示评论列表的代码 #} {% endif %}So when
commentsIf the array is empty, a friendly reminder will be displayed on the page.
Application scenario: Make data management smarter
tolengthandlength_isFilter integrated into template design, bringing many conveniences and optimizations:
- Dynamic display of count:Display the number of articles under each category on the category page, or the number of products under each brand in the product list. For example:
{{ category.Title }} ({{ category.Archives | length }} 篇). - Content truncation and prompt:For an article abstract or description, you can use
lengthDetermine its length, if it exceeds the preset value, then combineslicethe filter to truncate and add an ellipsis to keep the page tidy. - Conditional element display:When a certain image collection is empty, the image gallery may not be displayed;When a contact information field is empty, the corresponding contact icon may not be displayed.This avoids the visual confusion caused by displaying empty data.
- Form validation prompt:Perform real-time validation of the length of the user input field on the front end, and provide prompts to enhance the user experience.
By flexible applicationlengthandlength_isThe filter allows us to easily obtain and judge the length of data in the Anqi CMS template, thereby building a more dynamic and user-friendly website interface, making content operations twice as effective.
Frequently Asked Questions (FAQ)
1.lengthThe filter can handle what data types? What will be returned if the object is not a string or an array?
lengthThe filter is mainly designed to handle strings, Go language slices (corresponding to arrays or lists in templates), and maps (corresponding to key-value pairs).It calculates the number of Unicode characters in a string;For slice and map, it calculates the number of elements or key-value pairs it contains.If for non-above types (such as pure numbers{{ 123 | length }}and a boolean value{{ true | length }})lengthThe filter usually returns0But it is recommended to avoid using unexpected types as much as possible to ensure clarity in logic.
2.lengthWhen calculating the length of Chinese or multi-byte characters, is the filter calculated by bytes or by characters?
Of Security CMSlengthThe filter processes strings byUnicode character countIt is calculated. This means that a Chinese character (such as “你”) and an English character (such as “a”) are both counted as1length, this conforms to our usual habit of counting 'word count', rather than the original byte length.
**3. In addition to checking if it is equal to0,length_iswhat other length checks can it be used for