In website operation, user comments are an important element for building a community, obtaining feedback, and enhancing content interaction.For friends using AnQiCMS (AnQiCMS) to build and manage websites, efficiently displaying user comments and managing their review status is a key factor in ensuring the quality of website content and user experience.The AnQiCMS provides very flexible and practical features for this.
How to display a user comment list on a page
To display user-submitted comments on your website, you will use the powerful template tag system of AnQiCMS, especiallycommentListThe tag can help us easily retrieve comment data from the database and present it on the front page in the way we want.
Comments are usually associated with specific articles or products, and therefore, when usingcommentListWhen labeling, we need to specify the content ID of the comment. For example, if you want to display comments below the details page of an article, you can usearchiveIdParameters are used to pass the current article ID. This article ID can be obtained througharchiveDetailtags easily, such asarchive.Id.
You can also pass throughtypeParameters to control the display of the comment list. When you only need to retrieve a certain number of the latest comments (such as for the sidebar display), you cantypeis set to"list"And thenlimitParameter limits the number of displays. If it is in the comment area, it requires a complete comment list and pagination feature, thentypeis set to"page".
Here is a basic comment list code example, which retrieves the latest 5 comments of the current article:
{# 假设我们正在一个文章详情页,archive.Id代表当前文章的ID #}
{% commentList comments with archiveId=archive.Id type="list" limit="5" %}
{% for item in comments %}
<div>
<strong>{{ item.UserName }}</strong> 发表于 {{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}
<p>{{ item.Content }}</p>
</div>
{% empty %}
<p>目前还没有评论。</p>
{% endfor %}
{% endcommentList %}
In this code block,item.UserNameDisplay the nickname of the commentor,item.Contentwhich is the specific content of the comment,item.CreatedTimeIt is the timestamp of the comment submission. To make the time display more friendly, we usedstampToDatea filter to format it into a common date and time format.{% empty %}Tags are displayed as a prompt when there are no comments.
Review status of comments processing
After the user submits a comment, there is usually a review process. AnQiCMS has built-in comment review mechanisms, each comment has aStatusfield to indicate its review status. Usually,Status = 1Indicates that the comment has been approved and can be publicly displayed, whileStatus = 0means that the comment is being reviewed or has not been approved.
When displaying the comment list on the front end, we should judge according toStatusThe field determines how to display.For comments under review, we can choose not to display the full content or show a "Under review" prompt to ensure the compliance of the website content.
Integrate the review status logic into the comment list, for example:
`twig {% commentList comments with archiveId=archive.Id type="list" limit="5" %}
{% for item in comments %}
<div class="comment-item">
<div class="comment-meta">
<strong>
{% if item.Status == 1 %}
{{ item.UserName }}
{% else %}
{# 评论审核中,只显示部分用户名或匿名处理 #}
审核中:{{ item.UserName|truncatechars:6 }}
{% endif %}
</strong> 发表于 {{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}
</div>
{# 处理回复评论的显示 #}
{% if item.Parent %}
<blockquote class="reply-blockquote">
{% if item.Parent.Status == 1 %}
回复 <strong>{{ item.Parent.UserName }}</strong>: {{ item.Parent.Content|truncatechars:100 }}
{% else %}
回复 <strong>审核中用户</strong>: <i>该回复正在审核中。</i>
{% endif %}