In the AnQiCMS templatelengthFilter: Deep Understanding of its Length Calculation Mechanism

During the template development process in AnQi CMS, 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 created for this purpose, it can help us get the length information of these data types. However, whether it considers null values or not when calculating the length is a question.nilElements, many users may feel confused. Next, let's delve into it.lengthThis behavior of the filter.

lengthThe basic function of the filter.

First, let's take a look back atlengthThe basic way filters work on different data types:

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

    • For example,"john doe"has a length of 8.
    • while"你好世界"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 pair 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: Null value andnilelement's influence

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

1. Treatment of an empty string (""):

  • As an independent string:If a variable itself is an empty string (""),using itlengthThe filter, of course0.
    • For example:{{ "" | length }}It will output0.
  • As an element/value of a list or key-value pair:If an empty string isAn element existing in a list, orAs a value associated with a key in a key-value pair, it still occupies a position.In this case, lengthThe filter will take it into account.
    • For example, there is a list that is["item1", "", "item3"][en] Among them is an empty string. Using itlength[en] as a filter, the result would be3[en] because an empty string itself is also a placeholder
    • [en] Similarly, if the key-value pair is{"key1": "value1", "key2": ""}[en] its length would be2because"key2"Although it corresponds to an empty string, it is still a valid key-value pair entry.

2.nilelement ornilvariable handling:

in the context of AnQiCMS templates,nilIt usually means “none” or “empty”.

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