How to display the comment list of an article in AnQiCMS template, including multi-level replies and review status indicators?

Calendar 👁️ 66

In Anqi CMS, let your article page not only be a display of content, but also an active communication platform, and the comment feature is the bridge connecting content with readers.Understanding how to flexibly display a list of comments in an article template, including multi-level replies and clear review status indicators, is crucial for building interactive websites.

The foundation of the comment function: ensure the backend configuration is ready

Before delving into the template code, we first need to confirm that the comment feature has been enabled in the AnQi CMS backend.You can go to the "Function Management" menu, find the "Content Comment" option, and here you can make global settings for comments, such as whether to enable comments, whether to need to review, etc.Ensure these basic settings meet your website operation requirements, which is a prerequisite for the normal display of comments.

Introduce the comment list on the article detail page

Usually, we will add comments on the article detail page (like your article/detail.htmlOr a similar-named template file to display the comments of the current article. Anqi CMS provides concise and powerful template tagscommentListto help us achieve this.

To display the comment list, you need to know the unique identifier of the current article, which is the article ID. On the article detail page, this ID is usually accessible through{{ archive.Id }}Get it. Next, we can usecommentListtags to obtain comment data.

A basic comment list call might look like this:

{% commentList comments with archiveId=archive.Id type="list" limit="10" order="id desc" %}
    {% for item in comments %}
        <div>
            <!-- 这里将是每条评论的显示内容 -->
            <p>{{ item.UserName }} 说:</p>
            <p>{{ item.Content }}</p>
        </div>
    {% endfor %}
{% endcommentList %}

In the above example:

  • commentsIs the variable name we define for comment list data.
  • archiveId=archive.IdAssociate the comment with the current article.
  • type="list"Means we want to get a simple comment list, not the complete dataset for pagination (which will be mentioned later).
  • limit="10"The number of comments displayed per page is limited.
  • order="id desc"The latest comments are displayed at the top.

Elegantly handles multi-level replies.

Comments are often not one-way, users may have reply relationships between them. The Anqi CMS comment system naturally supports multi-level replies. IncommentListLabel returns the comment data, each comment item (we name it here asitem) will contain aParentIdfield indicates which comment it is replying to. What's more convenient is that it also providesParentThe object directly includes the complete data of the upper-level comment.

Using this information, we can build a comment display structure with a sense of hierarchy. A common practice is to check if comments haveParentAn object, if any, is displayed as a nested reply.

The following is an example structure containing multi-level reply logic:

<div class="comments-section">
    {% commentList comments with archiveId=archive.Id type="list" limit="10" order="id desc" %}
        {% for item in comments %}
            <div class="comment-item {% if item.Parent %}comment-reply{% endif %}">
                <div class="comment-meta">
                    <span class="comment-user">{{ item.UserName }}</span>
                    {% if item.Parent %}
                        <span class="comment-reply-to">回复 {{ item.Parent.UserName }}</span>
                    {% endif %}
                    <span class="comment-time">{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}</span>
                </div>
                <div class="comment-content">
                    {% if item.Parent and item.Parent.Status == 1 %}
                        <blockquote class="parent-quote">{{ item.Parent.Content|truncatechars:100 }}</blockquote>
                    {% endif %}
                    <p>{{ item.Content|safe }}</p> {# 使用 safe 过滤器确保富文本内容正确显示 #}
                </div>
                <div class="comment-actions" data-id="{{item.Id}}" data-user="{{item.UserName}}">
                    <a class="item praise-button">赞(<span class="vote-count">{{item.VoteCount}}</span>)</a>
                    <a class="item reply-button">回复</a>
                </div>
            </div>
        {% empty %}
            <p>目前还没有评论,快来发表第一条评论吧!</p>
        {% endfor %}
    {% endcommentList %}
</div>

In this structure, we use{% if item.Parent %}To determine whether it is a reply and add the corresponding style class (for examplecomment-reply) to distinguish. At the same time, we also refer toitem.Parent.UserNameto clarify the reply object and to proceed throughitem.Parent.ContentDisplaying part of the reply content, enhancing the clarity of the context.

Clear identification of the review status.

Content review is an indispensable part of website operation, and this also applies to the display of comments. The comment data of Anqi CMS contains aStatusfield, whose value is1indicating that the comment has passed the review,0It means that it is under review.

To provide users with a transparent experience, we can display comments according to theStatusfield to add review status hints.

In the above multi-level reply example, we can add such a judgment next to the comment content:

            <div class="comment-item {% if item.Parent %}comment-reply{% endif %}">
                <div class="comment-meta">
                    <span class="comment-user">
                        {% if item.Status != 1 %}
                            <span class="moderating-label">审核中:</span>{{ item.UserName|truncatechars:6 }}
                        {% else %}
                            {{ item.UserName }}
                        {% endif %}
                    </span>
                    {% if item.Parent %}
                        <span class="comment-reply-to">回复
                            {% if item.Parent.Status != 1 %}
                                <span class="moderating-label">审核中:</span>{{ 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.Parent and item.Parent.Status == 1 %}
                        <blockquote class="parent-quote">{{ item.Parent.Content|truncatechars:100 }}</blockquote>
                    {% elif item.Parent and item.Parent.Status != 1 %}
                        <blockquote class="parent-quote"><span class="moderating-label">上级评论正在审核中</span></blockquote>
                    {% endif %}
                    {% if item.Status != 1 %}
                        <p><span class="moderating-label">您的评论正在审核中,审核通过后将显示。</span></p>
                    {% else %}
                        <p>{{ item.Content|safe }}</p>
                    {% endif %}
                </div>
                <div class="comment-actions" data-id="{{item.Id}}" data-user="{{item.UserName}}">
                    <a class="item praise-button">赞(<span class="vote-count">{{item.VoteCount}}</span>)</a>
                    <a class="item reply-button">回复</a>
                </div>
            </div>

Here we added<span class="moderating-label">审核中:</span>and if the comment is in review status, display a prompt message instead of the actual content in the comment content section.This protects the website from displaying unreviewed content and also informs users of their comment status.

Introduce the comment submission form

In order for users to be able to post comments, you also need to add a comment submission form below the comment list or at a suitable location. This form usually POSTs data to/comment/publishinterface.

A simplified comment form may include these fields:

`twig

<input type="hidden" name="archive_id" value="{% archiveDetail with name="Id" %}">
<input type="hidden" name="parent_id" value="0" id="comment-parent-id"> {# 用于回复,初始为0 #}
<input type="hidden" name="return" value="html"> {# 可选择返回 html 或 json #}

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

<div class="form-group">
    <label for="comment_content">评论内容

Related articles

How to configure the pseudo-static URL rule in AnQiCMS to optimize the display path and SEO friendliness?

In website operation, the URL is not only the address of the page content, but also an important part of search engine understanding, user identification, and brand building.A clear and meaningful URL structure, known as "pseudo-static" or "staticized" URL, is crucial for improving the SEO performance and user experience of a website.AnQiCMS is a content management system that focuses on SEO-friendliness and provides flexible and diverse static URL configuration features, helping us optimize the display path of content better.

2025-11-07

How to perform simple arithmetic operations (+ - * /) and display the results in AnQiCMS template?

In AnQiCMS templates, we often need to display website content and data.But sometimes, we not only need to display stored data, but also need to process this data in some simple ways, such as calculating the total price of goods, statistics page loading progress percentage, or adding a certain number of values, etc.

2025-11-07

How to use the `default` filter to set a default display value for possibly empty variables?

In website content management, we often encounter a situation where a certain data variable may not have a value on some pages or under certain conditions, that is, "empty".If the template directly outputs these blank variables, ugly gaps will appear on the page, which not only affects the aesthetics but may also confuse the visitor.In order to avoid such embarrassment, AnQi CMS provides a very practical tool - the `default` filter, which can help us set a graceful default display value for these variables that may be empty.

2025-11-07

How to use the `cut` or `removetags` filter to remove specific characters or HTML tags from strings in the AnQiCMS template?

In AnQi CMS template design, in order to better control the display of content, the system provides a variety of flexible filters (Filter).These filters can help you perform various string processing operations when outputting variables, such as removing specific characters or HTML tags, making the content neater and meeting your display requirements.Today, we will focus on the practical filters `cut` and `removetags`.

2025-11-07

How to retrieve and display custom fields in the AnQiCMS template for the website message form and ensure correct submission?

In Anqi CMS, adding and managing custom fields for the website's message form can not only help us collect more specific and targeted customer information, but also significantly enhance the interactivity and data collection efficiency of the website.AnQi CMS with its flexible content model and easy extensibility makes this process very convenient.This article will guide you on how to obtain and display custom fields in the Anqi CMS template and ensure that these fields are submitted correctly.### Step 1: Define the custom message field in the Anqi CMS backend First

2025-11-07

How does AnQiCMS implement anti-crawling interference code through built-in features to protect the display of original content on the front end?

Today, as digital content becomes a core asset, how to effectively protect original content from malicious collection and misuse has become a common challenge for website operators and content creators.Hardly created articles, product descriptions, in a moment may appear on other websites, not only dilute the value of original content, but may also have a negative impact on the search engine rankings of websites.AnQiCMS (AnQiCMS) is fully aware of this pain point and therefore built-in anti-crawling interference code function at the initial stage of system design, aiming to build a clever and sturdy barrier for the display of original content on the front end.

2025-11-07

How to correctly render Markdown content in AnQiCMS and support the display of mathematical formulas and flow charts?

In modern content management, providing rich and structured content has become the key to improving user experience and information delivery efficiency.Especially for technical blogs, product documents, or data analysis reports, the ability to visually display code, mathematical formulas, and complex flowcharts undoubtedly makes the content more attractive and readable.AnQiCMS as an efficient and customizable enterprise-level content management system fully understands this requirement and provides comprehensive Markdown support, allowing you to easily achieve these advanced content displays.

2025-11-07

How to insert custom structured data using `Ld` tags on AnQiCMS pages to enhance search engine display effects?

When building and optimizing a website, we always hope to allow search engines to deeply understand the content of our pages so that we can achieve better display effects in search results.Structured data, especially structured data presented in JSON-LD format, is an important tool to achieve this goal.AnQiCMS (AnQiCMS) is a content management system that focuses on SEO optimization and provides flexible and powerful support for inserting custom JSON-LD.

2025-11-07