How to display the comment list and pagination on the article detail page with the `commentList` tag?

Calendar 👁️ 64

In Anqi CMS, the comment list on the article detail page is an important component for enhancing user interaction and content vitality. To present these comments naturally and smoothly, and to achieve a friendly pagination experience, we need to use clever techniques.commentListandpaginationThese two template tags. Let's take a look together on how to implement the display and pagination of comments on the article detail page.


I.commentListTags: The basic method to display comments

Of Security CMScommentListThe tag is specifically used to retrieve and display article comment data.In the article detail page, it can help us easily retrieve all comments of the current article, whether it is the latest comment or a reply comment, it can be handled through this tag.

The basic usage is:

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {# 在这里循环展示评论 #}
{% endcommentList %}

Among them,commentsThis is the variable name we define for the comment list data, you can name it according to your habit.

Explanation of several key parameters:

  • archiveIdThis parameter is essential, it tellscommentListThe tag needs to retrieve the comments of which article. In the article detail page, we can obtain the current article ID byarchive.Id(provided byarchiveDetailthe tag or page context).
  • typeThis parameter determines the display method of the comment list. When set totype="list"it will retrieve a specified number of comments; if set totype="page"it will enable pagination, usually in conjunction withpaginationlabel usage
  • limit:Whentype="list"When, this parameter specifies the maximum number of comments to display. For example,limit="6"means only the latest 6 comments are displayed.
  • order: can be used to control the sorting method of comments, such asorder="id desc"Indicates sorting comments by comment ID in reverse order (latest comments first).
  • siteIdIf you manage multiple sites and need to call comments from other sites, you can specify the site ID through this parameter.

Common fields contained in comment data:

commentsEach comment generated by the variable (we usually name it as)item) contains some useful fields, such as:

  • Id: The unique ID of the comment.
  • UserName: The nickname of the reviewer.
  • Content: The specific content of the comment.
  • CreatedTime: The timestamp of the comment, it needs to bestampToDateformatted.
  • Status: The review status of the comment,1indicating approved,0Pending review.
  • ParentId: If this is a reply comment, it will point to the ID of the replied comment.
  • Parent: An object containing the complete data of the replied comment, convenient for displaying the comment hierarchy.

Chapter 2: Hands-on Practice: Displaying Non-paginated Comment List

We start with the simplest comment list, assuming we want to display the latest few comments at the bottom of the article detail page without pagination.

