How to display a user comment list on the front end and support review status display?

Calendar 👁️ 66

User comments are an important manifestation of a website's vitality, they can not only increase the interactivity of the content, but also provide valuable references for other visitors.The Anqi CMS understands the importance of comment management, providing simple yet powerful features, allowing you to flexibly display comment lists on the website front-end and clearly identify the review status of comments.

The core of displaying the comment list on the front-end:commentListTag

You need to use the AnQiCMS provided to display user comments on your website front pagecommentListTemplate tag. This tag helps you easily retrieve comment data related to a specific article or product.

In most cases, the comment list is associated with a specific document (such as an article detail page or a product detail page). Therefore, when usingcommentListtags, we usually specifyarchiveIdThe value of this parameter is the unique identifier ID of the current page document. You can use{% archiveDetail with name="Id" %}tags to get the ID of the current document.

Moreover, to better control the display of comments, you cantypeselect the list type through parameters. When you want the comment list to support pagination, you willtypeis set to"page"It is a good choice; if you only need to display a fixed number of comments without pagination, you can set it to"list"coordinate withlimitParameter.

For example, on the article detail page, you can start fetching comment data like this

{% archiveDetail archiveId with name="Id" %} {# 获取当前文档的ID,并赋值给archiveId变量 #}

<div class="comments-section">
    <h3>用户评论 ({{ archive.CommentCount }}条)</h3>
    {% commentList comments with archiveId=archiveId type="page" limit="10" %}
        {# 遍历获取到的评论列表 #}
        {% for item in comments %}
            {# 这里是每条评论的展示内容 #}
        {% empty %}
            <p>目前还没有评论,快来抢沙发吧!</p>
        {% endfor %}
    {% endcommentList %}
</div>

In this example,commentsThe variable will contain all comments that meet the criteria.forThe loop will process each comment one by one,emptyThe block will display a prompt when there are no comments.

Clearly identify the review status

AnQiCMS has fully considered the needs of content review in comment management, so each comment data has aStatusThe field is the key to distinguishing whether the comment has passed the review.

  • WhenStatusWith1Means the comment has passed the review and can be displayed normally on the front end.
  • WhenStatusWith0When, it means that the comment is still under review, at this time we usually do not display it completely, but give the user a "under review" prompt.

UtilizeifLogical judgment tag, we can easily baseitem.StatusThe value to control the display content of comments. For example, we can only display approved comment content, and for comments under review, we can only display a username and a "under review" prompt, and even truncate or hide the content.

Here is an example of displaying the review status in a comment list and handling replies.

{% archiveDetail archiveId with name="Id" %}

<div class="comments-section">
    <h3>用户评论 ({{ archive.CommentCount }}条)</h3>
    {% commentList comments with archiveId=archiveId type="page" limit="10" %}
        {% for item in comments %}
            <div class="comment-item {% if item.Status != 1 %}comment-pending{% endif %}">
                <div class="comment-header">
                    <span class="comment-username">
                        {% if item.Status != 1 %}
                            <i class="icon-pending"></i> 审核中用户:{{item.UserName|truncatechars:6}}
                        {% else %}
                            {{item.UserName}}
                        {% endif %}
                    </span>
                    {% if item.Parent %}
                        <span class="reply-indicator">回复</span>
                        <span class="comment-parent-username">
                            {% if item.Parent.Status != 1 %}
                                审核中用户:{{item.Parent.UserName|truncatechars:6}}
                            {% else %}
                                {{item.Parent.UserName}}
                            {% endif %}
                        </span>
                    {% endif %}
                    <span class="comment-time">{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}</span>
                </div>
                <div class="comment-content">
                    {% if item.Status != 1 %}
                        <blockquote class="pending-message">该评论正在审核中,请耐心等待。</blockquote>
                        {# 如果您希望在审核中也显示部分内容,可以这样处理: #}
                        {# <p class="pending-content">{{item.Content|truncatechars:30}}...</p> #}
                    {% else %}
                        {% if item.Parent %}
                            <blockquote class="reply-quote">
                                {% if item.Parent.Status != 1 %}
                                    <span class="text-muted">原评论正在审核中...</span>
                                {% else %}
                                    {{item.Parent.Content|truncatechars:100}}
                                {% endif %}
                            </blockquote>
                        {% endif %}
                        <p>{{item.Content}}</p>
                    {% endif %}
                </div>
                <div class="comment-actions" data-id="{{item.Id}}" data-user="{{item.UserName}}">
                    {% if item.Status == 1 %}
                        <a class="action-btn praise-btn" data-id="praise">赞(<span class="vote-count">{{item.VoteCount}}</span>)</a>
                        <a class="action-btn reply-btn" data-id="reply">回复</a>
                    {% endif %}
                </div>
            </div>
        {% empty %}
            <p>目前还没有评论,快来抢沙发吧!</p>
        {% endfor %}

        {# 评论分页 #}
        {% pagination pages with show="5" %}
            <div class="pagination-nav">
                <a class="page-link {% if pages.FirstPage.IsCurrent %}active{% endif %}" href="{{pages.FirstPage.Link}}">首页</a>
                {% if pages.PrevPage %}<a class="page-link" href="{{pages.PrevPage.Link}}">上一页</a>{% endif %}
                {% for pageItem in pages.Pages %}
                    <a class="page-link {% if pageItem.IsCurrent %}active{% endif %}" href="{{pageItem.Link}}">{{pageItem.Name}}</a>
                {% endfor %}
                {% if pages.NextPage %}<a class="page-link" href="{{pages.NextPage.Link}}">下一页</a>{% endif %}
                <a class="page-link {% if pages.LastPage.IsCurrent %}active{% endif %}" href="{{pages.LastPage.Link}}">尾页</a>
            </div>
        {% endpagination %}

    {% endcommentList %}
</div>

In the above code, we useditem.ParentTo determine whether the current comment is a reply to another comment, and to display a portion of the parent comment in a block quote format. At the same time,stampToDateThe filter helps us format timestamps into readable date and time.

Comment submission form with captcha

In order for users to easily submit comments on the front end, you also need to integrate a comment submission form. AnQiCMS provides a unified comment submission interface/comment/publish. The form must includearchive_id(Document ID of the comment),user_name/contentand other basic information. If the comment is a reply to a specific comment, additional information is also required.parent_id.

To effectively prevent malicious flooding or robot comments, it is strongly recommended that you add a captcha function to the comment form.AnQiCMS has built-in captcha support, you just need to enable the relevant settings in the background and add the specific HTML structure and JavaScript code to the front-end form.

`twig

<h3>发表评论</h3>
<form method="post" action="/comment/publish">
    <input type="hidden" name="return" value="html"> {# 提交后返回html内容,或json #}
    <input type="hidden" name="archive_id" value="{% archiveDetail with name="Id" %}">
    <input type="hidden" name="parent_id" value="" id="comment-parent-id"> {# 用于回复评论时填充 #}

    <div class="form-group">
        <label for="comment-username">您的昵称</label>
        <input type="text" id="comment-username" name="user_name" required placeholder="请填写您的昵称" autocomplete="off">
    </div>

    <div class="form-group">
        <label for="comment-content">评论内容</label>
        <textarea id="comment-content" name="content" required placeholder="

Related articles

How to display a list of website friend links in the front-end template?

In website operation, friendship links are not only a bridge between websites, but also an important strategy to enhance website authority, increase external traffic, and optimize search engine rankings.Reasonably configure and display friendship links, which can effectively improve the website's SEO performance and provide users with more valuable external resources.AnQiCMS as an efficient content management system, fully considers this need, and provides a simple and intuitive way to manage and display friend links in the front-end template.### Backend Management: Setting and Maintaining Friend Links Display Friend Links on the Front-end Page

2025-11-07

How to extract the article summary and add an ellipsis in AnQiCMS template?

In website operation, how to make the article list page both beautiful and informative is a common concern.Generally, we do not display the full content of the article on the list page, but instead use a short 'abstract' to attract readers to click.To maintain the cleanliness and professionalism of the page, these summaries often need to be automatically truncated when exceeding a certain length and appended with an elegant ellipsis.AnQiCMS is a powerful content management system that provides flexible tools in the template to help us meet this need.Below, we will discuss in detail how to use AnQiCMS

2025-11-07

How to implement safe output of HTML tags in article content to avoid XSS attacks?

In AnQiCMS, we not only pursue efficient and flexible content management, but also regard website security as a core element.In daily content creation and template development, a seemingly simple HTML tag output actually hides potential security risks, the most common of which is cross-site scripting (XSS) attack.Understanding how AnQiCMS processes HTML output and mastering security practices is crucial for building a robust and reliable website.How does AnQiCMS handle HTML content output?In AnQiCMS template rendering mechanism

2025-11-07

How to determine if a variable is empty in AnQiCMS template and display default content?

In website content operation, data integrity and the elegance of display are crucial.We often encounter such situations: some fields of data may be missing for various reasons, such as an article may not have an illustration, a product may not have a detailed description, or a custom field may not be filled in.If the template code does not perform the corresponding null value judgment, it may result in blank pages on the page, affecting the aesthetics, or may even cause template rendering errors, affecting the user experience.AnQiCMS with its efficient architecture based on the Go language and similar to Django

2025-11-07

How to dynamically display the website's logo image in the template of AnQiCMS?

The website's logo is the core of the brand image, it not only enhances the professionalism of the website, but also facilitates users in quickly identifying and remembering your brand.For AnQiCMS users, dynamically displaying the logo image in the website template is a basic and important operation.Luckyly, AnQiCMS provides a very convenient way to achieve this, allowing you to easily manage and display the website logo without writing complex code.### Website Logo Management: Backend settings are crucial Display the Logo on your AnQiCMS website

2025-11-07

How to insert Markdown formatted text in website content and render it correctly as HTML?

In AnQiCMS, inserting Markdown formatted text and rendering it correctly to HTML is an important way for content operators to improve efficiency and produce high-quality pages.AnQiCMS has built-in support for Markdown editing and rendering, allowing you to enjoy the simplicity and efficiency of Markdown while ensuring that the final page output is beautiful and structured HTML.### Enable Markdown Editor To start using Markdown in AnQiCMS, you first need to ensure that the editor feature is enabled

2025-11-07

How to correctly display mathematical formulas and flowcharts in the template of AnQiCMS?

Today, with the increasing refinement of content management, websites not only carry text and images but also need to present complex information in a professional and understandable way, such as mathematical formulas and flowcharts.AnQiCMS is dedicated to providing efficient and flexible content management solutions. When we deal with technical articles, academic content, or business process diagrams on the website, how to ensure that these special elements are displayed correctly and beautifully becomes a topic worth discussing.AnQiCMS itself provides good support for Markdown editors, which means we can use concise

2025-11-07

Enhance interactive security: A practical guide to integrating captcha in the Anqi CMS frontend comments or messages

The website's comment section and message board are important channels for user interaction with the website.However, these interactive areas are often plagued by spam and automated programs, which not only affect the cleanliness of the website but also reduce the reading and communication experience of users.(CAPTCHA) is the effective barrier against such problems, it distinguishes between human users and malicious robots by requiring users to complete a task that is easy for humans but difficult for computers.For a website built with Anqi CMS, integrating captcha is not a complex matter.The Anqi CMS system provides a simple and clear path

2025-11-07