In AnQiCMS template, how to judge if the list is empty and display a prompt of 'No content'?
When using AnQiCMS (AnQiCMS) for website template development, it is often necessary to display list data, such as article lists, product lists, or image galleries. When we use template tags (such asarchiveListorcategoryList) Retrieve data and useforWhen iterating through a list, if the list is empty, it is usually necessary to provide the user with a friendly prompt instead of displaying a blank space. How toforIn a loop, how can you elegantly judge whether a list is empty and display a prompt saying "No content"? AnQiCMS template engine provides a very concise and efficient solution.
In AnQiCMS template.forLoop withemptyTag
AnQiCMS's template engine syntax is similar to Django or Blade, it supportsforloop structures, and also includes aemptyThe tag is used specifically to handle the scenario where a list is empty. This means you do not need to write additionalifcondition checks, you can directly useforLoop internal handling of empty list display logic, making the code clearer and more readable.
The basic structure is as follows:
{% for item in yourList %}
{# 这里是列表不为空时,遍历每个 item 的内容 #}
<div>{{ item.Title }}</div>
{% empty %}
{# 当 yourList 为空时,这里的内容会被显示 #}
<p>抱歉,暂无内容可供展示。</p>
{% endfor %}
In this code block,yourListRepresents the data list you get through AnQiCMS template tags, for example, through{% archiveList archives with type="list" limit="10" %}ObtainedarchivesThe variable. WhenyourListWhen there is data in it, the program will enterforLoop body, process each one by oneitemand display the corresponding content. OnceyourListis an empty array or an empty set,forthe content inside the loop will be skipped, and instead{% empty %}the logic inside the tag will be executed, displaying the prompt “No content”.
Detailed Analysis and Application Scenarios
Thisfor...empty...endforThe structure design greatly simplifies the template code. It tightly combines the display logic for both 'content' and 'no content' cases in a single structure, avoiding the need to use firstifCheck the length of the list and write separatelyforRedundant steps for looping and empty content hints.
For example, in a block that displays the latest articles, you may want to display the text 'The latest articles are being prepared, please wait' when there are no articles. UseemptyLabel, you can implement it like this:
<div class="latest-articles">
<h3>最新文章</h3>
<ul>
{% archiveList latestArticles with type="list" order="id desc" limit="5" %}
{% for article in latestArticles %}
<li>
<a href="{{ article.Link }}">{{ article.Title }}</a>
<span>{{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
</li>
{% empty %}
<li>
<p>最新文章正在整理中,敬请期待!</p>
</li>
{% endfor %}
{% endarchiveList %}
</ul>
</div>
In this example,latestArticlesIt is througharchiveListThe list of articles obtained by the label. If there are no articles that meet the conditions in the database,latestArticlesThe variable will be empty, and the template will automatically display{% empty %}This is a prompt information. This way not only the code is neat, but the logic is also clear, and it is also more convenient to maintain.
Similarly, if you need to display all the documents under a category on a category page, you can also take advantage of this feature:
<div class="category-documents">
<h2>{% categoryDetail with name="Title" %}下的文档</h2>
<ul>
{% archiveList categoryDocuments with type="page" categoryId=category.Id limit="10" %}
{% for doc in categoryDocuments %}
<li>
<a href="{{ doc.Link }}">{{ doc.Title }}</a>
</li>
{% empty %}
<li>
<p>当前分类下暂无文档。</p>
</li>
{% endfor %}
{% endarchiveList %}
</div>
</div>
Through this method, regardless ofarchiveListThe page renders correctly whether the label returns data or not, thus enhancing the user experience.
Summary
In the AnQiCMS template engine,for...empty...endforStructure is a very practical feature when dealing with list data.It not only makes your template code more concise and easy to read, but also can flexibly handle the scenario of empty data, showing friendly prompt information to visitors.Mastering this skill can help you build high-quality AnQiCMS website templates more efficiently.
Frequently Asked Questions (FAQ)
Question:
emptyCan the content inside the tag be accessedforin the loopitemVariable?Answer: No.{% empty %}The content inside the tag is onlyforExecuted when the loop list is empty, at this timeitemThe variable is not defined, so it cannot beemptyAccess within blockitemVariable. You should beemptyPlace general prompt information within the block, rather than relying on specific data from the list.Question: Besides,
emptyTags, are there other ways to determine if the list is empty?Of course, there is. You can also useifLabel collaborationlengtha filter to judge. For example:{% if yourList|length > 0 %} {% for item in yourList %} {# 列表内容 #} {% endfor %} {% else %} <p>暂无内容。</p> {% endif %}But usually,
for...empty...endforThe structure is more recommended because it is more concise and unifies the logic of 'has data' and 'no data' processing.Question:
forWhat are some other common features of loops?Answer:forLoops, besidesemptyOutside the label, some modifiers and built-in variables are supported, which can help you control the loop behavior more flexibly:reversed: At{% for item in yourList reversed %}Used in, you can traverse the list in reverse order.sorted: At{% for item in yourList sorted %}Used, can be sorted by default in the list.forloopVariable: Can access a special variable named inside the loopforloopwhich provides some useful information such asforloop.Counter(Current loop count, starting from 1),forloop.Revcounter(The remaining number of iterations in the current loop, counting down from the total number of items in the list, etc., can be used to add different styles or logic to list items.)