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

Calendar 👁️ 69

As an experienced website operations expert, I am well aware of 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 operation.When building a content-rich website, good pagination navigation not only enhances user experience but is also a key aspect of search engine optimization.

Today, let's delve into a very practical template feature in AnQiCMS: how to cleverly traversepages.PagesAn array that dynamically generates a set of aesthetically pleasing and practical clickable middle page number links. This is not just a technical implementation, but also an operation strategy to enhance the professionalism and user stickiness of the website.


Page magic of AnQi CMS: Creating clickable page number links for seamless reading experience

In the vast world of websites, how can users efficiently find the information they need?In addition to precise search and clear classification, a well-designed and responsive pagination navigation system is also indispensable.AnQi CMS is well-versed in this, providing us with a set of intuitive and powerful pagination tags, making it easy to dynamically generate page numbers.Say goodbye to the麻烦 of manually maintaining page numbers, allowing the system to intelligently guide users.

The core we focus on is the AnQi CMS template engine,paginationthe tag returns thepagesobject. thispagesThe object is the intelligent center of the entire pagination mechanism, it not only contains an overview of the current pagination status, such as the total number of entriesTotalItems, total number of pagesTotalPages, and the current pageCurrentPagebut more importantly, it provides a namedPagesarray. ThisPagesThe array is the key to dynamically generating the middle page link.

Core: Traversepages.PagesArray, dynamically generate page links

Imagine when a user is browsing a list page, they hope to see a page number sequence similar to "1 2 3 ... 7 8 9", allowing them to directly click and jump to any page. Anqi CMS'spages.PagesThe array is born for this. It internally stores a series of objects representing the middle page numbers, each of which contains all the information needed to build clickable links.

Let us go through a specific template code example to analyze how to iterate through this array and render it in combination with the actual state of the page:

<div class="pagination-container">
    {% pagination pages with show="5" %}
        {# 渲染“首页”链接,如果存在且不是当前页 #}
        {% if pages.FirstPage and not pages.FirstPage.IsCurrent %}
            <a href="{{ pages.FirstPage.Link }}" class="page-link">{{ pages.FirstPage.Name }}</a>
        {% endif %}

        {# 渲染“上一页”链接,如果存在 #}
        {% if pages.PrevPage %}
            <a href="{{ pages.PrevPage.Link }}" class="page-link">{{ pages.PrevPage.Name }}</a>
        {% endif %}

        {# 遍历并渲染中间页码链接 #}
        {% for item in pages.Pages %}
            <a href="{{ item.Link }}" class="page-link {% if item.IsCurrent %}active{% endif %}">{{ item.Name }}</a>
        {% endfor %}

        {# 渲染“下一页”链接,如果存在 #}
        {% if pages.NextPage %}
            <a href="{{ pages.NextPage.Link }}" class="page-link">{{ pages.NextPage.Name }}</a>
        {% endif %}

        {# 渲染“尾页”链接,如果存在且不是当前页 #}
        {% if pages.LastPage and not pages.LastPage.IsCurrent %}
            <a href="{{ pages.LastPage.Link }}" class="page-link">{{ pages.LastPage.Name }}</a>
        {% endif %}
    {% endpagination %}
</div>

In the above code, we first use{% pagination pages with show="5" %}Tag to initialize pagination data. Here is theshow="5"It is a very practical parameter, which determines how many intermediate page number links are displayed before and after the current page number (excluding "Home", "Previous", "Next", "End").This setting can effectively control the length of pagination navigation, avoiding visual confusion caused by too many page numbers, which is particularly important on mobile end views.

Next, we go through a series of{% if %}condition judgments to render the 'Home' and 'Previous Page' links.pages.FirstPageandpages.PrevPagewhich represent the detailed information of the 'Home' and 'Previous Page', including their links (LinkAnd display name (NameAnd a boolean valueIsCurrentUsed to determine if it is the current page. We usually only display them when they are not the current page, ensuring the logic and user experience of navigation.

The real core lies in{% for item in pages.Pages %}loop. Here,pages.PagesarrayitemRepresents an independent middle page number.item.LinkIs the URL address corresponding to the page number,item.NameWhich is the page number displayed on the page.item.IsCurrentBoolean values can help us add special styles to the current page number (such as highlighting), thus clearly telling users where they are.

Finally, similarly, we pass through{% if %}render the "next page" and "last page" links by conditional judgment, completing the construction of the entire pagination navigation.

Integrate pagination into the actual content list

In order topaginationLabels can correctly retrieve and process pagination data, which is usually used witharchiveList/tagDataListor other content list labels. When using these list labels, be sure totypethe parameter to"page"And specifylimitTo control the number of articles displayed per page, for example:

{% archiveList archives with type="page" limit="10" %}
    {# 这里是文章内容的循环渲染区域 #}
    {% for item in archives %}
        <div class="article-item">
            <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
            <p>{{ item.Description }}</p>
        </div>
    {% endfor %}
    {% empty %}
        <p>抱歉,目前没有相关内容。</p>
{% endarchiveList %}

{# 在此下方放置我们之前定义的分页代码块 #}
<div class="pagination-container">
    {# ... 你的 pagination 标签和循环代码 ... #}
</div>

Through such a combination,archiveListResponsible for fetching and preparing the article data for the current page from the backend, and will also pass all the necessary context information for pagination.paginationTag, so that it can accurately generate the corresponding page number links.

Consideration of design aesthetics and user experience

Dynamic page number generation is not just a functional implementation, but also a fine-tuning of user experience. We can utilizeitem.IsCurrentThis property, in conjunction with the front-end CSS style, gives the current page number a unique visual effect, such as changing the background color, font color, or adding a border, so that users can identify it at a glance. At the same time,show="5"This parameter provides flexible control, allowing us to adjust the number of middle page numbers according to the page layout and content density,

Related articles

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 handle the requirement that the `pagination` tag does not display pagination navigation when the `archiveList` query result is empty?

As an experienced website operations expert, I am more than happy to delve 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 aesthetics of the page, but also directly affects the user experience and the professionalism of the website. --- ## How to elegantly hide pagination navigation when `archiveList` query results are empty?

2025-11-07

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

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

How to ensure SEO-friendliness of pagination links in AnQiCMS, avoiding duplicate content or waste of crawling budget?

## Ensure SEO-friendliness of pagination links in AnQiCMS: Avoid duplicate content and crawling budget waste As an experienced website operations expert, I know full well that pagination links are a double-edged sword when it comes to the impact on website SEO.On one hand, they can effectively organize a large amount of content and enhance the user's browsing experience; on the other hand, if not handled properly, they may also lead to waste of search engine crawling budget, excessive duplicate content, and even affect the overall ranking of the website.

2025-11-07

Will the `pagination` tag automatically generate the `rel="nofollow"` attribute to some pagination links?

As an experienced website operations expert, I fully understand the importance of each tag and attribute for website search engine optimization (SEO).When it comes to the presentation and indexing of website content, the handling of pagination links is particularly worthy of attention.Today, let's delve into the `pagination` tag in AnqiCMS and whether it automatically adds the `rel="nofollow"` attribute to pagination links.

2025-11-07