In the template development of AnQi CMS, we often need to decide the display content of the page based on whether the data exists or not, for example, a list of articles. If the list is empty, we may need to display 'No content available' instead of an empty area.lengthThe filter becomes our reliable assistant, which can help us easily determine if arrays, strings, and other data are empty.
KnowlengthFilter
lengthThe filter is a very practical feature provided by the Anqi CMS template engine, which mainly serves to return the length of a variable. This 'length' varies depending on the type of the variable:
- For the string:
lengthit calculates the actual number of characters in the string. Whether it is English letters, numbers, or Chinese characters,lengthit will count them as one character. - For an array or slice:
lengthIt will return the total number of elements contained in the array or slice. - For a key-value pair (Map):
lengthIt will return the number of key-value pairs.
Its basic usage syntax is very intuitive:
{{ 变量 | length }}
For example, if you have a string in your templatesomeText = "Hello AnQiCMS"so that{{ someText|length }}it will output:13. If the string ischineseText = "你好安企CMS"so that{{ chineseText|length }}it will also output:6This reflects its friendly support for multilingual characters. Similarly, if there is an arraymyList = ["item1", "item2"],{{ myList|length }}it will return2.
How to determine if an array or string is empty
MasteredlengthAfter learning the basic usage of the filter, we can then flexibly apply it to the scenario of determining whether data is empty.
1. Determine if a string is empty
There are usually two very convenient ways to judge if a string is empty:
Method one: uselengthfilter combined withifstatementThis method directly gets the length of the string and compares it with0Compare. When the length is0it means the string is empty.
{% set articleTitle = "安企CMS模板开发指南" %}
{# 或者 {% set articleTitle = "" %} #}
{% if articleTitle|length == 0 %}
<p>抱歉,文章标题是空的。</p>
{% else %}
<h1>{{ articleTitle }}</h1>
{% endif %}
Method two: directly inifthe statement to judge the stringIn the template engine of AnQi CMS, non-empty strings are considered as 'true', while empty strings are considered as 'false'. Therefore, you can use string variables directly asifThe condition of the statement.
{% set authorName = "安企小助手" %}
{# 或者 {% set authorName = "" %} #}
{% if authorName %}
<p>作者:{{ authorName }}</p>
{% else %}
<p>作者未知。</p>
{% endif %}
This method is usually recommended because it has less code and is more readable.
2. Determine if the array (or slice) is empty
For arrays or slices, there are many diverse and practical methods to determine if they are empty:
Method one: uselengthfilter combined withifstatementSimilar to strings, to get the number of elements in an array and determine if it is0.
{% set productList = ["产品A", "产品B"] %}
{# 或者 {% set productList = [] %} #}
{% if productList|length == 0 %}
<p>当前没有可用的产品。</p>
{% else %}
<ul>
{% for product in productList %}
<li>{{ product }}</li>
{% endfor %}
</ul>
{% endif %}
Method two: usingforLoopingemptySyntaxThis is to determine whether the list is empty when displaying list data in a loop.Most recommended.Method. When.forThe dataset being traversed in the loop is empty.{% empty %}The content within the block will be executed automatically.
{% set commentList = ["评论1", "评论2"] %}
{# 或者 {% set commentList = [] %} #}
{% for comment in commentList %}
<div class="comment-item">{{ comment }}</div>
{% empty %}
<p>目前还没有任何评论。</p>
{% endfor %}
This method perfectly integrates the two logic of 'traverse' and 'no content prompt', making the code structure more compact and clear.
Method three: directly inifthe statement to judge the arrayAs with strings, in AnQi CMS templates, non-empty arrays or slices are considered as "true", while empty arrays or slices are considered as "false". Therefore, you can also use them directly asifThe condition of the statement.
{% set relatedArticles = [] %}
{# 或者 {% set relatedArticles = ["相关文章1"] %} #}
{% if relatedArticles %}
<h3>相关推荐</h3>
<ul>
{% for article in relatedArticles %}
<li>{{ article }}</li>
{% endfor %}
</ul>
{% else %}
<p>暂无相关推荐。</p>
{% endif %}
3. Advanced Usage:length_isFilter
Sometimes, we need to not only check if the data is empty, but also determine if its length meets specific requirements. At this point,length_isThe filter is very useful. It is used to check if the length of a value (which can be a string, array, etc.)equalsa specified number, and returns a boolean valueTrueorFalse.
The syntax is:
{{ 变量 | length_is: 目标长度 }}
For example, if you need to ensure that a navigation menu exactly has 5 items, you can use it like this:
{% set navItems = ["首页", "产品", "服务", "关于我们", "联系我们"] %}
{# 或者 {% set navItems = ["首页", "产品"] %} #}
{% if navItems|length_is:5 %}
<p>导航菜单刚好有5个项目,布局完美!</p>
{% else %}
<p>导航菜单项目数量不符合预期。</p>
{% endif %}
Combinelength_is:0You can also achieve the effect of being empty, but it is usually usedlength == 0or directlyifVariable names are more common.
Summary
PasslengthFilter and its related judgment methods, users of Anqi CMS can control the display logic of content more flexibly and accurately in the template.Whether it is simple string or array null judgment, or more complex length matching, these tools can help you build websites with better user experience and more robust logic.