How to display the comment list of a document in AnQiCMS and implement pagination?

Calendar 👁️ 69

In AnQi CMS, adding a comment list to the document page and implementing pagination is a key step to improving user interaction experience.This not only allows visitors to participate in discussions and share opinions, but also brings more vitality to the website content.AnQi CMS provides intuitive and powerful template tags, allowing us to easily achieve these functions.

Integrate the comment feature into your document detail page

Generally, comment lists and comment forms are integrated into the document detail pages. In Anqicms, the template files for the document detail page are usually located in similararchive/detail.htmlThe position. Before starting, we need to ensure that the document detail page can correctly obtain the unique identifier of the current document, which is usually througharchiveDetailLabel implementation, for example, get the ID of the current document:{% archiveDetail with name="Id" %}.

1. Display the comment list of the document

AnQi CMS providescommentListThe tag to retrieve and display document comments. This tag is very flexible, and can be used to retrieve comments of specific documents, and supports various display methods, including list mode (list)and pagination mode(page)

To display the comment list on the document detail page and prepare for pagination, you can use it like thiscommentListTags:

<div class="comments-section">
    <h3>用户评论</h3>
    {% commentList comments with archiveId=archive.Id type="page" limit="10" %}
        {% for item in comments %}
        <div class="comment-item">
            <div class="comment-meta">
                <span>
                    {% if item.Status != 1 %}
                    审核中:{{item.UserName|truncatechars:6}}
                    {% else %}
                    {{item.UserName}}
                    {% endif %}
                </span>
                {% if item.Parent %}
                <span>回复</span>
                <span>
                    {% if item.Parent.Status != 1 %}
                    审核中:{{item.Parent.UserName|truncatechars:6}}
                    {% else %}
                    {{item.Parent.UserName}}
                    {% endif %}
                </span>
                {% endif %}
                <span>于 {{stampToDate(item.CreatedTime, "2006-01-02 15:04")}} 发布</span>
            </div>
            <div class="comment-content">
                {% if item.Parent %}
                <blockquote>
                    {% if item.Parent.Status != 1 %}
                    该内容正在审核中:{{item.Parent.Content|truncatechars:9}}
                    {% else %}
                    {{item.Parent.Content|safe}}
                    {% endif %}
                </blockquote>
                {% endif %}
                {% if item.Status != 1 %}
                    该内容正在审核中:{{item.Content|truncatechars:9}}
                {% else %}
                {{item.Content|safe}}
                {% endif %}
            </div>
            <div class="comment-actions">
                <a class="praise-button" data-id="{{item.Id}}">赞(<span class="vote-count">{{item.VoteCount}}</span>)</a>
                <a class="reply-button" data-id="{{item.Id}}" data-user="{{item.UserName}}">回复</a>
            </div>
        </div>
        {% empty %}
        <p>目前还没有评论,期待您的第一条评论!</p>
        {% endfor %}
    {% endcommentList %}
</div>

