Check array or string length: `length_is` filter practical skills in AnQiCMS template?

Calendar 👁️ 73

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>
    

Related articles

How to get the actual character length of a string (including Chinese) in Anqi CMS template?

When making website templates, we often need to control the display length of text to ensure that the page layout is beautiful and information is conveyed efficiently.For example, limit the number of characters in the article summary, ensure that the text in the navigation menu is not too long, or perform character count verification before submitting the form.The AnQi CMS template engine provides a very practical filter called `length`, which can easily obtain the actual character length of strings, arrays, or key-value pairs, and has good support for Chinese.

2025-11-08

What is the impact of the `slice` filter on performance when handling large strings or large arrays?

Performance considerations for the `slice` filter in AnqiCMS when handling large strings or large arrays The `slice` filter is a very convenient feature in the AnqiCMS template engine, allowing users to extract specified length data segments from strings or arrays.Whether it is to extract an abstract from a long article or to select a portion of elements from a large data list, `slice` can provide flexible control.

2025-11-08

How to use the `slice` filter to get all elements of the array except the first and last?

When managing and displaying content in Anqi CMS, the flexibility of the template is the key to improving website operation efficiency.The built-in Django template engine syntax provides many practical filters, allowing us to handle various data in a concise and efficient manner.Today, let's delve deeply into a very practical filter——`slice`, which can help us accurately cut a specific part of an array (or called slice in the Go language environment), especially on how to get all elements except the first and last one.Content display is the core of website operation, whether it is an article list

2025-11-08

What result will the `slice` filter return when operating on an empty string or array?

In AnQi CMS template development, we often need to perform fine-grained control and processing of content.Among them, the `slice` filter is a very practical tool that allows us to extract the specified part from a string or array (usually referred to as a slice in Go language).This feature is very convenient when displaying article summaries, partial elements in pagination lists, or processing dynamic data.

2025-11-08

How can you quickly get the first element of an array or the first character of a string in a template?

In Anqi CMS template development, efficiently obtaining data is the key to improving website performance and user experience.Whether it is dealing with a document list or image group obtained from the background, or operating on a common string, we often encounter the need to extract the first element of an array or the first character of a string.The Anqi CMS, based on Go language and Django template engine syntax, provides various intuitive and practical methods to achieve this goal.

2025-11-08

What are the limitations of the `first` and `last` filters when getting the first and last elements of a string/array?

In Anqi CMS template design, the `first` and `last` filters are two concise and practical tools designed to help us quickly obtain the first or last character of a string, as well as the first or last element of an array.They perform well in handling simple data structures, but in practical applications, if their internal mechanisms are not well understood, they may encounter some unexpected behaviors.Firstly, let us understand their core functions. For string type data, `first` will return the first character, and `last` will return the last character

2025-11-08

`split` filter: How to split a long string into an array by a specific delimiter?

In the powerful functions of AnQi CMS, the flexibility of content display has always been our focus.Sometimes, you may need to enter a long string of information in a field on the back end, such as multiple keywords for an article, multiple parameters for a product specification, or a list of skills for a team member.This information is usually connected by specific delimiters (such as commas, semicolons, or pipes).However, when you want to display this information individually on the front page, even to style each part differently, the problem arises: how can you conveniently split this long string into independent items?

2025-11-08

What is the difference between the `make_list` filter and the `split` filter? When should `make_list` be used to split strings?

In AnQi CMS template development, we often need to process string data, such as splitting a long string into multiple independent parts for traversal, display, or further logical judgment.Anqi CMS provides a variety of filters (Filters) to help us complete these tasks, among which `make_list` and `split` are two commonly used string splitting tools.Although they can all convert strings to lists (or arrays), there are obvious differences in their functional focus and usage scenarios.

2025-11-08