As an experienced website operations expert, I am well aware of how important an active and easy-to-read comment section is for increasing user engagement and website stickiness.In AnQiCMS, although comment data may be stored in a flat structure in the database, we can still skillfully build a clear hierarchical and parent-child comment association multi-level reply display effect on the front end through its powerful template tag function.This not only makes it easier for users to understand the context of the conversation, but also greatly improves the overall interactive experience.
This article will deeply explore how to use the comment list tag in AnQiCMScommentList) and its returned data structure, elegantly display multi-level replies, making your comment section come alive.
Understand the core of AnQiCMS's comment mechanism
AnQiCMS as an efficient and flexible content management system, understands the importance of user interaction.It provides a comprehensive comment function, where users can post comments below articles, products, and other content.commentListTags are our core tools.
This tag can retrieve the comment list of a specified document and provides rich parameters to control the filtering, sorting, and display of comments. The most critical part for implementing multi-level replies is that each comment item it returns (itemIt includesParentIdandParentThese fields:
ParentId: Indicates the parent comment ID of the current comment. If it is 0, it means this is a top-level comment; if it is greater than 0, it means this is a reply to a specific comment.ParentThis is a very powerful field, it directly contains the complete data object of the parent comment.By it, we can easily obtain the username, content, and other information of the parent comment, thereby constructing a clear reply relationship.
Combine loop traversal tags (for), we can then traverse all comments and judge and render the hierarchy of comments according toParentIdthe value of the field.
Build comment section template: Implement parent-child comment association display
To display multi-level replies on the front end, the core idea is to identify which are top-level comments and which are replies to other comments. We can use CSS indentation or different styles to distinguish them, as well as byParentThe field displays the information 'Reply to whom'.
We usually put the template (such as articles, product details) on the document detail page.archive/detail.html)commentListtags to obtain comment data.
First, we need to usecommentListtags to get the comment list. Suppose we are displaying a detailed article page, we can usearchive.IdGet the current article ID.To make the response look more natural, we can sort by comment ID in ascending order, so that the response will follow the parent comment immediately (if the parent and child comment IDs are consecutive).
`twig {# Assume this is on the document detail page, archive.Id is available #}