In website content operation, we often encounter scenarios where we need to obtain or judge the length of data.For example, display the word count of an article, the number of items in a list, or determine whether to display an element based on the length of the content.lengthandlength_isThe filter is one of the best. It can help us easily achieve these functions, making the dynamic display and interaction logic of the website more flexible.
lengthFilter: The core tool for getting data length
As the name suggests,lengthFilter 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 Go language slice type in Aigle CMS templates), or a key-value pair (map), it can accurately give the number of elements or the number of characters contained.
Specifically, whenlengthThe filter applied to a string will calculate the actual Unicode character count of the string. This means that both Chinese characters and English characters will be counted as1A length in characters, rather than the original byte count. This is particularly friendly for operators of Chinese websites, as it aligns with our intuitive understanding of 'word count' in daily life.
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 collection of custom parameters),lengththe filter will directly return the number of elements or key-value pairs it contains.
Example Usage:
We have an article objectarchivewhere,Titleis a string,Imagesis an image array,Paramsis a set of key-value pairs:
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.Titlehas a value of"安企CMS让内容管理更简单"Then the output will be:12.Get the length of the array:To display the number of images of the article, you can use it like this:
<p>文章配图数量:{{ archive.Images | length }}</p>If
archive.ImagesContains 3 images, the output will be3.Get the length of the key-value pair (Map):If you want to know how many custom parameters the article has, you can do it like this:
<p>自定义参数数量:{{ archive.Params | length }}</p>If
archive.ParamsDefined 5 custom parameters, the output will be5.
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 determining whether the length of a string is exactly equal to a certain 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, then compares it with the actual length, and returnsTrueorFalse(a boolean value).
This filter is very useful when it is necessary to control the display logic of the page according to the length of the data.such as, when the list is empty, a prompt "no data" is displayed, or when a certain description field is too short, supplementary information is displayed, etc.
Example Usage:
Check if the string length is equal to the specified value:If you need to check whether 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 “Zhang Sanfeng” 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
commentsthe array is empty, a friendly reminder message will be displayed on the page.
Actual application scenarios: Make data management smarter
tolengthandlength_isFilter integrated into template design, which can bring many conveniences and optimizations:
- Dynamic display count:Show 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 article abstract or description, you can use
lengthjudge its length, if it exceeds the preset value, then combineslicethe filter for truncation and add an ellipsis to keep the page tidy. - Conditional element display:When a set of images is empty, the image gallery can be not displayed; when a contact information field is empty, the corresponding contact icon can be not displayed.This avoids visual confusion caused by displaying empty data.
- Form validation prompt:Real-time validation of the length of user input fields on the front end, and provide prompts to enhance user experience.
By using flexibilitylengthandlength_isFilter, we can easily obtain and judge the length of data in the Anqi CMS template, thereby building a more dynamic and user-friendly website interface, making the content operation work twice as effective.
Common Questions (FAQ)
1.lengthFilter can handle what data types? If the object is not a string or an array, what will it return?
lengthThe filter is primarily designed to handle string, Go language slice (corresponding to array or list in templates), and map (corresponding to key-value pairs) types.For strings, it calculates the number of Unicode characters; for slices and maps, it calculates the number of elements or key-value pairs they contain.{{ 123 | length }}, boolean value{{ true | length }})usinglengthFilter usually returns0But it is recommended to avoid using non-expected types to ensure logic clarity.
2.lengthWhen calculating the length of Chinese or multibyte characters, is it calculated by bytes or by characters?
Anqi CMS'slengthThe filter processes strings according tothe number of Unicode charactersThis is calculated. This means that a Chinese character (such as “你”) and an English character (such as “a”) are both counted as1An individual length, which conforms to our usual habits of counting 'word count', rather than the original byte length.
**3. Besides determining whether it is equal to0,length_iswhat other length judgments can it be used for