In AnQiCMS, how can the `commentList` tag implement pagination for comment data?

Calendar 👁️ 59

As an experienced website operations expert, I am well aware that in a content management system, the comment function not only increases user interaction but also brings activity and richness to the website.AnQiCMS (AnQiCMS) is an efficient content management system developed based on the Go language, which provides powerful comment functions while also considering the flexibility of template creation.Today, let's delve into how to usecommentListLabel elegantly implements the pagination of comment data.


In AnQiCMS,commentListHow does the label implement the pagination of comment data?

The importance of User Generated Content (UGC) in today's internet environment is self-evident.Comments are an important form of interaction between users and website content, which can significantly enhance the community feeling and user stickiness of the website.However, when the number of comments is large, if not managed and displayed reasonably, the page loading speed, user experience, and even server performance will be affected.For this, AnQiCMS providescommentListtags, with the help ofpaginationTags, can efficiently and flexibly implement pagination of comment data, ensuring that even with a large number of comments, the website can maintain a smooth browsing experience.

commentListTag: The core of comment data acquisition.

commentListThe tag is the starting point for obtaining comment data in the AnQiCMS template.It can not only pull the comment content, but more importantly, it can obtain data in "pagination mode", which lays a foundation for subsequent pagination display.

To enable pagination function,commentListthe tag needs to be set with two critical parameters:

  1. archiveIdThis parameter specifies the ID of the document (article, product, etc.) you want to retrieve comments for. Usually, on the document detail page, we willarchiveDetailLabel to get the current document ID and then pass it tocommentListFor example,archiveId=archive.IdRepresents getting comments for the current page document.
  2. type="page": This is the core to enable pagination mode. Whentypeis set to"page"then,commentListThe tag will intelligently split the comment data according to the current page number and the number of comments per page, and generate the necessary pagination information forpaginationlabel usage
  3. limit: is used to set the number of comments displayed per page. For example,limit="10"Displays 10 comments per page.

WhencommentListtags totype="page"When the mode runs, it will store the comment data in a variable that you can customize (for example, we often name itcommentsThis variable is an array containing comment objects, each of which includes the comment ID, username, content, posting time, review status, and whether it is a reply or not.

At the same time, it silently prepares a global "pagination object", which contains the total number of comments, total pages, current page number, first page, previous page, next page, last page, and links to all middle page numbers, which we will use laterpaginationaccess it by tag.

Let's take a look first.commentListThe basic structure of tags:

{% commentList comments with archiveId=archive.Id type="page" limit="10" %}
    {% for item in comments %}
        {# 在这里渲染每一条评论的具体内容 #}
        <div>
            <span>{{ item.UserName }}</span> 于
            <span>{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}</span> 评论道:
            <p>{{ item.Content|safe }}</p>
            {% if item.Parent %}
                <blockquote>回复 {{ item.Parent.UserName }}:{{ item.Parent.Content|truncatechars:50|safe }}</blockquote>
            {% endif %}
            {% if item.Status != 1 %}
                <p style="color: gray;">(评论正在审核中...)</p>
            {% endif %}
            <div class="comment-actions">
                <a href="javascript:;" data-id="{{ item.Id }}" class="reply-btn">回复</a>
                <a href="javascript:;" data-id="{{ item.Id }}" class="like-btn">点赞 ({{ item.VoteCount }})</a>
            </div>
        </div>
    {% empty %}
        <p>暂无评论,快来发表您的真知灼见吧!</p>
    {% endfor %}
{% endcommentList %}

In the above code,archive.IdIt is usually through the document detail page that wearchiveDetailTags such as{% archiveDetail archive with name="Id" %})get the current document ID. We also usedstampToDateThe filter to format time,|safeThe filter to ensure that HTML tags in the comment content can be rendered correctly, as well as|truncatecharsTo truncate the reply content, improve readability.{% empty %}Blocks elegantly handles the case where there are no comments.

paginationTag: Build user-friendly pagination navigation

It is not enough to just get the comment data, users also need an intuitive pagination navigation to browse comments on different pages. At this point,paginationthe tag comes into play.paginationTags are specifically used to generate navigation links such as "Home", "Previous Page", "Next Page",

paginationThe common parameters of the tag include:

  1. show: Controls the number of page numbers displayed in the pagination navigation. For example,show="5"it indicates that 5 page number digits are displayed before and after the current page number.

paginationThe tag creates a namedpagesThe object (you can define a variable name), thispagesThe object is very rich, containing all the status and links related to pagination:

  • TotalItems: Total number of comments.
  • TotalPages: Total number of pages.
  • CurrentPage: Current page number.
  • FirstPage/PrevPage/NextPage/LastPageThese are objects, each containingName(such as "Home") andLinkcorresponding URL).
  • PagesThis is an array that contains all the intermediate page number objects that need to be displayed, each object also hasName/LinkandIsCurrent(Indicates whether it is the current page).

Next ispaginationwith the tag andcommentListComplete example of using the tag with cooperation:

