The `length` filter considers empty values or nil elements when calculating the length of an array or a key-value pair?

Calendar 👁️ 68

AnQiCMS template inlengthFilter: Deep Understanding of Its Length Calculation Mechanism

During the development of AnQi CMS templates, we often need to judge the length of strings, lists (arrays) or key-value pairs (Maps) in order to control the display logic of content.lengthThe filter is exactly for this purpose, it can help us get the length information of these data types. However, whether it considers null values ornilThe element, many users may feel puzzled. Let's delve into it in depth.lengthThis behavior of the filter.

lengthThe basic function of the filter

First, let's reviewlengthThe basic working mode of the filter on different data types:

  1. For strings: lengthThe filter calculates the actual number of UTF8 characters in a string. This means that an English letter counts as one character, and a Chinese character also counts as one character.

    • For example,"john doe"The length is 8.
    • And"你好世界"The length is 4.
    • An empty string""The length is then 0.
  2. For a list (or a slice in Go language): lengthThe filter counts the total number of elements in the list, which is also the number of indices.

    • For example,['a', 'b', 'c', 'd']The length of this list is 4.
    • An empty list[]has a length of 0.
  3. For key-value pairs (or Map in Go language): lengthThe filter counts the number of key-value pairs entries.

    • For example,{"name": "AnQiCMS", "version": "v3"}The length of this key-value pair is 2.
    • An empty key-value pair{}has a length of 0.

Core issue: the effect of null andnilelements

This is where many users feel confused. The Anqi CMS template engine (based on Pongo2, similar to Django templates) handleslengthfilters, for null values andnilThe consideration of elements has its specific logic:

1. Empty string ("") handling:

  • As an independent string:If a variable itself is an empty string (""Use it forlengthThe filter results are0.
    • For example:{{ "" | length }}It will output.0.
  • As an element/value of a list or dictionary:If an empty string isAs an element in a listOrAs a value associated with a key in a key-value pairIt still occupies a position. In this case,lengthThe filter will include it in the calculation.
    • For example, there is a list that is["item1", "", "item3"]including an empty string. Use it forlengthfilter, the result will be3because an empty string itself is also a placeholder.
    • Similarly, if the key-value pair is{"key1": "value1", "key2": ""}the length will be2because"key2"Although it corresponds to an empty string, it is still a valid key-value pair entry.

2.nilORnilVariable processing:

In the context of the AnQiCMS template,nilIt usually means “none” or “empty”.

  • The variable itself isnilor uninitialized:If a variable (whether a string, list, or dictionary) itself isnilor it is a completely uninitialized empty set, then it useslengthThe filter usually gets0. This meets our intuitive need to determine whether a set is empty.
    • For example, if you define an empty list {% set my

Related articles

How to dynamically get the actual character length of the article abstract in AnQiCMS template?

In website content operation, we often need to fine-tune the display form of content, especially for information such as article summaries, which not only affects the first impression of users but also plays an important role in the inclusion of search engines.In AnQiCMS (AnQiCMS) flexible and powerful template system, getting the actual character length of the article abstract is a very practical skill, which can help us achieve more accurate content presentation.Why do you need to get the actual character length of the article summary?The article abstract is the core essence of the content, usually used on list pages

2025-11-07

The `index` filter calculates the position of Chinese characters in a string, how many positions does a Chinese character occupy?

In AnQi CMS template development, the `index` filter is a very practical tool that helps us locate the position of a specific substring in a string.However, when dealing with content containing Chinese characters, its performance in position calculation may confuse some users.How many positions does a Chinese character occupy in the `index` filter?What kind of logic is hidden behind this? Let's delve deeper together.### `index` filter's working principle In simple terms

2025-11-07

What result does the `index` filter return when the keyword does not exist in the string?

In the flexible and powerful template system of AnQiCMS, we often need to process and judge various text content on the page.Among them, the `index` filter is a very practical tool that helps us quickly locate the first occurrence of a keyword in a string or array. Then, when we need to search for a keyword that does not exist at all in the target string or array, what result will the `index` filter return to represent this situation?The answer is: it will return a clear **-1**.In the conventions of programming and template processing

2025-11-07

The `index` filter returns the index position or other identifier when searching for a specific value in an array?

In Anqi CMS template development, proficiently using various filters can make the display of page content more flexible and efficient.Among them, the `index` filter is a commonly used tool for handling array or string search tasks.However, many users may have a question when they first contact: When searching for a specific value in an array, does the `index` filter return the index position of the value or some other identifier?

2025-11-07

How to use the `length_is` filter to determine if the character length of a user comment meets the specified requirements?

In the daily operation of AnQi CMS, we often need to manage user-generated content, especially user comments, which directly affect the activity and professionalism of the website.Content length control is one of the common requirements, for example, we hope that the comments are not too short to seem perfunctory, nor too long to affect the reading experience.Anqi CMS's flexible template engine provides a variety of powerful filters that can help us easily implement these seemingly complex validation logic.

2025-11-07

How will the `length_is` filter handle non-string or numeric types when comparing lengths?

AnQiCMS provides rich template filters to help us flexibly handle and display data.Among them, the `length_is` filter is often used to determine whether the length of the variable meets expectations.However, during use, one may encounter a question: What will the system do if we pass non-string or non-numeric data to the `length_is` filter for length comparison?This is not just a technical detail, but also about how we avoid potential errors in template design and ensure the accuracy of data display.

2025-11-07

How to accurately slice an array in the AnQiCMS template?

During the template development process of AnQi CMS, we often need to flexibly handle the data displayed on the page, especially when the data is presented in the form of a list or sequence.Imagine that you are designing a product list page, where you need to select the top 5 most popular products from an array of dozens of products to display at the top of the page; or, you may need to truncate a long string from the content of an article detail page to use as a summary.

2025-11-07

How to ensure the integrity of the sliced result when截取中文字符串 using the `slice` filter (to avoid half characters)?

In Anqi CMS template development, the `slice` filter is a commonly used tool for handling strings and arrays.It can help us conveniently extract a part of the content, whether it is several elements of a list or a specified segment of a long text.However, when it comes to cutting Chinese character strings, if you are not familiar with the underlying working principle, you may encounter a common and annoying problem: the cutting result appears as 'half a character' or garbage code.

2025-11-07