How to display the default prompt information when the article list obtained through `archiveList` is empty?

Calendar 👁️ 54

When using Anqi CMS to build a website, we often need to display lists of articles, products, or other content. These lists are usually displayed througharchiveListThis template tag is used to dynamically retrieve and render.However, if a list is empty due to various reasons (such as a lack of content under a category, or empty search results, etc.), the user experience on the page will be greatly reduced if it is just empty.archiveListHow should we display a friendly default prompt when the article list is empty?

The AnQi CMS built-in template engine (based on Django template syntax) provides a very concise and powerful mechanism to handle this situation, that isforin the loop{% empty %}Block. This feature allows us to easily define the content displayed when the list has no data, thus optimizing the user experience.

archiveListLabel overview

First, let's briefly review.archiveListLabel. It is a very flexible label used to query and display a list of articles from a database. By setting different parameters, such asmoduleId(Model ID),categoryId(Category ID),limit(show quantity) as welltype(List type, such as pagination or regular list), you can accurately control which articles to retrieve.For example, you can get the latest articles under a certain category, or a list of articles with specific recommended attributes.

An elegant solution:{% empty %}block

WhenarchiveListAfter the tag gets the data, we usually use{% for item in archives %}such a structure to iterate through and display the detailed information of each article. And{% empty %}It is a blockforThe partner of the loop, it will beforLoop through the list (for examplearchiveListTagging for obtainingarchivesVariable) is executed and displayed when there is no data. In other words, ifarchivesthe variable is empty, then{% for %}and{% endfor %}The content between (i.e., the normal article list) will not be rendered, instead, it will be replaced with{% empty %}and{% endfor %}The content between.

This makes the logic of handling empty lists very intuitive and simple, without the need for additional conditional judgments, and the code is also more concise.

How to implement: code example

Assuming you are displaying an article list in a template, you can use it like thisarchiveListand{% empty %}To ensure that a prompt is displayed even when there are no articles

