How to conditionally display the 'Previous' and 'Next' buttons in the `pagination`?

Calendar 👁️ 67

As an experienced website operation expert, I am willing to delve into the conditional display techniques of the pagination navigation buttons in Anqi CMS.AnQi CMS is known for its efficiency and flexibility, its template engine provides powerful features, allowing us to easily realize both beautiful and practical page interactions.In website content management, pagination is an indispensable element, and how to intelligently display the "Previous Page" and "Next Page" buttons is directly related to the smoothness of the user's browsing experience.

AnQi CMS: Creating an Intelligent Pagination Navigation - How to Conditionally Display 'Previous Page' and 'Next Page' Buttons

On a content-rich website, pagination is the core mechanism for organizing and presenting a large amount of information.A well-designed, intuitive pagination system that can significantly enhance the user's browsing experience.In Anqi CMS, we take advantage of its powerful template tags to flexibly control the display logic of pagination navigation, especially the conditional display of the key buttons such as 'Previous Page' and 'Next Page' to avoid invalid links when not needed, maintaining the interface's cleanliness and professionalism.

Why is it Necessary to Conditionally Display 'Previous Page' and 'Next Page' Buttons?

Imagine, when the user is browsing the first page of the list, if the 'Previous Page' button is still visible and clickable, the user may encounter a blank page or an error page after clicking, which undoubtedly brings a bad experience.Similarly, displaying the "Next Page" button on the last page will also cause similar problems.By conditionally displaying, we ensure that these navigation elements only appear when logically feasible, thus:

  1. Improve user experience:Avoid users from clicking on invalid links, reducing confusion and frustration.
  2. Keep the interface tidy:Only display elements that are useful in the current state, making the page layout clearer.
  3. Optimize front-end performance:Reduce unnecessary rendering of DOM elements, although this is usually a negligible optimization.

Anqi CMS pagination tag overview

In AnQi CMS, the pagination feature usually works with content list tags (such asarchiveListcooperatetype="page")协同工作。archiveListThe tag will return a set of pagination information when it retrieves pagination content.pagesObject. And today's core is to make use of thispagesDetermine whether the "Previous Page" and "Next Page" buttons should be displayed by using specific fields in the object.

Pagination LabelpaginationBasic usage is{% pagination pages with show="5" %}...{% endpagination %}. This tag will encapsulate all data related to pagination, including the total number of items, total number of pages, current page number, first page, last page, previous page, next page, and the list of middle page numbers, all within a namedpagesthe variable is used for template.

core variables and judgment logic

Inpagesin the object, there are two fields that are crucial for our implementation of conditional display:pages.PrevPageandpages.NextPage.

  • pages.PrevPageThis is an object that contains the name (Name) and link (Link) of the previous page.
  • pages.NextPage: It is also an object that includes the name (Name) and link (Link) of the next page.

The AnQi CMS template engine (developed based on Go language, with syntax similar to Django templates) is very intelligent in handling conditional judgments. When there is no previous page,pages.PrevPageThis object will be empty (nil). Similarly, when there is no next page,pages.NextPageit will also be empty. The Go language template engine'sifjudgment will automatically treat these empty (nil) objects asfalseThis means that we can simply use{% if pages.PrevPage %}and{% if pages.NextPage %}such a structure to achieve conditional display of buttons.

Practice operation: Conditionally display 'Previous page' and 'Next page'

Let us demonstrate how to implement this function through a specific template code snippet. This is usually completed in yourarchiveListpagination area after the tag.

First, you need to go through the template in order toarchiveListLabel to get pagination data:

