How to determine if a comment in `commentList` has a parent comment (`Parent`) and display the reply structure?

Calendar 👁️ 65

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

{# 使用 commentList 标签获取评论数据 #}
{% commentList comments with archiveId=archive.Id type="list" limit="10" %}
    {% for item in comments %}
    {# 为每条评论添加一个基础容器,并根据是否有父级评论添加样式类 #}
    <div class="comment-item {% if item.Parent %}comment-reply{% endif %}">
      <div class="comment-header">
        <span class="comment-username">
          {# 判断评论是否通过审核,如果未通过则显示“审核中” #}
          {% if item.Status != 1 %}
          (审核中){{item.UserName|truncatechars:6}}
          {% else %}
          {{item.UserName}}
          {% endif %}
        </span>

        {# 如果存在父级评论(即当前评论是回复) #}
        {% if item.Parent %}
        <span class="reply-indicator">回复</span>
        <span class="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.Parent %}
        <blockquote class="parent-comment-quote">
          {# 判断父级评论内容是否通过审核 #}
          {% if item.Parent.Status != 1 %}
          原评论:<span class="moderating-text">(内容审核中){{item.Parent.Content|truncatechars:60}}</span>
          {% else %}
          原评论:{{item.Parent.Content|truncatechars:100}}
          {% endif %}
        </blockquote>
        {% endif %}

        {# 显示当前评论的内容,同样判断是否通过审核 #}
        {% if item.Status != 1 %}
          <span class="moderating-text">(评论审核中){{item.Content|truncatechars:60}}</span>
        {% else %}
        {{item.Content}}
        {% endif %}
      </div>

      {# 评论操作区域,如点赞、回复等 #}
      <div class="comment-actions" data-id="{{item.Id}}" data-user="{{item.UserName}}">
        <a class="action-item" data-action="praise">赞(<span class="vote-count">{{item.VoteCount}}</span>)</a>
        <a class="action-item" data-action="reply">回复</a>
      </div>
    </div>
    {% end

Related articles

How to judge whether the comment (`Status`) in the AnQiCMS template has passed the review and be displayed differently?

As an experienced website operation expert, I know the importance of the comment system for website user interaction and content ecology.In such a powerful content management system as AnQiCMS, flexibly controlling the display of comments, especially the differentiated display based on the review status, is a key factor in improving user experience and maintaining the quality of website content.Today, let's delve into how to use the `commentList` tag in the AnQiCMS template to judge the `Status` field of comments and implement intelligent differentiated display.

2025-11-06

How to conditionally display the 'Previous' and 'Next' buttons in the `pagination`?

As an experienced website operation expert, I am willing to analyze the conditional display techniques of the pagination navigation buttons in AnQi CMS in detail.AnQi CMS is known for its efficiency and flexibility, its template engine provides powerful functions, allowing us to easily achieve both beautiful and practical page interactions.In website content management, pagination is an indispensable element, and how to intelligently display the "Previous Page" and "Next Page" buttons is directly related to the smoothness of the user's browsing experience.### SecureCMS

2025-11-06

In the `pagination` tag, how to determine if the current page is the first or last page to control the style?

As an experienced website operations expert, I know the importance of a powerful and flexible content management system for the success of a website.AnQiCMS leverages the efficiency of the Go language and its deep consideration for content operation, providing us with many conveniences.Today, let's delve into the wonderful use of the `pagination` tag in AnQiCMS, especially how to accurately judge whether the current page is the first page or the last page, thereby flexibly controlling the style to create a smoother and more intuitive navigation experience for users.

2025-11-06

How to conditionally output the standard link according to the `CanonicalUrl` tag in AnQiCMS templates?

Hello! As an experienced website operations expert, I fully understand that in daily work, we not only need to pay attention to the quality of the website content, but also to the impact of technical details on SEO.The canonical URL is one of the crucial details that can effectively solve the problem of duplicate website content, concentrate page authority, and thus improve search engine rankings.Today, let's delve deeply into how to intelligently and elegantly output standard links based on the existence or absence of the `CanonicalUrl` field within the `tdk` tag in AnQiCMS templates

2025-11-06

How to dynamically add a required asterisk or hint based on the `Required` property of the `guestbook` form label?

As an experienced website operation expert, I know that every detail can affect user experience and operation efficiency.When building a website, forms are an important part of interacting with users, and clear, friendly form design is the key to improving conversion rates.AnQiCMS (AnQiCMS) with its flexible template engine and powerful backend management features, has provided us with great convenience, especially in handling scenarios such as message forms that require users to fill in information.Today, let's delve deeply into a seemingly simple but extremely practical skill: how to use the `Required` field of the back-end field

2025-11-06

How to render different input controls (such as text, radio, checkbox) in the `guestbook` form based on the `Type` attribute?

As an experienced website operations expert, I know that a flexible and versatile content management system is the foundation of website success.AnQiCMS (AnQiCMS) with its excellent flexibility and powerful customization capabilities has become an invaluable tool in our hands.Today, let's delve deeply into a very practical topic in actual operation: how to intelligently render different input controls, such as text boxes, radio buttons, or checkboxes, in the `guestbook` form of Anqi CMS according to the preset field types in the background.This is not just an implementation at the technical level

2025-11-06

How to determine if a custom field exists or has a value in `archiveParams` before displaying it?

As an experienced website operation expert, I deeply know the powerful potential of AnQiCMS (AnQi CMS) in content management and website optimization.The flexible content model and customizable fields provide great convenience for us to build highly personalized websites.However, how to elegantly handle these dynamically generated custom fields in template design, ensuring that they are displayed only when they exist or have a value, is the key to improving template quality and user experience.

2025-11-06

How to use the `IsCurrent` property to mark the currently selected filter condition in the `archiveFilters` filter tag?

As an experienced website operation expert, I deeply understand the critical role of user experience (UX) in the success of a website.A user-friendly, responsive interface can effectively guide users to discover content, and the content filtering feature is an important aspect of improving user experience.In AnQiCMS (AnQiCMS) this efficient and customizable content management system, the `archiveFilters` tag provides strong support for building flexible filtering functions, and its internal `IsCurrent` property is the 'magic wand' that lights up the user experience. Today

2025-11-06