When managing website content in AnQi CMS, we often need to flexibly adjust the page display based on the number of elements in a string, array, or collection.Whether it is to judge whether the length of the article title needs to be truncated, or to count how many documents are under a certain category, or to display the number of user comments, obtaining the 'length' of this content is the key to dynamic display and logical judgment.

The AnQi CMS template engine provides built-in features that are simple and efficient, helping us easily deal with these scenarios. Among them,lengthThe filter is a powerful assistant to get the length of strings, arrays, or key-value pairs, andlength_isThe filter also allows you to conveniently make length conditional judgments.

Master it easilylengthFilter

lengthThe filter, as the name implies, is used to obtain the length of a specified data type. It is very intelligent and can return different length information based on the type of variable you pass in:

  • For strings:It calculates the number of actual characters in a string. Even Chinese characters are counted as a single character, not by byte count.
  • For an array (Slice) or list:It returns the total number of elements contained in the array or list.
  • For a key-value pair (Map) or object:It will return the number of entries in the key-value pair, that is, how many properties there are.

UselengthThe filter is very intuitive, you just need to pass the variable you want to calculate the length through the pipeline symbol|Connected tolengthThat's it, for example:{{ 您的变量|length }}.

Let's look at some actual examples:

Get the length of a string

Suppose you have a piece of text, whether it's English or Chinese, and you want to know its character length:

{# 假设有一个字符串变量 title = "欢迎使用安企CMS" #}
<p>文章标题的字符长度是:{{ title|length }}</p>

{# 假设有一个字符串变量 description = "AnQiCMS is a powerful CMS." #}
<p>描述内容的字符长度是:{{ description|length }}</p>

This code will output separately:10and26This indicates:lengthThe filter can accurately calculate the length of a string containing both Chinese and English characters.

Get the length of an array or list

When processing a list of articles, images, or tags,lengthThe filter can help you quickly understand the amount of data. For example, you may want to know how many documents are on the current page, or how many associated items are under a certain tag:

{% archiveList articles with type="list" limit="5" %}
    <p>当前页面有 {{ articles|length }} 篇文档。</p>
    {# 接着您可以在这里遍历 articles 列表,展示每篇文档的信息 #}
    {% for item in articles %}
        {# ... 展示文档内容 ... #}
    {% endfor %}
{% endarchiveList %}

{% tagList tags with limit="10" %}
    <p>当前文章关联了 {{ tags|length }} 个标签。</p>
    {% for item in tags %}
        {# ... 展示标签信息 ... #}
    {% endfor %}
{% endtagList %}

IfarticlesThe list actually returned 5 documents, so the first{{ articles|length }}it will be displayed5Similarly, iftagsThe list returns 3 tags, it will display3This is very useful for controlling loop iterations, displaying total summary, and other scenarios.

Get the number of key-value pairs or object properties.

In the AnQi CMS template, some configurations or custom fields may exist in the form of key-value pairs.lengthThe filter can also calculate the number of entries for these key-value pairs. For example, if you have customized a set of document parametersparamsand want to know how many parameters there are:

{% archiveParams params %}
    <p>当前文档设置了 {{ params|length }} 个自定义参数。</p>
    {% for item in params %}
        <p>{{ item.Name }}:{{ item.Value }}</p>
    {% endfor %}
{% endarchiveParams %}

Hereparams|lengthwill returnparamsThe number of parameters contained in the set, helping you dynamically display the completeness of custom information.

Combinelength_isMake a conditional judgment.

Sometimes we need to make a judgment based on the length directly, such as 'If the number of articles is zero, display 'No content'.' At this time,length_isThe filter comes into play.

length_isA filter used to determine if the length of a variable is equal to a specified value, it will returnTrueorFalse(a boolean value).

The method of use is{{ 您的变量|length_is:数字 }}, which is usually配合ifused with logical judgment tags:

{% archiveList articles with type="list" limit="0" %} {# 这里故意设置 limit 为 0,模拟列表为空 #}
    {% if articles|length_is:0 %}
        <p>抱歉,当前分类暂无文章内容。</p>
    {% else %}
        <p>发现 {{ articles|length }} 篇文章,快来阅读吧!</p>
        {% for item in articles %}
            {# ... 展示文档内容 ... #}
        {% endfor %}
    {% endif %}
{% endarchiveList %}

In this example, ifarticlesThe list length is 0, the page will display If not, the number of articles will be displayed along with the list of articles. This allows the template to adjust the display content in real-time based on the data, enhancing the user experience.

Masterlengthandlength_isThe filter can make your safe CMS template development and content operation more flexible and efficient, whether it is refined layout or optimized user interaction, they will be indispensable tools for you.


Frequently Asked Questions (FAQ)

1.lengthandlength_isWhat are the main differences between these two filters? lengthFilters are used toObtainThe actual length of a string, array, or key-value pair, it will return a specific number. Andlength_isThe filter is used tothe judgment.Whether the length of a variable is equal to the number you specified, it will return a boolean value (TrueorFalse) and is usually used in{% if %}such conditional statements.

2.lengthIs the filter calculating the length of a string by bytes or characters?In the AnQi CMS template engine,lengthThe filter calculates the length of a string byactual character countIt is counted as characters, not by bytes. This means that whether it is English letters, numbers, or Chinese characters, each one is counted as a character. For example,{{ "你好世界"|length }}The result is4.

3. If I want to know if a list (array) is empty, otherlength_is:0There are other more concise methods?Of course! The template engine of AnQi CMS provides{% for ... empty ... endfor %}This is a very elegant structure to handle the case of an empty list. Whenforthe list being traversed by a loop is empty,{% empty %}and{% endfor %}the content between the brackets will be displayed and the normal loop body will not be executed. For example:

{% archiveList myArticles %}
    {% for article in myArticles %}
        <p>{{ article.Title }}</p>
    {% empty %}
        <p>暂无相关文章。</p>
    {% endfor %}
{% endarchiveList %}

This method is very excellent in terms of code readability and conciseness.