{# 假设这里是某个文章列表的模板区域 #}
<div class="article-list-container">
    {% archiveList articles with type="page" limit="10" %} {# 获取最新的10篇文章,如果当前在分类页或搜索页,则自动根据上下文获取 #}
        {% for item in articles %}
            {# 如果列表有内容,这里会循环显示每篇文章的详情 #}
            <div class="article-item">
                <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
                <p>{{item.Description}}</p>
                <span>发布时间:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
                <span>浏览量:{{item.Views}}</span>
            </div>
        {% empty %}
            {# 如果列表为空,这里的内容就会被显示 #}
            <div class="empty-list-message">
                <p>抱歉,当前列表暂时没有找到任何文章。</p>
                <p>您可以尝试调整筛选条件,或访问<a href="/">网站首页</a>获取更多内容。</p>
            </div>
        {% endfor %}
    {% endarchiveList %}
</div>

In this code block:

  • {% archiveList articles with type="page" limit="10" %}This is the tag to get the list of articles, we will assign the obtained article data toarticlesVariable.
  • {% for item in articles %}The standard loop structure, used to iteratearticlesEach article in it.
  • Within the loop: When there are articles, it will render the title, description, publish time, and view count of each article.
  • {% empty %}: This is the key! IfarticlesThe list is empty, thenforThe content inside the loop will be skipped and executed directly{% empty %}.
  • {% endfor %}and{% endarchiveList %}: meansforloop andarchiveListEnd of tag.

Custom prompt content

{% empty %}The strength of the block lies in the fact that you can place any HTML content in it, not just simple text. This means you can:

  1. Display friendlier text promptsFor example, 'Alas, I couldn't find the content you were looking for!' or 'There are no articles in this category, please look forward to them!'
  2. Add guiding operationsFor example, provide a search box to suggest other keywords to the user; or display some popular articles and recommended content to guide the user to continue browsing.
  3. 美化提示样式Add CSS styles to the prompt information to keep it consistent with the overall design style of the website, and even add small icons or animations to make the prompt more vivid and interesting.

For example, you can customize the prompt like this:

            <div class="empty-list-message">
                <p><i class="icon-info"></i> 当前没有找到相关内容。</p>
                <p>您可以试试:</p>
                <form action="/search" method="get">
                    <input type="text" name="q" placeholder="输入关键词搜索...">
                    <button type="submit">搜索</button>
                </form>
                <p><a href="/category/hot-articles" class="btn btn-primary">查看热门文章</a></p>
            </div>

Application scenarios and versatility

This{% empty %}The use of the block is not limited toarchiveListTags, other tags used for list display in AnQi CMS, such astagList(Document tag list),commentList(Comment list),categoryList(Category list) etc., also support similar functions,{% for ... empty ... endfor %}Syntax structure. This means you can apply this concise and efficient solution in any scenario where you need to loop through data but are concerned about it being empty.

By simply inarchiveListlabel'sforLoop added{% empty %}A block, and we can easily deal with the case where the article list is empty, ensuring that the website can always provide users with a friendly and complete page content, thereby significantly improving the user experience.

Frequently Asked Questions (FAQ)

  1. Question:{% empty %}Does the block affect the page performance?Answer: No.{% empty %}The block is a logical judgment of the template engine, only when it corresponds to theforWhen the list being traversed is indeed empty, its content will be rendered to the page. This will not add any additional load to the page, but can handle the case of no data more efficiently.

  2. Ask: Can I display{% empty %}other recommended content or nest otherarchiveListTag?Answer: Absolutely.{% empty %}The block can contain any valid HTML or other template tags. You can nest anotherarchiveListLabel to display hot articles, the latest released content, or insert a search form to guide users to explore more information.Please note that when nesting, make sure the logic is clear, avoid infinite loops, or unnecessary complexity.

  3. Ask: If my template does not have{% empty %}block, whenarchiveListwhat will happen if it returns empty data?Answer: If there is no{% empty %}block andarchiveListThe data returned is empty, then{% for %}Any content inside the loop will not be rendered.The corresponding area of the page will display as blank or missing content.{% empty %}How to handle an empty list situation.

Related articles

How to implement reverse or custom sorting output of AnQiCMS's `for` loop?

The order of website content listings is crucial for user experience and information delivery.Whether it is news updates, product displays, or article archives, flexibly controlling the output order of lists is a common demand in website operation.AnQiCMS provides a powerful and easy-to-use template engine, allowing us to easily control the arrangement of lists, including reverse order and sorting by specific rules.### AnQiCMS Template Engine Basics The AnQiCMS template system adopts the syntax style of the Django template engine, using concise tags and variable declarations

2025-11-08

How to get the current loop item index or remaining item count in an article?

When using AnQi CMS for website content display, we often need to finely control each item in the list.This may include adding numbers to articles, highlighting the first or last item in a list, or applying different styles based on the position of the item in the list.The AnQi CMS template system uses a syntax similar to the Django template engine, where the key to handling list loops is the `for` loop tag.

2025-11-08

How to loop through and output the article list in AnQiCMS template?

AnQiCMS provides a powerful and flexible template system for website content management, among which, the loop traversal and output of the article list is an indispensable core function for building a dynamic website.Whether you need to display the latest articles, category lists, or implement advanced filtering and pagination, AnQiCMS template tags can help you complete it efficiently.

2025-11-08

How to customize the display layout of the article detail page, including images, descriptions, and custom parameters

In AnQi CMS, the display layout of the article detail page has extremely high flexibility, whether it is to show the core content of the article, beautiful pictures, or customized business parameters, the system provides an intuitive and powerful way to help you meet your personalized needs.This is due to the template engine of AnQiCMS based on Django-like syntax, which separates content from design, allowing even users without a strong programming background to adjust layouts through simple tags.### Understanding the Composition of Article Detail Page In AnQiCMS, articles (or products

2025-11-08

How to alternate different CSS classes or styles while looping through list items?

In web design, to enhance visual aesthetics and user experience, we often encounter situations where we need to alternate different CSS classes or styles for list items (such as article lists, product lists, navigation menus, etc.).For example, make the background color of odd and even rows different, or apply a unique layout style every few items.AnQiCMS (AnQiCMS) leverages its flexible template engine to provide a variety of simple and efficient methods to meet this requirement.

2025-11-08

How to safely output content containing HTML tags in AnQiCMS templates (e.g., article content)?

Guide to safely output HTML content in AnQiCMS templates When building a website with AnQiCMS, we often need to display content with rich formatting, such as article content, product descriptions, category details, or single-page content.This content is usually entered through the backend rich text editor, which will naturally contain HTML tags such as `<p>`, `<strong>`, `<em>`, `<a>`, and so on.How to correctly and safely output this content with HTML tags in the front-end template is a very important issue.If handled improperly

2025-11-08

How to use the `include` tag to reuse header, footer, and other common code segments in AnQiCMS templates?

Building website templates in AnQiCMS, efficient code reuse is the key to improving development speed and maintenance efficiency.Imagine, the header (Header) and footer (Footer) of a website appear almost on every page. If the same code is written on each page, it is not only time-consuming, but also, once a modification is needed, it has to be searched and updated on each page, which is undoubtedly a huge amount of work.

2025-11-08

How to pass specific variables or data when including a sub-template?

Advanced AnQiCMS Template: `include` Sub-template Data Passing Techniques and Practices In AnQiCMS template development, the `include` tag is undoubtedly a powerful tool for enhancing template reusability and modularization.It allows us to extract common code snippets (such as page headers, footers, sidebars, etc.) and then introduce them where needed, thus avoiding repetition and making the template structure clearer and maintenance more efficient.However, the sub-templates introduced often need to display different content, which raises a core question: how to introduce sub-templates at the time

2025-11-08