In AnQiCMS template design, we often need to judge or obtain the length of content in order to flexibly control the display of content.lengthfilters andlength_isThe filter is born for this purpose.Although they are all related to 'length', in actual use, their functions and applicable scenarios have obvious differences.Understanding these subtle differences can help us build templates more efficiently and accurately.
lengthFilter: 'Counter' of content length
lengthThe filter acts like a 'counter', its main function is to return the actual length of the specified content.This length is a specific number that can be directly used for display, comparison, or other mathematical operations.
Its application range is relatively wide, not only can it calculate the number of characters in a string, but also the number of elements in an array (slice) or key-value pair (map).
- For a string:
lengthThe filter will calculate the actual number of characters in the string according to the UTF-8 encoding rules.This means that whether it is English letters, numbers, or Chinese characters, they will be treated as a character and counted in the total length.For example, the length of 'hello' is 5, and the length of 'Hello World' is 4. - For the array (slice)It returns the total number of elements in the array.
- For the key-value pair (map)It returns the total number of keys in the key-value pair.
Actual application scenarios:
lengthFilter is often used in situations that require displaying the number of characters in content, limiting the number of repetitions, or checking if the list is empty.
Example Usage:
{# 获取字符串长度 #}
{% set myString = "安企CMS是一个强大的系统" %}
<p>字符串 "{{ myString }}" 的长度是:{{ myString|length }}</p> {# 输出: 11 #}
{% set anotherString = "Hello AnQiCMS" %}
<p>字符串 "{{ anotherString }}" 的长度是:{{ anotherString|length }}</p> {# 输出: 13 #}
{# 获取数组元素数量 #}
{% set myNumbers = [10, 20, 30, 40] %}
<p>数组中元素的数量是:{{ myNumbers|length }}</p> {# 输出: 4 #}
{# 获取键值对(map)键的数量 #}
{% set userInfo = {"name": "张三", "age": 30, "city": "北京"} %}
<p>键值对中键的数量是:{{ userInfo|length }}</p> {# 输出: 3 #}
{# 判断内容是否为空(字符串或数组) #}
{% set emptyContent = "" %}
{% if emptyContent|length == 0 %}
<p>内容为空,可以显示提示信息。</p>
{% endif %}
{% set emptyList = [] %}
{% if emptyList|length == 0 %}
<p>列表为空,可以显示“暂无数据”。</p>
{% endif %}
length_isFilter: A 'judge' for content length.
length_isThe filter acts more like a 'judge', its function is to check the length of the content.exactly equal toThe number you specify. It does not return the specific length, but directly gives 'yes' (True) Or “No” (FalseThe Boolean value result of “)
A very important limitation is that, according to the design of AnQiCMS,length_isFilterCurrently only used to judge the length of the stringAnd the input value must be a number.If you try to use it with non-string types, or the value being compared is not a number, it will not work as expected, which may lead to errors or inaccurate results.
Actual application scenarios:
length_isThe filter is mainly used for strict length matching, such as requiring a certain input field to be a specific number of digits (such as a phone number must be 11 digits), or a certain title must reach a specific length to display a specific style.
Example Usage:
{# 判断字符串长度是否等于指定值 #}
{% set myCode = "AQCMS" %}
<p>字符串 "{{ myCode }}" 的长度是否是 5? {{ myCode|length_is:5 }}</p> {# 输出: True #}
{% set myText = "安企CMS" %}
<p>字符串 "{{ myText }}" 的长度是否是 4? {{ myText|length_is:4 }}</p> {# 输出: True #}
<p>字符串 "{{ myText }}" 的长度是否是 5? {{ myText|length_is:5 }}</p> {# 输出: False #}
{# 结合条件判断 #}
{% set productName = "高端定制网站开发服务" %}
{% if productName|length_is:10 %}
<p>这个产品名称恰好是 10 个字符!</p>
{% else %}
<p>这个产品名称的长度不是 10 个字符。</p>
{% endif %}
{# 错误示范(length_is 不能用于非字符串类型) #}
{% set myList = [1, 2, 3] %}
{# 下面的判断不会按预期工作,因为 length_is 仅用于字符串 #}
{# <p>列表长度是否是 3? {{ myList|length_is:3 }}</p> #}
Core Differences and Selection Guide
In simple terms,lengthIt is a 'ruler' that provides the actual numerical length of the content;length_isIt is a 'indicator', it gives a 'yes' or 'no' boolean answer based on the predefined value.
- Return type different:
lengthReturn oneInteger(Actual content length).length_isReturn oneBoolean value(TrueorFalse).
- Different applicable scope:
lengthCan be used forString, array, key-value pair.length_isOnly applicable to strings.
- Different focus of use:
lengthFocus onObtain and useThe specific length of content (e.g., display word count, size comparison)> <).length_isFocus onPrecise judgmentWhether the length of content meets a specific number (e.g., conditional judgment}==).
When to uselengthFilter?
- When you need to display the length of the content to the user (such as “Total 120 characters”).
- When you need to determine if a list or array is empty (
myList|length == 0). - When you need to compare the length of the content with other numerical values (such as
myString|length > 100).
When to uselength_isFilter?
- When you need to accurately determine whether the length of a string is exactly equal to a specific numerical value (such as
myPhone|length_is:11). - When your logic depends on the exact length of a string match, for example, when the verification code entered by the user must be 6 digits.
Understanding the subtle differences between these two filters can make us more proficient in AnQiCMS template development, writing more precise and logically sound code, thus providing users with a better website experience.
Common Questions (FAQ)
Q:
length_isFilter can be used to judge the length of an array or a list? A:According to the current design of AnQiCMS template,length_isFilterIt can only be used to judge the length of a stringIf you need to determine the length of an array or list, you should uselengtha filter to get the length, and then make a conditional judgment{% if myList|length == 5 %}to achieve.Q: How do I check if the length of a string is not equal to a certain value? A:You can use
lengthThe filter gets the length of the string and then combines it with the template.ifLogical not equal operator. For example, to judgemyStringlength is not equal to 10, you can write{% if myString|length != 10 %}. Althoughlength_isreturns a boolean value, but it only judges equality, inequality is usually usedlengthCombine!=more flexible.Q:
lengthIs the length of the character counted as one for the filter when calculating Chinese characters? A:Yes,lengthFilter calculates the length of a string based on the actual number of characters in UTF-8 encoding.This means that whether it is English letters, numbers, or Chinese characters, they will be treated as a single character and counted towards the total length."你好"The length is 2.