In Anqi CMS template development, flexible handling and displaying data is the key to improving website interactivity and user experience.Understanding how to efficiently detect the length of strings, arrays, or mappings (similar to dictionaries or associative arrays in other programming languages) is the foundation for content operations and template customization.This article will introduce the Anqi CMS template engine in detaillengthandlength_isThese practical filters help you better control the display of page content.

lengthFilter: A tool to get the length of data

When you need to know how many characters a string has, or how many elements a list (array) contains, or how many key-value pairs a key-value pair (map) has, lengthThe filter is your helpful assistant. It can accurately return the length of the data, allowing you to make various logical judgments and content displays based on this information.

Working principle:

  • String: lengthThe filter calculates the actual number of characters in a string. It is worth noting that it calculates according to UTF-8 encoding, which means that both Chinese characters and English characters are counted as a single length unit.For example, the length of 'Hello World' is 4, while 'john doe' is 8.
  • Array/slice:For list or array type data,lengthReturn the total number of elements contained.
  • Mapping/dictionary:For data of key-value pair type,lengthReturn the number of key-value pairs contained.

Usage example:

Assuming you have a variablemessageStoring a piece of text, or aitemsarray.

{# 假设 message 变量值为 "欢迎使用安企CMS" #}
<p>消息长度:{{ message|length }}</p>

{# 假设 productList 变量为一个包含3个产品的数组 #}
{% set productList = ["产品A", "产品B", "产品C"] %}
<p>产品数量:{{ productList|length }}</p>

{# 假设 userData 变量为一个映射(map) #}
{% set userData = {"name": "张三", "age": 30} %}
<p>用户信息字段数量:{{ userData|length }}</p>

Actual application scenarios:

  • Word count:Display the word count of each article in the article list page, or limit the number of characters in user comments.
  • Content prompt:When the list is empty, display a prompt such as 'No content available'; when the list elements are too many, display pagination or 'See more' button.
  • Layout adjustment: Select different CSS styles based on the length of the title to avoid overlong titles from disrupting the layout.

length_isFilter: Precisely judge whether the length matches.

withlengthThe filter directly returns different length values,length_isThe filter is used to determine whether the length of a data item isequalsThe value you specify. Its return result is a boolean value (TrueorFalse) which is very suitable for precise length matching in conditional statements.

Working principle: length_isIt will get the length of the variable, then compare it with the target length you provide. If they are equal, it will returnTrue; otherwise, it returnsFalse. It is mainly used for the length judgment of strings, arrays, or mappings.

Usage example:

Assuming you need to check if the user's nickname is exactly 6 characters long, or if the array of a certain configuration item contains a specific number of sub-items.

{# 假设 userName 变量值为 "admin888" #}
<p>用户名为6个字符吗?{{ userName|length_is:6 }}</p> {# 返回 False #}

{# 假设 tags 变量值为 ["SEO", "CMS", "Go"] #}
{% set tags = ["SEO", "CMS", "Go"] %}
<p>标签数量是3个吗?{{ tags|length_is:3 }}</p> {# 返回 True #}

{# 假设 emptyList 变量为空数组 #}
{% set emptyList = [] %}
<p>列表为空吗?{{ emptyList|length_is:0 }}</p> {# 返回 True #}

Actual application scenarios:

  • Form validation:Verify if the user's input phone number is 11 digits long and if the password meets the minimum or fixed length requirements.
  • Permission control:Check if a user group has a specific number of permissions to decide whether to display a feature.
  • UI logic:Based on the number of elements in a specific data set, certain UI elements are displayed or hidden, for example, when the image list has only one image, the carousel control button is not displayed.

How to choose the appropriate filter?

SelectlengthOrlength_isIt mainly depends on your needs:

  • If you need to get the specific length of the dataFor example, displaying "Total X records" on the page, or using the length for subsequent mathematical operations, thenlengthThe filter is your preferred option.
  • If you need to judge whether the length of the data meets a specific conditionfor example, "if the length is equal to 10", and you want to get a boolean result directly for{% if %}the statement, thenlength_isFilters make your code more concise and readable.

In Anqi CMS template development, these two filters are similar in function, but have different focuses in different scenarios.Master their usage, and you will be more skilled in handling dynamic content and implementing complex logic.


Frequently Asked Questions (FAQ)

1.lengthDoes the filter have a difference in calculating the length of Chinese and English strings?No difference.lengthThe filter considers the string length to be a sequence of UTF-8 encoded characters when calculating.This means that a Chinese character and an English character are counted as 1 unit of length, rather than by the number of bytes.Therefore, whether it is a mix of Chinese and English,lengthIt will return the number of characters you see.

2.length_isCan the filter be used to determine the length of numbers? For example, to determine the length of numbers12345Is the length 5? length_isThe filter is mainly used to judge the length of strings, arrays, or mappings. If it is used directly on a numeric typelength_is, the result may not be as expected. For example{{ 12345|length_is:5 }}May returnFalseIf you indeed need to determine the 'length' of a numeric value (i.e., its character count as a string), it is recommended to convert it to a string first