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:
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.
- For example,
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.
- For example,
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.
- For example,
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.
- For example:
- 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.
- For example, there is a list that is
2.nilORnilVariable processing:
In the context of the AnQiCMS template,nilIt usually means “none” or “empty”.
- The variable itself is
nilor 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