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 template logic more efficiently and accurately.
lengthFilter: Content Length "Counter"
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, which can be used directly 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 a key-value pair (map).
- For 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 a letter, number, or Chinese character, it is considered 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:
lengthFilters are commonly used when you need to display content word count, limit loop iterations, or check if a list is empty, etc.
Usage example:
{# 获取字符串长度 #}
{% 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: Content length 'judge'
length_isThe filter is more like a 'judge', its role is to check whether the length of the content isexactly equal tothe number you specify. It does not return the specific length, but directly gives out 'yes' (TrueOr No (FalseBoolean result of it.
A very important restriction is, according to AnQiCMS design,length_isFilterCurrently it can only be used to judge the length of a string.And it requires the input to be a number. If you try to use it for a non-string type, or if the value being compared is not a number, it will not work as expected, which may cause 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 title must reach a certain length to display a specific style, etc.
Usage example:
{# 判断字符串长度是否等于指定值 #}
{% 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> #}
Guide to Core Differences and Selection
In simple terms,lengthIt is a length_isIt is a "judge", which gives a "yes" or "no" boolean answer based on the preset value.
- Return type varies:
lengthReturn oneInteger(Actual length of content).length_isReturn oneBoolean(TrueorFalse)
- Scope of application varies:
lengthCan be usedString, array, key-value pair.length_isCan only be used for strings.
- Different emphasis on usage:
lengthFocus onObtain and useThe specific length of content (e.g., displaying word count, making size comparisons> <)length_isFocus onPrecise judgmentDoes the length of content match a specific value (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 content with other numbers (such as
myString|length > 100)
When to uselength_isFilter?
- When you need to precisely determine whether the length of a string is exactly equal to a specific number (such as
myPhone|length_is:11) - When your logic depends on the exact length of a string, for example, when requiring that the user's input code must be 6 digits.
Understanding the subtle differences between these two filters can make us more skilled in AnQiCMS template development, writing more precise and logical code, thereby providing users with a better website experience.
Frequently Asked Questions (FAQ)
Q:
length_isCan the filter be used to determine the length of an array or list? A:According to the current design of the AnQiCMS template,length_isFilterIt can only be used to determine 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 inequality operator. For example, to judgemyStringThe length is not equal to 10, you can write{% if myString|length != 10 %}. Althoughlength_isIt returns a boolean value, but it only judges equality, inequality is usually usedlengthCombine!=More flexible.Q:
lengthDoes the filter count Chinese characters as one character per length in the calculation? A:Yes,lengthThe filter calculates the length of a string based on the actual character count in UTF-8 encoding.This means that whether it is a letter, number, or Chinese character, it is considered a character and counted in the total length. For example,"你好"The length is 2.