”`twig {# Assume that the archiveDetail tag has already retrieved the archive.Id of the current document #} {# For example: {% archiveDetail archive with name=“Id” %}{% set archiveId = archive %}{% endarchiveDetail %} #}

<h3>用户评论</h3>

{# 第一部分:评论列表显示 #}
{% commentList comments with archiveId=archive.Id type="page" limit="5" %}
    {% for item in comments %}
        <div class="comment-item">
            <div class="comment-meta">
                <strong>{{ item.UserName }}</strong> 于
                <span>{{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}</span> 发布
                {% if item.Status != 1 %}
                    <span style="color: #999;">(待审核)</span>
                {% endif %}
            </div>
            {% if item.Parent %}
                <div class="comment-parent-quote">
                    引用 @{{ item.Parent.UserName }} 的评论:
                    <p>{{ item.Parent.Content|truncatechars:80|safe }}</p>
                </div>
            {% endif %}
            <div class="comment-content">
                {{ item.Content|safe }}
            </div>
            <div class="comment-actions">
                <a href="#comment-form" onclick="replyComment({{ item.Id }}, '{{ item.UserName }}')">回复</a>
                <span class="like-count">点赞: {{ item.VoteCount }}</span>
            </div>
        </div>
    {% empty %}
        <p>暂无评论,快来抢沙发吧!</p>
    {% endfor %}
{% endcommentList %}

{# 第二部分:分页

Related articles

How to use the AnQiCMS `commentList` tag to display comments on the frontend page?

As an experienced website operations expert, I know that an active website cannot do without user interaction, and the comment feature is an important embodiment of user participation.In AnQiCMS (AnQiCMS), integrating and displaying comments is not difficult.Today, let's delve deep into how to use the powerful `commentList` tag of AnQiCMS to elegantly present comment content on your website's frontend page, thereby enhancing user engagement and content vitality.

2025-11-06

How to truncate long category names in the `categoryList` loop and add an ellipsis?

## Enterprise CMS Template Tips: Gracefully truncate long category names and add ellipsis In website operation, the display of category names may seem trivial, but it actually has a significant impact on user experience and page aesthetics.A well-designed website ensures that all its elements can coexist harmoniously, especially the navigation and text in the list.When the category name is too long, it may cause layout confusion, text overflow, and even poor display on different devices, seriously affecting the user's reading experience

2025-11-06

`categoryList` returns the category data whether it includes user group or permission-related setting information?

In the daily operation and template development of AnQi CMS, we often need to obtain site data from different angles, and the category list (`categoryList`) is undoubtedly one of the most commonly used tags.However, regarding whether the `categoryList` returned classification data includes user group or permission-related setting information?This topic, we delve into the design philosophy and functional implementation of AnqiCMS, revealing the logic behind it.

2025-11-06

How to handle the pagination problem when nesting `archiveList` inside `categoryList`?

As an experienced website operations expert, I have accumulated rich experience in the content management practice of AnQiCMS.I am well aware that when facing the powerful template tag system of AnQiCMS, how to skillfully combine them to achieve the expected content display effect is a challenge that many operators and developers will encounter.Today, let's delve into a common yet confusing issue: how to properly handle the pagination problem of `archiveList` when nesting `categoryList`?

2025-11-06

How to configure the `commentList` tag to display comments for a specific article (`archiveId`)?

I am glad to analyze the comment management and template configuration skills of Anqi CMS for you in depth.In website content operation, comments are an important bridge connecting users to content.AnQiCMS as an efficient content management system naturally also provides powerful comment functions and flexible template tags, helping us accurately control the display of comments. Today, we will focus on a common and core requirement: **How to configure the `commentList` tag to display comments for specific articles (`archiveId`) only?

2025-11-06

What sorting methods does the AnQiCMS comment list support (`order` parameter), and how to sort by the latest or the most popular?

As an experienced website operations expert, I am well aware of the importance of user interaction in content management.The comment feature is an important indicator of website activity, and how efficiently and friendly these comments are displayed directly affects user experience and the spread of content.AnQiCMS as a powerful content management system also provides flexible configuration options for comment management.Today, let's delve into the sorting method of AnQiCMS comment list, especially how to display comments based on the latest release or popularity.--- ##

2025-11-06

How to limit the number of comments displayed per page or in total in the `commentList` tag (using the `limit` parameter)?

As an experienced website operations expert, I know that paying attention to details often determines the quality of user experience and page performance.Comments as an important part of website interactivity directly affect users' perception of content through the rationality of their display methods.In a system like AnQiCMS (安企CMS) that is efficient and flexible, how to finely control the number of comments displayed is a topic worth in-depth discussion.Today, let's talk about the妙用 of the `limit` parameter in the `commentList` tag.

2025-11-06

What are the required fields on the AnQiCMS comment submission form to successfully post a comment?

In website operation, user comments are undoubtedly an important way to enhance website interactivity, accumulate community content, and obtain user feedback.For AnQiCMS users, understanding which core fields are required in the comment submission form is crucial, as this not only concerns whether the comment can be successfully published, but also directly affects user experience and the richness of website content.As an experienced website operations expert, today I will delve into the essential fields of the AnQiCMS comment submission form and the logic behind them.

2025-11-06