In AnQiCMS template, how to judge if the list is empty and display a prompt of 'No content'?

Calendar 👁️ 70

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)

  1. 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.

  2. 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.

  3. 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.)

Related articles

In AnQiCMS template, how to decide whether to display the default image or placeholder based on the existence of a variable?

In the template development of AnQiCMS, we often encounter situations where the content may not contain images.For example, a news article may not have an illustration, or a certain product in a product list may not have uploaded the main image temporarily.In this case, if the template directly calls the image address, it may display a broken image icon or leave an abrupt blank, which undoubtedly affects the overall aesthetics and user experience of the website.To solve this problem, AnQiCMS provides flexible template tags and filters, allowing us to determine whether the image variable exists

2025-11-09

How to elegantly handle null or undefined variables returned by the database in AnQiCMS templates to avoid page errors?

During the development of website templates, we often encounter a headache-inducing problem: when the data obtained from the database is empty (null) or a variable is not defined under certain circumstances, the template rendering will cause an error, resulting in the page not displaying normally and significantly reducing the user experience.AnQiCMS (AnQiCMS) is based on its powerful Go language backend and flexible Django-style template engine, providing us with various elegant solutions for handling such situations, allowing the template to run smoothly even when the data is incomplete.### One, make good use of conditional judgments: `{% if

2025-11-09

What is the difference between the `default_if_none` filter and the `default` filter in the AnQiCMS template, and when should the former be preferred?

In web template development, handling the case where variables may not exist or are empty is a common task.The AnQiCMS template system provides a variety of filters to help us elegantly handle such issues, with `default` and `default_if_none` being two commonly used tools.They both provide a default value when the variable is 'no value', but there is a subtle but important difference in the definition of 'no value'.Understanding these differences can help us control the display of template content more accurately.###

2025-11-09

How to set default display content for possibly empty variables in the `default` filter of AnQiCMS templates?

In Anqi CMS template design, we often encounter situations where variable values may be empty.For example, an article may not have a thumbnail set or a product field may be temporarily unfilled.If the template directly displays these empty values, ugly blank spaces will appear on the page, affecting user experience, and may even confuse visitors.To solve this problem, Anqi CMS provides a very practical filter mechanism, where the `default` filter is the key tool to ensure the continuity of content display.### `default` Filter

2025-11-09

How to judge if there is a thumbnail in the `archiveList` tag in AnQiCMS template through `if` to selectively display images?

In AnQiCMS template development, displaying list content is a common requirement, and how to elegantly handle the images in these lists, especially thumbnails, is directly related to the visual effects and user experience of the website.The `archiveList` tag is one of the core content call tags of AnQiCMS, which helps us flexibly obtain various document lists.However, in practice, we often encounter situations where certain documents have not set thumbnails, which may lead to broken images or layout confusion on the page. At this time

2025-11-09

How to display different content in the AnQiCMS template based on the value of the `item.Status` field (such as approved, in review)?

In website content operation, the review status of the content is a very important link.Whether it is a comment submitted by the user, a forum post, or an article or product information released by the website administrator, it often needs to be reviewed before it can be displayed to the public.AnQiCMS provides a flexible template mechanism that allows us to easily display different content on the front-end page based on the review status of the content (such as "approved" or "in review"), thereby providing users with clearer and more accurate feedback.AnQiCMS template syntax is similar to the Django template engine

2025-11-09

In AnQiCMS template, how to judge and display the 'In stock' or 'Out of stock' status based on the product inventory (`Stock`) quantity?

It is crucial to clearly communicate the inventory status of products to users in website operations, especially for sites involving product display.This not only optimizes the user experience, reduces invalid consultations, but also effectively guides the user's purchase decision.AnQiCMS as a flexible and efficient content management system, implements the display of "in stock" or "out of stock" status according to the product inventory quantity in the template, which is very intuitive. AnQiCMS template system adopts a syntax similar to Django, which allows us to control the display of page content through concise tags and variables.When processing product information

2025-11-09

In AnQiCMS template, how to judge whether a string can be successfully converted to a numeric type and perform conditional processing?

In AnQi CMS template creation, we often encounter situations where we need to process strings entered by users or retrieved from the database.One common requirement is to determine whether a string can be successfully converted to a numeric type and to perform different conditional processing based on the result.This is crucial for data display, calculation, and even simple form validation.The AnQi CMS template engine (based on Go language's Pongo2) provides a rich set of filters (filters) and logical tags, allowing us to flexibly meet this requirement. Below

2025-11-09