How to handle the requirement that the `pagination` tag does not display pagination navigation when the `archiveList` query result is empty?

Calendar 👁️ 83

As an experienced website operations expert, I am happy to delve deeply into the exquisite aspects of AnQiCMS (AnQiCMS) in terms of content display, especially how to finely control the display of pagination navigation when the list content is empty.This not only concerns the beauty of the page, but also directly affects the user experience and the professionalism of the website.


WhenarchiveListHow to elegantly hide pagination navigation when the query results are empty? AnQiCMS template technique analysis

In a content management system, the list page is the core entry point for users to browse information. Anqi CMS, with its high efficiency and flexibility, provides us witharchiveListSuch a powerful tag, used to easily obtain and display various types of document content. At the same time,paginationThe tag is responsible for providing convenient pagination navigation for these lists.However, a common operational scenario is: when the document content under a certain category, search results, or filtering condition is empty, it is redundant and reduces the user experience to still display a lonely pagination navigation (such as "Page 1 / Total 1") on the page.

Today, let's delve into how to巧妙ly handle in AnQiCMS.archiveListWhen the query result is empty, letpaginationthe label also disappears.

Understand the template mechanism and core tags of AnQiCMS

AnQiCMS's template system draws on the syntax of the Django template engine, which is renowned for its simplicity and power. In the development of content list pages, we mainly use the following key tags:

  • archiveList: This is the core of the content list, used to query and display various documents such as articles, products, and other documents. When ittypethe parameter to"page"it will also prepare the data required for pagination at the same time.
  • pagination: This is a label specifically used for rendering pagination navigation. It will receivearchiveListGenerated pagination data and convert it into user-friendly links such as 'Previous page', 'Next page', 'Page number', etc.
  • forLoop withemptyblock:forLoop used for traversalarchiveListThe returned document collection. Its unique{% empty %}A block is a tool for handling empty list situations whenforthe loop has no data to iterate over,emptythe content inside the block will be rendered.
  • iflogical judgment:ifTags provide conditional judgment capabilities, allowing us to decide whether to execute a specific code block based on certain conditions.

Core pain point: Why should pagination be hidden?

Imagine a user searches and lands on a results page, only to find no matching content.At this time, it is reasonable for a line sayingBut if there is a pagination bar below with text saying 'Home Previous Page 1 Next Page End Page', it seems very inconsistent and redundant.This not only occupies valuable page space, but may also confuse users, even making them think that the website design is not rigorous.archiveListThe query result is empty, and it is synchronized to hide.paginationIt is an important aspect for improving user experience and optimizing page visuals.

Solution one: skillfully usefor emptyStructural processing of content is missing

AnQiCMS'forLoop tags provide a very convenientemptyblock. WhenarchiveListWhen no content meets the conditions,{% empty %}the code block will start, providing us with an opportunity to elegantly display a 'no content' prompt.

