In the template development of AnQi CMS, flexibly controlling the way content is displayed is the key to improving the user experience of the website.length_isA filter is a very practical tool that can help us easily validate the length of user input or data lists in templates and conditionally render based on the validation results.
length_isThe core function of the filter is to determine whether the length of a variable (whether it is a string, an array, or a map) is equal to a certain value we preset. It does not directly return the specific length number, but provides a concise and clear boolean result.True(true) orFalse(False). This 'yes' or 'no' judgment ability makes it an ideal choice for implementing conditional logic in the template.
The usage of this filter is very intuitive: you just need to pass the variable to be checked for length andlength_iscombine it with the filter, and specify an expected length value after the filter via a colon. For example,{{ 变量 | length_is: 期望长度 }}.Here, 'expected length' can be either a number or a string representing a number.It is noted that for string type data, it calculates the actual number of characters (even for Chinese characters, they are counted as one character); for array (or Go language's slice) and map type data, it calculates the number of elements contained within.
length_isThe filter returns a boolean value, which is the cornerstone of its powerful functions. When we use it in the template,{% if %}The boolean value can directly determine whether a section of HTML code should be rendered on the page when the label is used for conditional judgment.This allows us to dynamically adjust the page layout and display content based on the length of the data, thus realizing a more intelligent and user-friendly user interface.
Imagine, in your secure CMS website, there are various scenarios that need to be rendered conditionally based on the length of the data:
Scenario one: Validate the length of the user input
Assuming you are on a comment form or user registration page, and you want to remind users that the username or comment content they enter must meet specific length requirements.length_isFilter, you can easily implement this feature.
{# 假设 username 是用户输入的用户名变量 #}
{% if username|length_is:5 %}
<p class="success-message">您输入的用户名长度符合要求。</p>
{% else %}
<p class="error-message">用户名长度不正确,请确保为5个字符。</p>
{% endif %}
This code will checkusernameIs the length of the variable exactly 5? If so, display a success message; otherwise, display an error prompt.
Scene two: Render based on the number of elements in the data list
When you display a list of articles, products, or tags on a page, you may want to adjust the display style or prompt information according to the number of elements in the list.For example, if a list is empty, you may want to display 'No content available'; if the list has only a few items, you may want to prompt the user to view more.
{% archiveList articles with type="list" limit="10" %}
{% if articles|length_is:0 %}
<div class="empty-list">
<p>当前没有找到任何文章。</p>
<a href="/post-article" class="btn btn-primary">立即发布文章</a>
</div>
{% elif articles|length_is:1 %}
<div class="sparse-list">
<p>文章数量较少,仅有一篇。期待更多精彩内容!</p>
<ul>
{% for item in articles %}
<li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
{% endfor %}
</ul>
</div>
{% else %}
<ul class="article-list">
{% for item in articles %}
<li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% endarchiveList %}
This template code first tries to get up to 10 articles. Then, it useslength_isFilter determines if the article list is empty or has only one article, and renders different prompts and layouts accordingly. If there are more than one article, it is displayed in a conventional list format.
Scene three: Adjust style based on tag quantity
Suppose you want to add a unique visual identifier to articles with a specific number of tags.
{% tagList tags with itemId=archive.Id limit="5" %}
{% if tags|length_is:3 %}
<div class="tags-container special-highlight">
{% for tag in tags %}
<span class="tag-item">{{ tag.Title }}</span>
{% endfor %}
<p>这篇文章有3个精选标签!</p>
</div>
{% else %}
<div class="tags-container normal-style">
{% for tag in tags %}
<span class="tag-item">{{ tag.Title }}</span>
{% endfor %}
</div>
{% endif %}
{% endtagList %}
In this example, if the number of tags associated with the current article is exactly 3, the tag container will apply a special style namedspecial-highlightand display additional information.
It is not difficult to see through these actual exampleslength_isThe powerful role of the filter in the Anqi CMS template.It provides a concise and effective way to make logical judgments based on data length, thus helping you build a more flexible, responsive, and user-friendly website.length_isFocuses on the precise length equality rather than size comparison.
Common Questions (FAQ)
length_isfilters andlengthWhat are the differences between filters?lengthThe filter is used to get the actual length of a variable (for example, the number of characters in a string or the number of elements in an array), and returns a number. Andlength_isThe filter is used to determine whether the length of a variable isequalsSome specified value is returned directlyTrueorFalseA boolean value that does not return specific length values. Therefore,lengthIt is usually used to get and display length, whilelength_isIt is specifically used for conditional judgment.length_isCan you judge the length of a number? For example, a number123has a length of3?length_isThe filter is used to determine the number of elements in a string, array (slice), or map. It does not convert the number itself to a string before determining the length. For example,{{ 123|length_is:3 }}The result isFalse。If you really need to judge the "digit" length of a number, you can first convert the number to a string throughstringformatfilter, and then uselength_isfilter to judge, for example{{ number|stringformat:"%v"|length_is:3 }}.How can I determine if the length is "greater than
length_isFilter can only determine if it is "equal" to the specified length. If you need to perform "greater thanlengthThe filter gets the actual length of the variable and then combines{% if %}comparison operators in the label (such as>/</!=) to make judgments. For example, to determine if the list length is greater than 5:{% if articles|length > 5 %}.