When managing website content in Anqi CMS, you often encounter situations where you need to get the number of characters in a text or determine how many elements are contained in a list or array.In order to control the page layout, ensure the display length of the title introduction, or dynamically adjust the display logic based on the amount of data, it is crucial to understand how to obtain these 'length' information in the template for creating flexible and user-friendly websites.

The Anqi CMS template engine provides a simple and powerful way to handle such requirements, with the most core being thatlengthThe filter that helps us easily get 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 for dealing with such "length" issues. Its usage is very intuitive, just pass the variable to be measured through the pipe symbol|pass tolengthJust do it.

For example, if you want to get the character count of a string, you can use it like this:

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

For Chinese and English,lengthThe filters will correctly calculate their character count. For example, the above code will output8It accurately calculates each Chinese character in the string. If it is an English string, such as"hello world"it will output:11.

When dealing with arrays or lists,lengthThe filter applies as well, it returns the total number of elements in the array. For example, you can fromarchiveLista list of articles labeledarchives:

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

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

This will display.archivesThe actual number of articles contained in the array. For key-value pairs (map) or objects,lengthThe filter will return the number of key-value pairs.

CombineifThe statement performs conditional judgment

After getting the length, we often need to make some conditional judgments based on this number to control the display of content. At this time,lengththe filter meetsifUsing logical judgment tags in combination can 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, would you like to limit the display length of a description text, truncating 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 introducedtruncatecharsA filter that can truncate a string to a specified number of characters and automatically add an ellipsis, which is a good partner in practical applications.lengthA filter is a good partner.

length_isFilter: directly judge whether the length matches.

exceptlengthThe filter directly gets the length, and the Anqi CMS also provides alength_isA filter used to determine if the length of a variable is exactly equal to a specific number. This can make the code more concise in some scenarios.

The usage is as follows:

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

length_isIt will return directlyTrueorFalseto make it convenient for you inifUse directly in the statement.

Summary

In the AnQi CMS template, to obtain the actual length of a string or an array, mainly rely onlengthfilter. It can accurately calculate the number of characters or the number of elements, and can be used withifTags can be flexibly combined to implement 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.


Frequently Asked Questions (FAQ)

Q1:lengthDoes the filter count a Chinese character as one character when calculating the length of a Chinese string?A1: Yes, it is in AnQi CMS.lengthThe filter calculates the length of a string 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: If I need to limit the number of characters displayed in the article title and show an ellipsis for the part that exceeds, what should I do?A2: In this case, you can combine the use oflengthThe filter makes a judgment and usestruncatecharsThe filter is used to truncate strings. 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 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 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.