Firstly, we usually write it like thisarchiveListandforLoop to display content:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
        <div class="archive-item">
            <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
            <p>{{ item.Description }}</p>
            <span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
        </div>
    {% empty %}
        <div class="no-content-message">
            <p>抱歉,未找到任何相关内容。</p>
            <p>您可以尝试修改搜索词或浏览其他分类。</p>
        </div>
    {% endfor %}
    {# 默认情况下,pagination标签会紧随其后 #}
    <div class="pagination-container">
        {% pagination pages with show="5" %}
            {# 分页导航的代码,例如: #}
            <a href="{{ pages.FirstPage.Link }}">首页</a>
            {% if pages.PrevPage %}<a href="{{ pages.PrevPage.Link }}">上一页</a>{% endif %}
            {% for page in pages.Pages %}<a class="{% if page.IsCurrent %}active{% endif %}" href="{{ page.Link }}">{{ page.Name }}</a>{% endfor %}
            {% if pages.NextPage %}<a href="{{ pages.NextPage.Link }}">下一页</a>{% endif %}
            <a href="{{ pages.LastPage.Link }}">末页</a>
        {% endpagination %}
    </div>
{% endarchiveList %}

In the above example, ifarchivesis empty, the page will display 'Sorry, no related content was found.' However,paginationThe label will still try to render. To solve this problem, we need to control it more accuratelypaginationat the time of display.

Solution two: precise controlpaginationat the time of display.

WhenarchiveListStart withtype="page"When the pattern runs, it not only providesarchivesa list, but also generates a namedpagesobject that contains all information related to pagination, such as total number of entries(",TotalItems) and total number of pages(}TotalPages) of the current page(CurrentPage) and so on.

Even ifarchiveListThe query result is empty,pagesThe object will still exist, but itsTotalItemsusually will be 0,TotalPagesIt will be 1 (indicating that there is only one page and it is empty). Therefore, we can renderpaginationAdd a conditional judgment before the tag, only when the total number of pages is greater than 1, should the pagination navigation be displayed.

This is the recommended and more perfect approach:

”`twig {# Use archiveList to get document and pagination data #} {% archiveList archives with type=“page” limit=“10” %}

{# 先处理文档列表的显示 #}
{% for item in archives %}
    <div class="archive-item">
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <p>{{ item.Description }}</p>
        <span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
    </div>
{% empty %}
    <div class="no-content-message">
        <p>抱歉,未找到任何相关内容。</p>
        <p>您可以尝试修改搜索词或浏览其他分类。</p>
    </div>
{% endfor %}

{# 接着,在分页导航渲染前,进行条件判断 #}

Related articles

Will the AnQiCMS pagination tag generate a URL that automatically includes all the filter parameters on the current page (such as `q`)?

AnQiCMS (AnQiCMS) is an enterprise-level content management system that deeply understands the core needs of content operation and SEO, and has fully considered user experience and the optimization of search engine optimization from the beginning of its design.About whether the URL generated by the AnQiCMS pagination tag automatically includes all the filter parameters on the current page (such as `q`)?This question has an affirmative answer, and this is exactly where AnQiCMS demonstrates its intelligence and convenience in detail.

2025-11-07

What do `pages.PrevPage` and `pages.NextPage` return when there are no previous or next pages, and how can template errors be avoided?

As an experienced website operation expert, I have accumulated rich experience in the practice of AnQiCMS.Today, let's delve deeply into a common but crucial issue in template development: what `pages.PrevPage` and `pages.NextPage` return when there is no previous or next page, and how we can cleverly avoid the template errors caused by this, ensuring the stability of the website's frontend and the smoothness of the user experience.

2025-11-07

How to transform the traditional pagination navigation in AnQiCMS into 'Load More' or 'Infinite Scrolling' effect?

As an experienced website operation expert, I am well aware that user experience has become one of the key factors for the success of a website in today's rapidly changing internet environment.The traditional pagination navigation, although clear in function, often causes unnecessary sense of disconnection and waiting time while users browse the content.The effect of 'Load More' or 'Infinite Scrolling' can greatly enhance the user's continuous reading experience, reduce the bounce rate, and increase user stickiness.

2025-11-07

Does AnQiCMS support custom pagination URL structure for its pagination tag? What is the specific usage of the `prefix` parameter?

AnQiCMS Pagination Tag and Custom URL Structure: Deep Analysis of `prefix` Parameter As an experienced website operations expert, I know how important it is to have a clear, SEO compliant, and user-friendly URL structure for a website.How to flexibly control pagination links, especially in content management systems, is a common concern for operators.AnQiCMS (AnQiCMS) took this into consideration from the very beginning, providing website administrators with a great degree of freedom through its powerful template engine and pseudo-static features. Today

2025-11-07

Does AnQiCMS support configuring different pagination styles for different content models (such as articles, products)?

As an experienced website operations expert, I deeply understand the importance of the flexibility of the Content Management System (CMS) for efficient operations.AnQiCMS (AnQiCMS) truly provides great convenience in content management with its excellent customization capabilities.Today, let's delve deeply into a common and crucial issue in operation: 'Does the AnQiCMS pagination tag support configuring different pagination styles for different content models (such as articles, products)?

2025-11-07

How to dynamically generate clickable middle page number links by traversing the `pages.Pages` array?

As an experienced website operations expert, I fully understand the importance of an efficient and user-friendly content management system for the development of a company.AnQiCMS (AnQiCMS) leverages its high-performance architecture in Go language and flexible template mechanism to provide strong support for content operations.When building a content-rich website, good pagination navigation not only improves user experience but is also a key aspect of search engine optimization.

2025-11-07

What is the default number of page numbers displayed by the AnQiCMS pagination tag? Where can I modify this default value?

As an experienced website operation expert, I know that how to flexibly and efficiently control the display method of content in a content management system is directly related to user experience and the operation effect of the website.AnQiCMS (AnQiCMS) is a modern content management system developed based on the Go language, which provides us with great flexibility with its powerful template tag system.Today, let's delve deeply into the default page number display quantity and modification strategy of the pagination tag in Anqi CMS.

2025-11-07

How can I make the AnQiCMS pagination tags display more friendly on mobile devices while designing a responsive website?

Certainly, as an experienced website operation expert, I am happy to delve into how AnQiCMS can optimize pagination tags in responsive design to provide a more friendly mobile experience. --- ## AnQiCMS Responsive Pagination Optimization: Make Mobile Browsing Smoother In today's mobile internet age, the mobile experience of a website is crucial.

2025-11-07