As an experienced website operation expert, I fully understand how to effectively display user comments in content management, especially comments with a reply structure, which is crucial for enhancing user interaction and website activity.AnQiCMS (AnQiCMS) leverages its powerful template engine and flexible data tags to make this requirement accessible.commentListIn the tag, cleverly judge whether the comment has a parent comment, and elegantly present a clear reply structure.
Cleverly utilize the AnQiCMS comment mechanism to build an interactive experience.
AnQiCMS is a modern content management system based on the Go programming language, which not only provides efficient and secure content publishing capabilities but also offers us comprehensive user interaction features, with the comment system being a major highlight.It not only allows users to comment on content, but also supports replies between comments, thus forming a natural conversation chain.commentListThe data structure returned by the label, especially the one inParentfield.
Firstly, we know that AnQiCMS's template system uses a syntax similar to Django's template engine, which means we can use{% ... %}to write logic, with{{ ... }}Output the variable. We usually use it for comment lists.commentListThis powerful tag.
In the document detail page, to get the comments related to the current document,commentListTags are our first choice. It acceptsarchiveId(the current document ID),type(list type, can belistorpage)、limitThe display number and other parameters. For example, on an article detail page, we would usually call the comment list in this way:
{% commentList comments with archiveId=archive.Id type="list" limit="10" %}
{# 循环遍历每一条评论 #}
{% for item in comments %}
{# 在这里处理单条评论的显示逻辑 #}
{% endfor %}
{% endcommentList %}
here,commentsThe variable will carry all the comment data that meets the conditions, anditemRepresents each independent comment in the loop.
ParentField: reveals the key to the relationship between comment replies
Every time we loop throughcommentsin the list ofitemAt that time, AnQiCMS provided several key fields to judge and display the hierarchical relationship of comments:
item.Id: The unique identifier of the current comment.item.UserName: The username of the commentor.item.Content: The specific content of the comment.item.CreatedTime: Comment posting timestamp.item.ParentId: If the current comment is a reply to another comment, this field will store the ID of the parent comment; if it is 0, then it means this is a top-level comment.item.ParentThis is the field we will focus on today! It is a complete parent comment object, containing all the information of the parent comment, such asitem.Parent.UserName/item.Parent.Content/item.Parent.CreatedTimeetc. Ifitem.ParentIf it exists (i.e., it is not empty), then the currentitemmust be a reply comment.
Exactlyitem.ParentThis field provides the most direct and convenient way for us to build the reply structure.
Build the comment reply structure: step by step implementation
Now that we have the theoretical foundation, we can start building an elegant structure for comment replies.The core idea is: when looping through each comment, first determine whether it is a reply comment.If so, the information of the parent comment will be displayed additionally, and it will be visually distinguished by style.
Let's see the specific template code example:
`twig