In this code, we:

  • Use{% commentList comments with archiveId=archive.Id type="page" limit="10" %}to retrieve comments.
    • archiveId=archive.IdMake sure to get the comments of the current document.archive.IdIt is obtained automatically or manually from the outer document detail template to get the current document ID.
    • type="page"Is the key to enabling pagination, it tells the system that you need paginated data.
    • limit="10"It specifies that 10 comments are displayed per page.
  • By{% for item in comments %}Loop through each comment.
  • Displays the username of the commentor (item.UserName)、comment content(item.ContentAnd the publish time(item.CreatedTime, usingstampToDateFiltered formatting()).
  • Special handling of replies (viaitem.ParentJudging whether there is a superior comment) and the review status of comments(item.Status != 1)
  • Added for comment content and parent comment content|safeA filter to ensure that HTML content (if comments support rich text) can be parsed correctly instead of being escaped.
  • Added "Like" and "Reply" buttons, which usually require JavaScript to interact with the backend API.

2. Implement pagination for the comment list

WhencommentListtags totype="page"A pattern used providespagesan object that contains all the information needed for pagination. We can usepaginationtags to generate pagination navigation based on this information.

Following the above comment list code, you can add the following pagination code:

<div class="pagination-section">
    {% pagination pages with show="5" %}
    <ul class="pagination-list">
        <li class="pagination-item {% if pages.FirstPage.IsCurrent %}active{% endif %}"><a href="{{pages.FirstPage.Link}}">{{pages.FirstPage.Name}}</a></li>
        {% if pages.PrevPage %}
            <li class="pagination-item"><a href="{{pages.PrevPage.Link}}">{{pages.PrevPage.Name}}</a></li>
        {% endif %}
        {% for item in pages.Pages %}
            <li class="pagination-item {% if item.IsCurrent %}active{% endif %}"><a href="{{item.Link}}">{{item.Name}}</a></li>
        {% endfor %}
        {% if pages.NextPage %}
            <li class="pagination-item"><a href="{{pages.NextPage.Link}}">{{pages.NextPage.Name}}</a></li>
        {% endif %}
        <li class="pagination-item {% if pages.LastPage.IsCurrent %}active{% endif %}"><a href="{{pages.LastPage.Link}}">{{pages.LastPage.Name}}</a></li>
    </ul>
    <p class="pagination-info">总评论数:{{pages.TotalItems}}条,共{{pages.TotalPages}}页,当前第{{pages.CurrentPage}}页。</p>
    {% endpagination %}
</div>

Here:

  • {% pagination pages with show="5" %}UsepagesGenerate pagination for the object.
  • show="5"The parameter limits the maximum display of middle page numbers to 5.
  • We traversed.pagesWithin the objectFirstPage(Home page),PrevPage(Previous page),Pages(Page number array in the middle),NextPage(Next page),LastPage(End page), and according toIsCurrentfield to add the current pageactiveClass.
  • item.LinkProvided the correct page link,item.NameProvided page text (such as "1
  • Finally, it also displays the total number of comments, total pages, and current page number, allowing users to get an overview of the comments at a glance.

3. Add comment submission form

To allow users to post comments, we need a submission form.This form typically includes a username, comment content, and a hidden field to specify the document ID of the comment.

`twig

<h3>发表您的评论</h3>
<form method="post" action="/comment/publish">
    <input type="hidden" name="archive_id" value="{% archiveDetail with name="Id" %}">
    <input type="hidden" name="parent_id" value="0" id="comment-parent-id"> {# 用于回复功能,默认为0 #}
    <p id="reply

Related articles

How to retrieve and display the list of documents under a specific Tag label in AnQiCMS?

In Anqi CMS, managing website content, the Tag tag is undoubtedly a very flexible and practical tool that helps us classify different categories and even different model content according to themes, greatly enriching the organization of content and the browsing experience of users.So when we want to display a list of all documents under a specific Tag on a dedicated page, Anqi CMS provides a very intuitive and powerful template tag to help us achieve this goal.### Understanding the role of Tag tags in Anqi CMS Before delving into the code

2025-11-08

How to display the description and link of a specific Tag label in AnQiCMS?

In Anqi CMS, a tag is a powerful content organization tool that not only helps you classify content more finely but also optimizes the website's SEO performance and improves the user's browsing experience.Sometimes, you may wish to display not only the name of the tag at a certain position on the website's frontend, but also its detailed description information, or even jump directly to the link related to the content of the tag.This is not a difficult task, Anqi CMS provides flexible template tags, allowing you to easily meet these needs.

2025-11-08

How to list all Tag tags in the AnQiCMS template?

In Anqi CMS, using the template function to display all Tag tags on the website is an important operation that helps improve content discoverability and website SEO.AnQiCMS as an efficient content management system provides an intuitive and flexible tag (Tag) management mechanism, allowing users to easily list this content in website templates. ### Learn about AnQiCMS's Tag Function In AnQiCMS, tags (Tag) can be seen as topics or keywords that connect different content.They do not follow a strict hierarchical structure like categories

2025-11-08

How to use the filtering feature of AnQiCMS to display a document list under specific conditions?

How to manage a large amount of content in AnQiCMS and allow users to quickly find the information they need is one of the key factors in content operation.Fortunately, AnQiCMS provides powerful filtering functions, allowing you to flexibly display document lists according to specific conditions, thereby greatly enhancing the website's user experience and content search efficiency. ### Clearly define your filtering requirements: Starting from the backend content model To make use of AnQiCMS's filtering feature, you first need to clarify the conditions through which you want users to search for content.

2025-11-08

How to generate and display the website's message form in AnQiCMS templates?

In website operation, an efficient and convenient feedback form is an important bridge for user interaction with the website.It not only collects user feedback, but also serves as the entry point for potential customers to obtain information.AnQiCMS as an enterprise-level content management system provides powerful and flexible functions in this aspect, allowing us to easily generate and manage comment forms in templates.

2025-11-08

How to get and display the list of friendship links configured in the AnQiCMS background?

In website operation, friendship links play an indispensable role. They not only bring valuable external traffic to the website but also help improve search engine optimization (SEO) effects, enhance the authority and credibility of the website.For users using AnQiCMS, managing and displaying friend links is a simple and efficient process.This article will introduce in detail how to configure friend links in the AnQiCMS background, as well as how to elegantly present them in your website front-end template.In AnQiCMS admin manage friend links First

2025-11-08

How to format a timestamp and display it as a readable date and time in AnQiCMS?

In website content operation, time information plays an indispensable role.Whether it is the publication time of the article, the shelf date of the product, or the submission time of the comments, a clear and readable date and time format can greatly enhance the user experience.AnQi CMS as an efficient content management system, fully considers this point, and provides a flexible way to format and display these timestamp data.### Understanding Timestamps in AnQi CMS In the AnQi CMS backend, when we publish articles, products, or perform other content management operations

2025-11-08

How to implement conditional judgment in AnQiCMS template to control the display of content?

In AnQi CMS template design, flexibly controlling the display of content is the key to building dynamic, responsive websites.Whether it is to display different information based on page type, data status, or specific conditions, conditional judgment is an indispensable tool.The AnQiCMS template engine provides an intuitive and powerful conditional judgment mechanism, allowing you to easily implement these complex logic.### Core Grammar: The Basics of Conditionals The condition judgment in AnQiCMS templates is similar to many mainstream template engines, using the `{% if ... %}` tag structure

2025-11-08