{# 首先,确保你能获取到当前文章的ID。在文章详情页通常可以直接使用archive.Id #}
{# 如果页面上下文没有archive.Id,你可能需要先使用{% archiveDetail with name="Id" %}来获取 #}

<div class="comments-section">
    <h3>读者评论</h3>
    <ul class="comment-list">
        {% commentList comments with archiveId=archive.Id type="list" limit="5" order="id desc" %}
            {% for item in comments %}
                {# 检查评论状态,只显示已审核通过的评论,或给出审核中的提示 #}
                {% if item.Status == 1 %}
                    <li class="comment-item">
                        <div class="comment-meta">
                            <span class="comment-author">{{ item.UserName }}</span>
                            于
                            <span class="comment-time">{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}</span>
                            说:
                        </div>
                        <div class="comment-content">
                            {% if item.Parent %}
                                <blockquote class="reply-to">
                                    回复 <span class="original-author">{{ item.Parent.UserName }}</span>:
                                    <p>{{ item.Parent.Content|truncatechars:100 }}</p> {# 截取部分内容,避免太长 #}
                                </blockquote>
                            {% endif %}
                            <p>{{ item.Content }}</p>
                        </div>
                        {# 这里可以添加点赞或回复按钮,具体功能需要JS和后台接口支持 #}
                        <div class="comment-actions">
                            <a href="javascript:;" class="btn-reply" data-id="{{ item.Id }}" data-user="{{ item.UserName }}">回复</a>
                        </div>
                    </li>
                {% else %}
                    <li class="comment-item pending-review">
                        <div class="comment-meta">
                            <span class="comment-author">{{ item.UserName }}</span>
                            于
                            <span class="comment-time">{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}</span>
                            评论:
                        </div>
                        <div class="comment-content">
                            <p>您的评论正在审核中,请耐心等待。</p>
                        </div>
                    </li>
                {% endif %}
            {% empty %}
                <li class="no-comments">目前还没有评论,快来发表您的看法吧!</li>
            {% endfor %}
        {% endcommentList %}
    </ul>
</div>

Code analysis:

  • We used a<ul>List to wrap comments, each comment is one<li>.
  • item.StatusUsed to determine whether the comment has been reviewed. Unreviewed comments show a prompt and usetruncatecharsa filter to truncate content.
  • item.ParentThe judgment is very important, it allows us to identify and display the reply relationship, making the comments look more organized. Whenitem.ParentWhen present, it indicates that this is a reply, and we can display the nickname and part of the content of the replied person.
  • stampToDate(item.CreatedTime, "2006-01-02 15:04")Format the timestamp into a readable date and time format that we can understand.
  • {% empty %}A block displays a friendly prompt when there are no comments.

3. Advanced Application: Comment Pagination Display

When there are many comments on the article, loading all comments at once can affect the page performance and user experience. At this point, we need to introduce pagination. In Anqi CMS, commentListcooperatepaginationTags can perfectly solve this problem.

The steps are as follows:

  1. modifycommentListoftypeparameters for"page": This tells the system that we need paginated data.
  2. addpaginationTag: This tag will generate pagination navigation links and information.

`twig

<h3>读者评论</h3>
<ul class="comment-list">
    {% commentList comments with archiveId=archive.Id type="page" limit="10" order="id desc" %}
        {% for item in comments %}
            {% if item.Status == 1 %}
                <li class="comment-item">
                    <div class="comment-meta">
                        <span class="comment-author">{{ item.UserName }}</span>
                        于
                        <span class="comment-time">{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}</span>
                        说:
                    </div>
                    <div class="comment-content">
                        {% if item.Parent %}
                            <blockquote class="reply-to">
                                回复 <span class="original-author">{{ item.Parent.UserName }}</span>:
                                <p>{{ item.Parent.Content|truncatechars:100 }}</p>
                            </blockquote>
                        {% endif %}
                        <p>{{ item.Content }}</p>
                    </div>
                    <div class="comment-actions">
                        <a href="javascript:;" class="btn-reply" data-id="{{ item.Id }}" data-user="{{ item.UserName }}">回复</a>
                    </div>
                </li>
            {% else %}
                <li class="comment-item pending-review">
                    <div class="comment-meta">
                        <span class="comment-author">{{ item.UserName }}</span>
                        于
                        <span class="comment-time">{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}</span>
                        评论:
                    </div>
                    <div class="comment-content">
                        <p>您的评论正在审核中,请耐心等待。</p>
                    </div>
                </li>
            {% endif %}
        {% empty %}
            <li class="no-comments">目前还没有评论,快来发表您的看法吧!</li>
        {% end

Related articles

How does the `tagDataList` tag display all document lists under a specific tag?

In Anqi CMS, managing and displaying content, the tag (Tag) is undoubtedly a very practical tool.It can help us organize documents more flexibly, realize multi-dimensional content aggregation, and greatly benefit the improvement of the website's SEO performance and user experience.When we need to display a list of all documents under a specific tag page, or at any location on the website, the `tagDataList` tag is an indispensable tool.###

2025-11-08

How to retrieve and display the details of a specific `tagDetail` tag?

AnQiCMS (AnQiCMS) provides a series of powerful template tags to help website operators efficiently display content.Among them, the `tagDetail` tag is a key tool for obtaining and displaying specific tag details.Whether it is to build a beautiful tag detail page or to refer to related data of tags on other pages, `tagDetail` can provide a flexible and intuitive solution.

2025-11-08

How to display the associated tags of the `tagList` and implement a tag cloud effect?

## Utilizing AnQiCMS's `tagList` tag, easily implement document association and tag cloud display Tags play a crucial role in website content operations.They can not only help users quickly find the content they are interested in, improve the convenience of site navigation, but also optimize the search engine's understanding of the website's content through keyword aggregation, thereby bringing better SEO performance.AnQiCMS fully considered this requirement, built-in powerful tag features, and provided flexible template tags `tagList`

2025-11-08

How to build a complex search interface for product filtering or property filtering using the `archiveFilters` tag?

In the era of content explosion, when users visit websites, they often hope to quickly find the content they are interested in.Whether it is browsing a large number of products on e-commerce platforms or looking for housing that meets your preferences on real estate websites, a powerful and easy-to-use filtering and search interface is the key to improving user experience. AnQiCMS (AnQiCMS) knows this and provides a powerful `archiveFilters` tag for this purpose.This tag can help you easily build a complex product or property filtering interface, making your website content more discoverable and interactive

2025-11-08

How to quickly generate a customizable website guestbook form with the `guestbook` tag?

In website operation, interacting with visitors and collecting feedback is an important link to improve user experience and obtain valuable information, and the comment form is the core tool to achieve this goal.AnQiCMS (AnQiCMS) fully understands its importance and provides the `guestbook` tag for users, allowing you to easily and flexibly build a powerful and highly customizable website guestbook form.

2025-11-08

How to use the `linkList` tag to display the list of friends' links on the website?

During the operation of our website, friendship links not only help to increase the amount of traffic, but are also an indispensable part of SEO optimization.A healthy link ecosystem that can effectively pass on weight and enhance the authority of the website.In AnqiCMS, managing and displaying friend links is very convenient, mainly due to its built-in `linkList` tag.This article will introduce in detail how to use the `linkList` tag to display the friend link list on your AnqiCMS website.

2025-11-08

How to use the `pagination` tag to generate a beautiful pagination navigation for articles or product lists?

In website operation, as the content volume continues to grow, whether it is articles, products, or other lists, how to efficiently organize and display these contents while ensuring a smooth user browsing experience and a friendly search engine is a problem that needs to be carefully considered.Page navigation is the key tool to solve this problem.AnQiCMS provides a powerful and flexible `pagination` tag that allows you to easily create beautiful and functional pagination navigation for various lists.

2025-11-08

How to flexibly control the conditional display and loop iteration of logic tags such as `if` and `for` in the template?

In the world of AnQi CMS templates, we often need to display content based on different conditions or repeat a series of data, at this point, the `if` and `for` logical tags become particularly important.They are the foundation for building dynamic content and implementing flexible layouts in templates, allowing us to precisely control the presentation of every piece of information on the web page.

2025-11-08