When developing website templates with AnQiCMS, you often encounter situations where you need to display list data, such as article lists, product lists, or image galleries. When we use template tags (for examplearchiveListorcategoryList)Get 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 page. How toforHow to elegantly judge if a list is empty in a loop and display a prompt of 'No content'? AnQiCMS template engine provides a very concise and efficient solution.
AnQiCMS template inforLoop withemptytags
AnQiCMS template engine syntax is similar to Django or Blade, and it supportsforLoop structure, also built-in.emptyLabel, used specifically for handling the scenario where the list is empty. This means that you do not need to write extra.ifConditional judgments, you can directly use inforLoop internal handling of empty list display logic, making the code clearer and more readable.
Its basic structure is as follows:
{% for item in yourList %}
{# 这里是列表不为空时,遍历每个 item 的内容 #}
<div>{{ item.Title }}</div>
{% empty %}
{# 当 yourList 为空时,这里的内容会被显示 #}
<p>抱歉,暂无内容可供展示。</p>
{% endfor %}
In this code,yourListRepresents the data list you get through AnQiCMS template tags, such as through{% archiveList archives with type="list" limit="10" %}obtainedarchivesvariables. WhenyourListthere is data, the program will enterfora loop, processing one by oneitemand display the corresponding content. OnceyourListis an empty array or empty setforthe content inside the loop will be skipped, and then the logic inside the{% empty %}tags will be executed, displaying a prompt indicating "No content available".
Detailed Explanation and Application Scenarios
Thisfor...empty...endforThe structure design greatly simplifies template code. It tightly combines the display logic for both 'content' and 'no content' cases within a single structure, avoiding first usingifDetermine the length of the list and then write separatelyforRedundant steps for loop and empty content prompt.
For example, in a block displaying the latest articles, you may want to display the text 'The latest articles are being prepared, please wait' when there are no articles. UseemptyTags, 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,latestArticlesisarchiveListTags to get the list of articles. 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 type of prompt is informative. It not only keeps the code tidy but also makes the logic clear and easier to maintain.
Similarly, if you need to display all 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>
In this way, no matterarchiveListThe page renders correctly whether the returned data is empty or not, thus enhancing the user experience.
Summary
In the AnQiCMS template engine,for...empty...endforThe structure 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 scenarios where data is empty, displaying friendly prompt information to visitors.Master this skill, it can help you build high-quality AnQiCMS website templates more efficiently.
Common Questions (FAQ)
Q:
emptyCan the content within the tag be accessed?forIn the loopitemWhat is the variable?Answer: No.{% empty %}The content within the tag is only executedforwhen the list in the loop is empty, at this timeitemVariable is not defined, so it cannot be accessed inemptyblock.itemYou should place general prompt information inemptyblock instead of depending on the specific data in the list.Question: Besides
emptyLabel, are there other methods to determine if a list is empty?Answer: Of course. You can also useiflabel combined withlengtha filter to determine. For example:{% if yourList|length > 0 %} {% for item in yourList %} {# 列表内容 #} {% endfor %} {% else %} <p>暂无内容。</p> {% endif %}But usually,
for...empty...endforThe structure is more recommended as it is more concise and unifies the logic of 'having data' and 'no data'.Q:
forWhat are some other commonly used features of loops?Answer:forBesides loopsemptyTags outside, it also supports some modifiers and built-in variables that can help you control the loop behavior more flexibly:reversedOn:{% for item in yourList reversed %}can be used to traverse the list in reverse order.sortedOn:{% for item in yourList sorted %}Used in, it can sort the list by default.forloopvariable: It can access a variable named inside the loop.forloopa special variable, which provides some useful information such asforloop.Counter(Current loop count, starting from 1),forloop.Revcounter(Remaining loop count, counting down from the total number of items in the list) etc., which can be used to add different styles or logic to list items.