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 these contents is the key to dynamic display and logical judgment.
The template engine of AnQi CMS provides simple and efficient built-in features, 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,length_isand it allows you to easily perform length condition judgments.
Easy to masterlengthFilter
lengthThe filter, as the name implies, is used to obtain the length of specified data types. It is very intelligent and can return different length information based on the type of variable you pass in:
- For the string:It calculates the number of actual characters in a string. Even Chinese characters are counted as a single character, not based on 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 of through the pipe symbol|connected tolengthThat's it, for example:{{ 您的变量|length }}.
Let's look at a few 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 indicateslengthThe filter can accurately calculate the length of strings containing both Chinese and English characters.
Get the length of an array or list
When processing article lists, image lists, or tag lists,lengthThe filter can help you quickly understand the amount of data. For example, you might 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 one will be displayed{{ articles|length }}will be displayed5Similarly, iftagsthe list returns 3 tags, they will be displayed3This is very useful for scenarios such as controlling the number of loop iterations, displaying a summary of the total count, etc.
Get the number of key-value pairs or object properties
In the template of AnQi CMS, 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|lengthit will returnparamsThe number of parameters contained in the set, which helps you dynamically display the completeness of customized information.
Combinelength_isPerform conditional judgment
Sometimes, we need to make judgments based on the length rather than just obtaining it, for example, 'Display 'No content' if the number of articles is zero'. At this time, length_isThe filter comes into play.
length_isThe filter is used to determine if the length of a variable is equal to a specified value, it will returnTrueorFalse(a boolean value).
The usage method is{{ 您的变量|length_is:数字 }}, which is usually used withiflogical 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 length of the list is 0, the page will display Otherwise, it will display the number of articles and list them.This allows your template to adjust the displayed content in real-time based on the data, enhancing the user experience.
Masterlengthandlength_isFilter, making your CMS template development and content operation more flexible and efficient. Whether it's fine-grained layout or optimizing user interaction, they will be indispensable tools for you.
Common Questions (FAQ)
1.lengthandlength_isWhat are the main differences between these filters?
lengthFilters are used togetthe actual length of a string, array, or key-value pair, which will return a specific number. Whilelength_isfilters are used toJudgmentWhether the length of a variable is equal to the number you specify, it will return a boolean value (TrueorFalse) and is usually used in{% if %}such conditional statements.
2.lengthHow is the filter calculating string length, by bytes or by characters?In the template engine of AnQi CMS,lengthIs the filter calculating string length bythe actual number of characters?This is calculated rather than by the number of bytes. This means that whether it is an English letter, a number, or a Chinese character, 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, apart fromlength_is:0is there any more concise way?Of course there is! The template engine of Anqi CMS provides{% for ... empty ... endfor %}This very elegant structure is used to handle the case of an empty list. Whenforthe list being traversed is empty,{% empty %}and{% endfor %}the content between them will be displayed without executing the normal loop body.
For example:
{% archiveList myArticles %}
{% for article in myArticles %}
<p>{{ article.Title }}</p>
{% empty %}
<p>暂无相关文章。</p>
{% endfor %}
{% endarchiveList %}
This approach is excellent in terms of code readability and conciseness.