How to display comments of the `commentList` tag

Calendar 👁️ 66

In website operation, article comments are an indispensable part to enhance user interaction and activate the community atmosphere. AnQiCMS (AnQiCMS) provides us with a powerful and flexible comment management function, by cleverly usingcommentListTags, we can easily display article comments on the front end of the website.

This article will delve deeper intocommentListVarious features of the tag and their actual application in the template, helping you create a highly interactive and user-friendly comment section.


Core tags:commentListOverview

In AnQi CMS,commentListTags are the core tools used to retrieve and display the comment list of specific articles.It allows us to flexibly control the source, sorting, number, and even whether to display pagination of comments.No matter if you want to display comments at the bottom of the article detail page, or build an independent comments page,commentListAll of them can meet your needs.

commentListThe basic usage form of the tag is as follows:

{% commentList comments with archiveId="文章ID" type="page|list" %}
    {# 循环输出评论内容 #}
{% endcommentList %}

Here, commentsIs the variable name you specified for the comment list you obtained, you can loop through this variable within the tag to access the specific information of each comment.

Parameter Details: Fine Control of Comment Display

To better control the display of comments,commentListThe tag provides several important parameters:

  1. archiveId: Specify the article to which the comment belongsThis parameter is crucial, it tells the system which article's comments you want to retrieve. In most cases, on the article detail page, you canarchive.IdDynamically obtain the current article ID. For example:archiveId=archive.IdIf you do not specifyarchiveIdThe system will try to obtain the document ID of the current page by default.

  2. type: Select list display mode

    • type="list": When you want to display all comments (or a specified number of comments) without pagination, choose this mode.
    • type="page": If you need to paginate comments, such as showing 10 per page, then select this mode. It works with Anqicms pagination tags.pagination, you can build a complete pagination navigation.
  3. order: Define the comment sorting methodYou can control the display order of comments through this parameter:

    • order="id desc": Default value, sorts comments in descending order by comment ID, usually meaning the latest comments are at the top.
    • order="id asc": Comments are sorted in ascending order by comment ID, which usually means the oldest comments are at the top.
  4. limit: Limits the number of comments displayed.This parameter is used to set the number of comments you want to display.

    • If you want to display a specified number of comments, for examplelimit="10".
    • It also supportsoffset,limitpatterns, such aslimit="2,10"It means starting from the 3rd comment and displaying 10 comments.
  5. siteId: Comment call in a multi-site environmentFor users of the AnQi CMS deployed on multiple sites, if you need to call the comment data of other sites, you cansiteIdThe parameter specifies the ID of the target site. This parameter is usually not set in a single-site environment.

Comment data field: learn about the available information.

commentListTags retrieved from comments.commentsA variable is an array object, eachitemrepresents a comment, and includes the following important field information:

  • Id: The unique ID of the comment.
  • ArchiveId: The ID of the article the comment belongs to.
  • UserName: The nickname or name of the comment user.
  • UserId: The ID of the comment user (if the user is logged in).
  • Ip: The IP address of the comment user.
  • VoteCount: The number of likes the comment has received.
  • Content: The specific content of the comment.Please note that when displaying HTML content submitted by users, to prevent HTML escaping from causing tags to be displayed as plain text, it may be necessary to use|safeFilter, for example{{item.Content|safe}}. But also be alert to potential security risks and ensure that the content has been strictly filtered.
  • ParentId: If this is a reply, it points to the ID of the parent comment.
  • Status: Review status of the comment.Status = 1Indicates that the comment has been reviewed and displayed.Status = 0Indicates that the comment is being reviewed and should not usually be displayed on the front end.
  • Parent: If there is a parent comment, this field will contain the complete data object of the parent comment, with the same structure as the current comment.itemfor easy display of the reply relationship.
  • CreatedTime: The timestamp of the comment, you can usestampToDatetags to format it, for example{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}.

Two common ways to display comments:

After understanding the label parameters and comment data structure, let's take a look at how to display comments in the front-end template.

1. Display of conventional comment list

When you want to list all comments (or a specified number) on a single page without pagination, you can usetype="list":

{# 假设我们正在文章详情页,archive.Id 可用 #}
<div class="comments-section">
    <h3>读者评论</h3>
    {% commentList comments with archiveId=archive.Id type="list" limit="5" %}
        {% for item in comments %}
            {# 仅显示已审核通过的评论,或根据业务需求显示审核中的占位符 #}
            {% if item.Status == 1 %}
                <div class="comment-item">
                    <div class="comment-meta">
                        <strong>{{item.UserName}}</strong>
                        于 {{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}
                        {% if item.Parent %}
                            回复 <strong>{{item.Parent.UserName}}</strong>
                        {% endif %}
                    </div>
                    <div class="comment-content">
                        {{item.Content|safe}} {# 使用|safe避免HTML转义,请确保内容已通过后端安全过滤 #}
                    </div>
                    <div class="comment-actions">
                        <a href="#" class="praise-btn" data-id="{{item.Id}}">赞 ({{item.VoteCount}})</a>
                        <a href="#" class="reply-btn" data-id="{{item.Id}}" data-user="{{item.UserName}}">回复</a>
                    </div>
                </div>
            {% elif item.Status == 0 %}
                 <div class="comment-item comment-pending">
                     <p>您的评论正在审核中,审核通过后将显示。</p>
                 </div>
            {% endif %}
        {% empty %}
            <p class="no-comments">暂无评论,快来发表您的看法吧!</p>
        {% endfor %}
    {% endcommentList %}
</div>

2. Paginated comment list display

For articles with a large number of comments, pagination can greatly improve page loading speed and user experience. At this time, it is necessary totype="page"and combinepaginationTags:

`twig {# Assuming we are on the article detail page, archive.Id is available #}

<h3>读者评论</h3>
{% commentList comments with archiveId=archive.Id type="page" limit="10" %}
    {% for item in comments %}
        {% if item.Status == 1 %}
            <div class="comment-item">
                <div class="comment-meta">
                    <strong>{{item.UserName}}</strong>
                    于 {{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}
                    {% if item.Parent %}
                        回复 <strong>{{item.Parent.UserName}}</strong>
                    {% endif %}
                </div>
                <div class="comment-content">
                    {{item.Content|safe}}
                </div>
                <div class="comment-actions">
                    <a href="#" class="praise-btn" data-id="{{item.Id}}">赞 ({{item.VoteCount}})</a>
                    <a href="#" class="reply-btn" data-id="{{item.Id}}" data-user="{{item.UserName}}">回复</a>
                </div>
            </div>
        {% endif %}
    {% empty %}
        <p class="no-comments">暂无评论,快来发表您的看法吧!</p>
    {% endfor %}
{% endcommentList %}

{# 分页导航 #}

Related articles

How to get and display the carousel or advertisement images on the homepage using the `bannerList` tag?

In the operation of a website, the homepage carousel or advertisement image is undoubtedly the golden area to attract visitors' attention, convey core information, and promote important content.They not only enhance the visual appeal of a website, but are also crucial for guiding user behavior and promoting content consumption.In Anqi CMS, the functions to implement and manage these carousel or ad images are all concentrated on a powerful and easy-to-use tag —— `bannerList`.

2025-11-07

How to build a dynamic filter using the `archiveFilters` tag to display documents that meet specific conditions?

How to help users quickly find the information they are interested in when managing a content-rich website, and improve the access experience, is a challenge that every content operator needs to face.AnQiCMS (AnQiCMS) understands this need and therefore provides a powerful `archiveFilters` tag, allowing you to easily build dynamic filters to provide flexible and diverse search methods for website content. Dynamic filter, as the name implies, is to automatically generate filtering conditions for users to choose from based on different attributes of the content.

2025-11-07

How does the `archiveParams` tag display custom field data for AnQiCMS documents?

In AnQiCMS, the flexibility of website content is one of its highlights, which is attributed to its powerful content model custom field function.In addition to standard fields such as article title, content, and publication time, you can also add various custom fields according to business needs for different content models (such as articles, products), such as 'author', 'product model', 'house area', or 'service features'.These custom fields greatly enrich the expression of the website content, making it more closely aligned with practical application scenarios.So, when you tirelessly define and fill in these custom fields in the background

2025-11-07

How to dynamically set and display the page's Title, Keywords, and Description using the `tdk` tag to optimize SEO?

How to make our content better discovered by search engines and attract more potential users is a continuous challenge in website operation.Among them, the Title (title), Keywords (keywords), and Description (description) these three TDK tags are like a series of 'business cards' on our website in the search engine results page, directly affecting the user's click intention and the understanding of the search engine.AnQi CMS knows this and therefore fully considered the dynamic setting of TDK and SEO friendliness in the initial system design.<h3>Importance of TDK

2025-11-07

In Anqi CMS template, how to center the product title within a fixed area?

In Anqi CMS template, centering the product title within a fixed area is a common layout requirement, which can effectively enhance the visual aesthetics and user experience of the website.Due to the similar Django template engine syntax adopted by AnQi CMS and its good support for frontend styles, we can achieve this goal by combining HTML structure and CSS styles. ### Understanding Anqi CMS Template Structure Firstly, we need to clarify how the product title is called in the Anqi CMS template.

2025-11-07

How to use the AnQiCMS `ljust` filter to align the navigation menu text to the left and fill with spaces?

AnQi CMS is an efficient and flexible content management system that provides users with rich template tags and filters, helping us easily customize all aspects of the website.Today, we will explore a practical template trick: how to use the `ljust` filter to align your navigation menu text neatly to the left and intelligently fill in spaces, thereby improving the visual consistency and user experience of the website.

2025-11-07

How to align the date and time stamp to the right to a specified length on the AnQiCMS article detail page?

On the article detail page of the website, we often hope that the publication date or update date of the article can be displayed neatly, and sometimes they need to be aligned to the right and occupy a fixed width, so that the page looks professional and beautiful.AnQiCMS provides flexible template tags and filters to help us easily meet this requirement. ### Understanding AnQiCMS Date Handling AnQiCMS typically stores dates and times in the form of timestamps (timestamps).This means calling `{{

2025-11-07

How to distribute the padding spaces on both sides when the string length is odd for the AnQiCMS `center` filter?

In AnQiCMS template development, we often need to fine-tune text layout and alignment to make the page content look neater and more beautiful.At this time, filters like `center` are particularly practical.It can help us easily center the string and automatically fill in spaces on both sides.But, when you start using it, you may be curious about a detail: how does AnQiCMS distribute the spaces on both sides when the number of spaces to be filled is odd?### The basic function of the `center` filter First

2025-11-07