When building a website, we often need to dynamically adjust the page display or execute different logic based on the length of the content.For example, if a title is too long and needs to be truncated, or if a list is empty and a prompt such as 'No content available' needs to be displayed.length_isThe filter is exactly the tool for handling such precise length judgments.

The AnQiCMS template system, with its syntax similar to Django, provides many powerful built-in filters to help users flexibly control the front-end display without writing complex backend code.length_isThe filter is one of the most practical tools, which can help us handle content length-related conditional judgments more elegantly and intuitively.

Deep understandinglength_isFilter

length_isThe core function of the filter is to compare whether the actual length of a variable is equal to the specified value and return a boolean value (TrueorFalse) Its syntax is concise and clear: {{ 变量 | length_is: 期望长度 }}.

For example, if we have a stringmy_string = "AnQiCMS"to check if its length is 7, we can use it like this:

{{ my_string|length_is:7 }} {# 输出 True #}

If the expected length is 10:

{{ my_string|length_is:10 }} {# 输出 False #}

It is worth noting that according to the AnQiCMS filter document,length_isPrimarily designed for strings and arrays (often referred to as slices/slice in Go language). Using it directly on numeric types does not yield the expected comparison results, as it attempts to convert the number to a string and then calculate the length, or simply returnFalseTherefore, when using it, be sure to ensure that the object being operated on is a string or an array type.

lengthwithlength_isThe wisdom of choice:

In the AnQiCMS template, there is also a closely related filter that islengthThey are all related to 'length', but they have clear distinctions in usage.

lengthThe filter will directly return the actual length of a string or an array, resulting in an integer. For example:

{% set my_string = "安企CMS" %}
<p>字符串 "{{ my_string }}" 的长度是:{{ my_string|length }}</p> {# 输出 5 #}

length_isThen, give the judgment result directly(TrueorFalse)

When should it be usedlength, when should it be usedlength_isWhat?

When you needGet and display the specific length of the contentat the time, for example, when counting the number of words in article titles or displaying the number of comments, you uselengthfilter. And when you needto make a conditional judgment based on a precise lengthFor example, if the title is exactly 15 characters long, or if the list of related articles is empty (length is 0), thenlength_isIt is a more direct and elegant choice. It makes the intent of the template code clearer and more readable.

Let's explore through some specific examples.length_isPractical techniques.

length_isUseful scenarios and techniques

  1. Precise control of content truncation and promptsIn web design, in order to keep the layout neat or improve readability, we often limit the length of titles, summaries, and other text content.length_isIt can help us accurately determine whether the text has reached a certain critical length and decide whether to perform additional processing accordingly.

    For example, when the product description may be empty, we can uselength_is:0To judge and display different prompt information:

    {% set product_description = archive.Description %} {# 假设获取到的产品描述 #}
    {% if product_description|length_is:0 %}
        <p class="no-description">该产品暂无详细描述,敬请期待!</p>
    {% else %}
        <p class="product-detail">{{ product_description }}</p>
    {% endif %}
    

    This example is compared{% if not product_description %}More accurate, because it only checks the length of 0 instead of all 'falsy' values (such as empty strings, nil).

  2. Dynamically adjust the display logic of list itemsWhen displaying something like a picture gallery, related articles, or comment list, we often need to determine if the list is empty.If the list is empty, it may be necessary to display a friendly prompt; if it contains a specific number of items, it may be necessary to adjust the layout.

    For example, check if the list of related articles is empty: “`twig {% archiveList related_articles with type=“related” limit=“5” %} {% if related_articles|length_is:0 %}

    <p class="empty-list">暂无相关文章推荐。</p>
    

    {% else %}

    <div class="related-articles-section">
        <h3>相关推荐</h3>
        <ul>
            {% for article in related_articles %}
                <li><a href="{{ article.Link }}">{{ article.Title }}</a></li>
            {% endfor %}
        </ul>