In AnQiCMS template development, we often need to process and judge the data displayed on the page.Among these, getting the length of a string, array, or key-value pair is a very basic and practical operation.lengthThe filter makes this task simple and intuitive.
Get to knowlengthFilter: A tool to get the length of data
lengthThe filter, as the name implies, is a tool used to obtain the "length" of data. Whether you want to know how many characters a piece of text has, how many items a list contains, or how many key-value pairs a set of configuration options has,lengthThe filter can help you easily achieve this. Its design philosophy is to simplify complexity, allowing template developers to complete a quick perception of the scale of data with the least amount of code.
Its basic usage is very concise, just need to use the pipeline symbol after the variable to get the length|Continuelengthand it is done:{{ obj|length }}.
lengthFilter application on different data types
lengthThe filter can intelligently identify the data type of the incoming data and return the corresponding length information.
1. Get the length of a string.
When faced with a piece of text, sometimes you need to know how many characters it has.lengthThe filter calculates the actual number of UTF-8 characters in the string. This means that whether it is English letters, numbers, or Chinese characters, each character is counted as 1.
For example, if you have a variable namedtitlewith the value “AnQi CMS makes your website more secure”, to get its character length, you can use the following:
{% set title = "安企CMS让你的网站更安全" %}
<p>标题的字符长度是:{{ title|length }}</p>
This code will execute and then, the page will display: 'The character length of the title is: 13'.Similarly, if it is an English title, such as 'AnQiCMS is Great', its length will also be returned according to the actual number of characters.
2. Get the length of the array (Slice)
In the AnQiCMS template, we often get fromarchiveList/categoryListRetrieve a series of data from tags, which is usually in the form of an array (or a Slice in Go language). To get the number of elements in this array,lengthThe filter applies equally.
Assuming you pass througharchiveListLabel to get a group of article data and name itarchives:
{% archiveList archives with type="list" limit="10" %}
<p>当前页面共有 {{ archives|length }} 篇文章。</p>
<ul>
{% for item in archives %}
<li>{{ item.Title }}</li>
{% endfor %}
</ul>
{% endarchiveList %}
IfarchivesThere are actually 5 articles, then the page will display: "There are a total of 5 articles on the current page."
3. Get the length of the key-value pair (Map or Struct)
When data exists in the form of key-value pairs, such as througharchiveParamsThe tag retrieves the document custom parameters, or other custom structures (Struct) or maps (Map) objects,lengthThe filter calculates the number of key-value pairs in it.
For example, if you get a document's custom parameterparams:
{% archiveParams params %}
<p>该文档共有 {{ params|length }} 个自定义参数。</p>
<ul>
{% for item in params %}
<li>{{ item.Name }}:{{ item.Value }}</li>
{% endfor %}
</ul>
{% endarchiveParams %}
If the document is set to 3 custom parameters (such as "author", "source", "publish date"), then the page will display: "The document has 3 custom parameters."
Advanced usage:length_isFilter
In addition to simply getting the length, AnQiCMS also provides a related filterlength_isUsed to determine whether the length of a variable is equal to a specified value. This is very useful when making conditional judgments.
length_isThe filter takes a parameter, which is the length value you want to compare. It returns a boolean value (TrueorFalse)
For example, to determine whether a variableitemshas a length of 5:
{% set items = "这是一个测试字符串" %}
{% if items|length_is:8 %}
<p>字符串“{{ items }}”的长度正好是 8。</p>
{% else %}
<p>字符串“{{ items }}”的长度不是 8。</p>
{% endif %}
length_isThe filter applies not only to strings, but also to arrays and key-value pairs, with the same judgment logic aslengththe filter is consistent.
Summary
lengthThe filter is a very basic but powerful tool in the AnQiCMS template, making it easy to obtain the length of strings, arrays, or key-value pairs. Combinedlength_isFilter, we can make more refined data display and logical control in the template, thereby building more flexible and more responsive website content.Master these filters, and it will greatly enhance your content operation efficiency and template development experience on the AnQiCMS platform.
Frequently Asked Questions (FAQ)
lengthWhat data types can the filter calculate the length of?lengthThe filter is mainly used to calculate the number of characters in a string, the number of elements in an array (Slice), and the number of key-value pairs in a map (or struct).For other types of data, such as pure numbers, it may return unexpected results or even report an error.lengthWhat is the counting method for Chinese characters in the filter?lengthThe filter counts Chinese characters and English characters in the same way, both are counted according to the actual UTF-8 character count.That is to say, a Chinese character (such as “安”) is counted as 1, consistent with the count of an English letter (such as “A”), not the number of bytes.except
lengthWhat are some filters that can be used to determine or process the length of a string or collection?exceptlengthandlength_is,AnQiCMS also provides some other filters related to string or collection length processing. For example,truncatecharsandtruncatewordsyou can truncate strings or HTML content and add an ellipsis;wordcountYou can count the number of words in a string (separated by spaces);sliceThe filter can extract the specified part of a string or array. These filters collectively provide rich choices for the flexible display of template content.