In Anqi CMS template development, we often need to decide the display content of the page based on the existence or absence of data, such as an article list. If the list is empty, we may need to display "No content" instead of a blank area. At this point,lengthThe filter becomes our powerful assistant, it helps us easily judge whether arrays, strings, and other data are empty.
Get to knowlengthFilter
lengthThe filter is a very practical feature provided by the Anqi CMS template engine, its main function is to return the length of a variable. This 'length' varies depending on the type of the variable:
- For strings:
lengthCalculates the actual number of characters in a string. Whether it is an English letter, number, or Chinese character,lengthit is counted as a character. - For an array or slice (Slice):
lengthReturns 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 your template contains a stringsomeText = "Hello AnQiCMS"then{{ someText|length }}will output13. If the string ischineseText = "你好安企CMS"then{{ chineseText|length }}it will also be output6. This 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 the basic usage of the filter, we can use it flexibly in the scenario of determining if data is empty.
1. Check if a string is empty
There are two very convenient ways to check if a string is empty:
Method one: uselengthFilter combinationifStatement.This 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 determine the stringIn AnQi CMS template engine, non-empty strings are considered 'true', and empty strings are considered 'false'. Therefore, you can directly use string variables 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 requires less code and is more readable.
2. Check 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 combinationifStatement.Similar to strings, obtain the number of elements in the array and determine if it is:0.
{% 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: useforrepeatedlyemptySyntaxThis is to determine if the list is empty when looping through the list datamost recommendedmethod. Whenforthe data set being looped over is empty,{% empty %}the content inside 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 logics of 'traversal' and 'no content prompt', making the code structure more compact and clear.
Method three: directly inifthe statement to determine the arrayAs with strings, in AnQi CMS templates, non-empty arrays or slices are considered "true", and empty arrays or slices are considered "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 if its length meets a specific requirement. 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.) isequalsa 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:0It can also achieve the effect of being empty, but it is more common to uselength == 0or directlyifvariable names.
Summary
BylengthThe filter and its related judgment methods allow Anqi CMS users to control the display logic of content more flexibly and accurately in templates.Whether it is simple string or array null judgment, or more complex length matching, these tools can help you build a website page with better user experience and more robust logic.Understanding and proficiently applying these filters is the key to improving the efficiency and quality of AnQi CMS template development