{% archiveList archives with type="page" limit="10" %}
    {# 循环显示文章列表内容 #}
    {% for item in archives %}
        <li>
            <a href="{{item.Link}}">
                <h5>{{item.Title}}</h5>
                <div>{{item.Description}}</div>
                <div>
                    <span>{% categoryDetail with name="Title" id=item.CategoryId %}</span>
                    <span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
                    <span>{{item.Views}} 阅读</span>
                </div>
            </a>
            {% if item.Thumb %}
                <a href="{{item.Link}}">
                    <img alt="{{item.Title}}" src="{{item.Thumb}}">
                </a>
            {% endif %}
        </li>
    {% empty %}
        <li>
            该列表没有任何内容
        </li>
    {% endfor %}
{% endarchiveList %}

Then, followed by your content list, usepaginationLabel to build pagination navigation, and use it cleverly inifConditional judgment:

<div class="pagination">
    {% pagination pages with show="5" %}
        <ul>
            {# 首页按钮:通常总是显示,但可以根据pages.FirstPage.IsCurrent来添加active样式 #}
            <li class="page-item {% if pages.FirstPage.IsCurrent %}active{% endif %}">
                <a href="{{pages.FirstPage.Link}}">{{pages.FirstPage.Name}}</a>
            </li>

            {# 上一页按钮:仅当存在上一页时显示 #}
            {% if pages.PrevPage %}
                <li class="page-item">
                    <a href="{{pages.PrevPage.Link}}">{{pages.PrevPage.Name}}</a>
                </li>
            {% endif %}

            {# 中间页码列表:循环显示,并为当前页添加active样式 #}
            {% for item in pages.Pages %}
                <li class="page-item {% if item.IsCurrent %}active{% endif %}">
                    <a href="{{item.Link}}">{{item.Name}}</a>
                </li>
            {% endfor %}

            {# 下一页按钮:仅当存在下一页时显示 #}
            {% if pages.NextPage %}
                <li class="page-item">
                    <a href="{{pages.NextPage.Link}}">{{pages.NextPage.Name}}</a>
                </li>
            {% endif %}

            {# 尾页按钮:通常总是显示,但可以根据pages.LastPage.IsCurrent来添加active样式 #}
            <li class="page-item {% if pages.LastPage.IsCurrent %}active{% endif %}">
                <a href="{{pages.LastPage.Link}}">{{pages.LastPage.Name}}</a>
            </li>
        </ul>
        <p>总数:{{pages.TotalItems}}条,总共:{{pages.TotalPages}}页,当前第{{pages.CurrentPage}}页</p>
    {% endpagination %}
</div>

In this example,{% if pages.PrevPage %}and{% if pages.NextPage %}It is the key to conditional display. Whenpages.PrevPageorpages.NextPageThe object is not empty, corresponding to<li>elements and their internal<a>The link will be rendered on the page. This way, on the first page, you will not see the 'Previous page' button;On the last page, the 'Next Page' button does not appear, thus providing a more intelligent and user-friendly pagination navigation.

Summary and **practice**

Passing through the Aanqi CMS'spaginationThe tag with its built-in logical judgment ability allows us to easily implement the conditional display of "Previous Page" and "Next Page" buttons in pagination navigation.This method is not only concise and efficient in code but also greatly enhances the user experience and professionalism of the website interface.

When developing templates, it is always recommended to fully utilize the built-in features of the Anqi CMS template engine.They are often carefully designed to solve common display and logical problems in the most elegant way.When implementing pagination functionality, in addition to conditional display, you can also adjust according to your needsshowParameters to control the number of middle page numbers, and even through CSS to beautify the pagination style, making it perfectly blend with your website design style.


Frequently Asked Questions (FAQ)

  1. Why did I set the pagination label, but the 'Previous Page' and 'Next Page' buttons still did not appear?Please first check if you have set the content list label (such asarchiveList) whether it is set.type="page". Only whentypethe parameter to"page"then,archiveListIt will generate complete pagination information and pass it on topaginationtag. If set totype="list", thenarchiveListit will only return a specified number of list items without pagination information,pages.PrevPageandpages.NextPageit will naturally be empty.

  2. Ask: If I don't want to display the "First Page" and "Last Page" buttons, can I only show the "Previous Page", "Next Page" and the middle page numbers?Answer: Of course you can. You just need topaginationTag the code block, thenpages.FirstPageandpages.LastPagethe corresponding<li>Remove the element. The flexibility of the Anqi CMS template allows you to freely cut and combine these pagination elements according to your actual needs.

  3. How to modify the display text of the 'Previous' and 'Next' buttons, for example, changing them to 'Previous' and 'Next'?Answer: Inpages.PrevPageandpages.NextPageThe object itself contains,NameField, for example{{pages.PrevPage.Name}}Default display is 'Previous page',{{pages.NextPage.Name}}The default is "Next page". If you wish to customize this text, it usually needs to be modified in the Anqi CMS backend language package or related configuration, or directly in the template.{{pages.PrevPage.Name}}Replace with the hard-coded text (for examplePrevious). However, it is recommended to modify the backend language package management to support multi-language switching.

Related articles

In the `pagination` tag, how to determine if the current page is the first or last page to control the style?

As an experienced website operations expert, I know the importance of a powerful and flexible content management system for the success of a website.AnQiCMS leverages the efficiency of the Go language and its deep consideration for content operation, providing us with many conveniences.Today, let's delve into the wonderful use of the `pagination` tag in AnQiCMS, especially how to accurately judge whether the current page is the first page or the last page, thereby flexibly controlling the style to create a smoother and more intuitive navigation experience for users.

2025-11-06

How to conditionally output the standard link according to the `CanonicalUrl` tag in AnQiCMS templates?

Hello! As an experienced website operations expert, I fully understand that in daily work, we not only need to pay attention to the quality of the website content, but also to the impact of technical details on SEO.The canonical URL is one of the crucial details that can effectively solve the problem of duplicate website content, concentrate page authority, and thus improve search engine rankings.Today, let's delve deeply into how to intelligently and elegantly output standard links based on the existence or absence of the `CanonicalUrl` field within the `tdk` tag in AnQiCMS templates

2025-11-06

How to determine if the `linkList` friendship link tag list is empty to avoid displaying an empty tag?

As an experienced website operations expert, I have accumulated rich experience in content management and template optimization in AnQiCMS.I know well that a smooth, beautiful and quick-loading website cannot be separated from the meticulous refinement of every detail.Today, let's delve deeply into an issue that is often overlooked in template development but is crucial for user experience and page neatness: how to avoid displaying redundant empty tags when the `linkList` friendship link tag list is empty.Avoid displaying empty tags: AnQi CMS

2025-11-06

How to determine if the `navList` menu item has child navigation (`item.NavList`) and perform nested rendering?

## AnQi CMS navigation menu: skillfully use `navList` to implement multi-level nesting and child navigation judgment As an experienced website operations expert, I fully understand the importance of a clear and flexible navigation system for user experience and the overall architecture of the website.In AnQiCMS (AnQiCMS), the `navList` tag is the core tool we use to build a powerful navigation system.

2025-11-06

How to judge whether the comment (`Status`) in the AnQiCMS template has passed the review and be displayed differently?

As an experienced website operation expert, I know the importance of the comment system for website user interaction and content ecology.In such a powerful content management system as AnQiCMS, flexibly controlling the display of comments, especially the differentiated display based on the review status, is a key factor in improving user experience and maintaining the quality of website content.Today, let's delve into how to use the `commentList` tag in the AnQiCMS template to judge the `Status` field of comments and implement intelligent differentiated display.

2025-11-06

How to determine if a comment in `commentList` has a parent comment (`Parent`) and display the reply structure?

As an experienced website operations expert, I fully understand how to effectively display user comments in content management, especially comments with reply structures, which are crucial for enhancing user interaction and website activity.AnQiCMS (AnQiCMS) relies on its powerful template engine and flexible data tags to make this requirement easily achievable.Today, let's delve into how to cleverly judge whether a comment has a parent comment in the AnQiCMS `commentList` tag and elegantly present a clear reply structure.

2025-11-06

How to dynamically add a required asterisk or hint based on the `Required` property of the `guestbook` form label?

As an experienced website operation expert, I know that every detail can affect user experience and operation efficiency.When building a website, forms are an important part of interacting with users, and clear, friendly form design is the key to improving conversion rates.AnQiCMS (AnQiCMS) with its flexible template engine and powerful backend management features, has provided us with great convenience, especially in handling scenarios such as message forms that require users to fill in information.Today, let's delve deeply into a seemingly simple but extremely practical skill: how to use the `Required` field of the back-end field

2025-11-06

How to render different input controls (such as text, radio, checkbox) in the `guestbook` form based on the `Type` attribute?

As an experienced website operations expert, I know that a flexible and versatile content management system is the foundation of website success.AnQiCMS (AnQiCMS) with its excellent flexibility and powerful customization capabilities has become an invaluable tool in our hands.Today, let's delve deeply into a very practical topic in actual operation: how to intelligently render different input controls, such as text boxes, radio buttons, or checkboxes, in the `guestbook` form of Anqi CMS according to the preset field types in the background.This is not just an implementation at the technical level

2025-11-06