In managing website content in Anqi CMS, it is often necessary to obtain the character count of text, or to determine how many elements are contained in a list or array.Whether it is to control the page layout, ensure the display length of the title introduction, or to dynamically adjust the display logic based on the amount of data, understanding how to obtain these 'length' information in templates is crucial for creating flexible and user-friendly websites.

The template engine of Anqi CMS provides a concise and powerful way to handle such requirements, the core of which islengthFilter. This filter helps us easily obtain the number of characters in a string, as well as the number of elements in an array or a key-value pair (map).

UselengthFilter to get length

lengthThe filter is the main force in dealing with such 'length' issues. Its usage is very intuitive, simply pass the variable to be measured through the pipe symbol|Pass tolength.

For example, if you want to get the number of characters in a string, you can use it like this:

{{ "欢迎使用安企CMS"|length }}

For Chinese and English,lengthFilters will correctly calculate their character count. For example, the above code will output8It precisely calculates every Chinese character in the string. If it is an English string, such as"hello world"it will output11.

When dealing with arrays or lists,lengthThe filter applies as well, it returns the total number of elements in the array. For example, you getarchiveListan article list from the tagarchives:

{% archiveList archives with type="list" limit="10" %}
    {# ... 循环文章列表 ... #}
{% endarchiveList %}

<p>目前有 {{ archives|length }} 篇文章。</p>

This will displayarchivesThe actual number of articles contained in the array. For a key-value pair (map) or object,lengththe filter will return the number of key-value pairs.

Combineifthe statement is used for conditional judgment

After obtaining the length, we often need to make some conditional judgments based on this number to control the display of content. At this time,lengthFilter is related toifThe combination of logical judgment tags can be used to exert a powerful effect.

For example, you may want to display a prompt message when the article list is empty instead of a blank area:

{% archiveList archives with type="list" limit="10" %}
    {% if archives|length > 0 %}
        <ul>
            {% for item in archives %}
                <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
            {% endfor %}
        </ul>
    {% else %}
        <p>当前分类下暂无文章。</p>
    {% endif %}
{% endarchiveList %}

Or, do you want to limit the display length of a description text, and truncate it if it exceeds a certain number of characters:

{% set description_length = item.Description|length %}
{% if description_length > 50 %}
    <p>{{ item.Description|truncatechars:50 }}</p>
{% else %}
    <p>{{ item.Description }}</p>
{% endif %}

Here we also introduce:truncatecharsA filter that can truncate a string to a specified number of characters and automatically add an ellipsis, which is very useful in practical applications.lengthA good partner of the filter.

length_isFilter: directly judge whether the length matches.

ExceptlengthThe filter directly gets the length, in addition, the security CMS also provides alength_isA filter to determine if the length of a variable is exactly equal to a specific number. This can make the code more concise in certain scenarios.

Usage is as follows:

{% if "你好世界"|length_is:4 %}
    <p>字符串“你好世界”的长度确实是4个字符。</p>
{% else %}
    <p>字符串长度不匹配。</p>
{% endif %}

length_isIt will return directly.TrueorFalseConvenient for you to use inifstatements directly.

Summary

In the Anqi CMS template, to get the actual length of a string or an array, mainly relies onlengthFilter. It can accurately calculate the number of characters or elements, and canifcooperate flexibly with tags to achieve various conditional judgments and dynamic content display.length_isThe filter provides a convenient way to determine if the length is equal to the expected value. Mastering these tools will allow you to better control the presentation of website content and enhance user experience.


Common Questions (FAQ)

Q1:lengthIs the filter counting each Chinese character as one character when calculating the length of a Chinese string?A1:Yes, the AnQi CMS islengthThe filter calculates the string length based on the actual character count in UTF-8 encoding.This means that whether it is English, numbers, or Chinese characters, each character is counted as a unit.

Q2: How can I limit the number of characters displayed in the article title and show an ellipsis for the part that exceeds?A2: In this case, you can combine the use oflengthThe filter makes a judgment and usestruncatecharsThe filter truncates the string. For example:{% if item.Title|length > 30 %}{{ item.Title|truncatechars:30 }}{% else %}{{ item.Title }}{% endif %}.truncatecharsThe filter automatically adds at the truncation point....

Q3: I have a list of images and I want to check if it is empty to decide whether to display the image area. How should I operate?A3: You can uselengthA filter to check the number of elements in a list. For example, if your image list variable is nameditem.Images, you can judge it like this:{% if item.Images|length > 0 %} <div class="image-gallery"> ...显示图片... </div> {% endif %}. The image area will only display when the image list is not empty.