How to implement pagination display and like function for comment content in AnQiCMS?

Calendar 👁️ 75

In website operation, user comments and interaction are the key to enhancing the vitality of content.AnQiCMS (AnQiCMS) provides powerful content management capabilities, among which the comment function is an important bridge for user interaction with content.This article will discuss in detail how to implement pagination for the comment list in Anqi CMS, as well as add a like function for comment content, to enhance the interactive experience of your website.

AnQiCMS comment feature overview

AnQi CMS has been built-in with article comment and comment management functions since its early version, which provides the website with native user interaction support.The system allows users to post comments and supports administrators to review and manage them in the background, ensuring the quality and compliance of the comment content.We need to use the flexible template tags provided by Anqicms to display these comments.

Implement comment list pagination display

When the number of comments on an article is high, loading all comments at once not only affects the page loading speed but also reduces the user experience.The Anqi CMS provides a pagination feature for the comment list, allowing you to load comments as needed to keep the page neat and efficient.

To implement comment list pagination display, we mainly use two key template tags:commentListused to retrieve comment data, as well aspaginationused to generate pagination navigation.

  1. UsecommentListTag to get comment data commentListTags are used to retrieve comment data from the database. To implement pagination, we need to specify the number of comments to display per page.typethe parameter to"page",and specify the number of comments to display per page.limit。“Furthermore,”archiveIdThe parameter is mandatory, it indicates which article's comments to retrieve. Usually, you will use this feature on the article detail page, at this timearchive.Idthe current article's ID will be provided automatically.

    Here is a basiccommentListTag usage example, it will retrieve the comments of the current article and display 10 comments per page:

    {% commentList comments with archiveId=archive.Id type="page" limit="10" %}
        {# 遍历评论列表 #}
        {% for item in comments %}
            <div>
                {# 显示评论用户名,如果未审核则提示 #}
                <span>
                    {% if item.Status != 1 %}
                    审核中:{{item.UserName|truncatechars:6}}
                    {% else %}
                    {{item.UserName}}
                    {% endif %}
                </span>
                {# 如果是回复,显示回复对象 #}
                {% if item.Parent %}
                <span>回复</span>
                <span>
                    {% if item.Parent.Status != 1 %}
                    审核中:{{item.Parent.UserName|truncatechars:6}}
                    {% else %}
                    {{item.Parent.UserName}}
                    {% endif %}
                </span>
                {% endif %}
                {# 显示评论时间 #}
                <span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
            </div>
            <div>
                {# 显示评论内容,如果未审核则提示 #}
                {% if item.Parent %}
                <blockquote>
                    {% if item.Parent.Status != 1 %}
                    该内容正在审核中:{{item.Parent.Content|truncatechars:9}}
                    {% else %}
                    {{item.Parent.Content|truncatechars:100}}
                    {% endif %}
                </blockquote>
                {% endif %}
                {% if item.Status != 1 %}
                    该内容正在审核中:{{item.Content|truncatechars:9}}
                {% else %}
                {{item.Content}}
                {% endif %}
            </div>
            {# 此处可以放置点赞和回复按钮,下文将详细讲解点赞 #}
            <div class="comment-control" data-id="{{item.Id}}" data-user="{{item.UserName}}">
                <a class="item" data-id="praise">赞(<span class="vote-count">{{item.VoteCount}}</span>)</a>
                <a class="item" data-id="reply">回复</a>
            </div>
        {% endfor %}
    {% endcommentList %}
    

    In the above code:

    • archive.IdIs the ID of the current article, making sure that the comments are associated with the article.
    • type="page"Informs the system that we need paginated data.
    • limit="10"Set to display 10 comments per page.
    • item.Status != 1Used to determine whether a comment has passed the review, unreviewed comments can be displayed as "Under review".
    • item.ParentUsed to display the parent comment information of a reply comment, forming a nested comment structure.
    • stampToDateIs a convenient time formatting function.
  2. UsepaginationThe tag generates pagination navigationAfter obtaining the paginated comment data, we still need a pagination navigation so that users can jump to different comment pages.paginationwith the tag andcommentListUsing the tag in conjunction, it can automatically generate pagination links that match the current page status.

    IncommentListlabel's{% endcommentList %}After that, you can addpaginationTags:

    <div>
        {% pagination pages with show="5" %}
            <ul>
                <li>总数:{{pages.TotalItems}}条,总共:{{pages.TotalPages}}页,当前第{{pages.CurrentPage}}页</li>
                <li class="{% if pages.FirstPage.IsCurrent %}active{% endif %}"><a href="{{pages.FirstPage.Link}}">{{pages.FirstPage.Name}}</a></li>
                {% if pages.PrevPage %}
                    <li><a href="{{pages.PrevPage.Link}}">{{pages.PrevPage.Name}}</a></li>
                {% endif %}
                {% for item in pages.Pages %}
                    <li class="{% if item.IsCurrent %}active{% endif %}"><a href="{{item.Link}}">{{item.Name}}</a></li>
                {% endfor %}
                {% if pages.NextPage %}
                    <li><a href="{{pages.NextPage.Link}}">{{pages.NextPage.Name}}</a></li>
                {% endif %}
                <li class="{% if pages.LastPage.IsCurrent %}active{% endif %}"><a href="{{pages.LastPage.Link}}">{{pages.LastPage.Name}}</a></li>
            </ul>
        {% endpagination %}
    </div>
    

    here,pagesIspaginationthe pagination information object provided by the tag,show="5"Indicate that up to 5 page number buttons are displayed. You can adjust the display style and content of pagination according to your design requirements.

Implement the like function for comment content.

Adding a like feature to comments is an effective way to enhance user interaction and encourage high-quality comments.The like function of AnQi CMS is implemented through asynchronous requests (AJAX) sent by the front-end JavaScript to interact with the backend.

  1. HTML structure for liking commentsIn the display area of each comment, we need an clickable element (such as<a>tag), and include the number of likes displayed<span>. The key is to add a like element.data-idattribute, storing the unique ID of the current comment so that you know which comment was liked when clicked.

    <div class="comment-control" data-id="{{item.Id}}" data-user="{{item.UserName}}">
        <a class="item vote-comment" data-comment-id="{{item.Id}}">赞(<span class="vote-count">{{item.VoteCount}}</span>)</a>
        <a class="item" data-id="reply">回复</a>
    </div>
    

    Here, we add likes for the<a>Tag addedclass="vote-comment"anddata-comment-id="{{item.Id}}"and a like count display<span>Itsclass="vote-count"will be used for subsequent JavaScript updates

  2. JavaScript implementation for comment likingThe like function needs to listen for click events with JavaScript and send a POST request to the backend. The Anqi CMS comment like interface is/comment/praiseIt needs to accept a parameter namedidwhich is the ID of the liked comment. The backend will handle the like logic and return the latest number of likes.

    Here is an example of a JavaScript code snippet using jQuery to implement a like function

    `javascript $(document).ready(function() {

    $(".vote-comment").on("click", function (e) {
        e.preventDefault(); // 阻止默认的链接跳转行为
        let that = $(this);
        let commentId = that.data("comment-id"); // 获取评论ID
    
        // 发送点赞请求
        $.ajax({
            url: "/comment/praise",
            method: "POST", // 点赞通常是POST请求
            data: { id: commentId }, // 传递评论ID
            dataType: "json", // 期望后端返回JSON数据
            success: function (res) {
                if (res.code === 0) { // 假设 res.code == 0 表示成功
                    // 点赞成功,更新页面上的点赞数量
                    that.find(".vote-count").text(res.data.vote_count);
                    // 可以选择性地禁用点赞按钮,防止重复点赞
                    // that.addClass("liked").off("click");
                    alert(res.msg || "点赞成功!");
                } else {
                    // 点赞失败,显示错误信息
                    alert(res.msg ||
    

Related articles

How to enable anti-crawling interference code and image watermark in AnQiCMS to protect the display of content?

In today's era of increasingly rich digital content, protecting original content on websites is particularly important.AnQiCMS (AnQiCMS) understands the pain points of content creators and corporate users, and has specifically built anti-crawling interference codes and image watermarking functions to help users effectively prevent their content from being maliciously scraped and stolen, while also strengthening brand identity.### Protect content security: Enable anti-capture interference code The value of original content is self-evident, but information collection tools on the Internet may copy your articles in large quantities without permission, which not only harms the rights and interests of the original creators but may also divert website traffic

2025-11-08

How to safely parse a URL string into a clickable a tag in AnQiCMS template?

In website content operation, we often need to display some website URLs, email addresses, and other information in articles, introductions, or custom fields.If this information is just plain text, users cannot click to jump directly, which not only affects the user experience but may also reduce the interactivity of the content and the SEO friendliness of the website.Convert these URL strings securely and intelligently into clickable `<a>` tags, which is a very practical feature in the AnQiCMS template.AnQiCMS as an enterprise-level content management system has fully considered the flexibility and security of content display from the beginning of its design

2025-11-08

How to implement the filtering function of the article list in AnQiCMS, displaying different results according to custom parameters?

How to make it easier for users to find the content they need on the website is the key to improving user experience and conversion rates.The filtering function of the article list is an important means to achieve this goal.AnQiCMS provides a flexible and powerful mechanism that allows us to easily filter and display different list results of articles based on custom parameters, which is very helpful for building personalized and efficient content platforms.### The Foundation of Building a Filtering Function: Custom Content Model To implement an article list filter based on custom parameters

2025-11-08

How to use a filter in AnQiCMS template to convert a timestamp to a readable date format?

In website operation, we often need to display various time information on the front-end page, such as the publication date of articles, content update time, or user comment time.AnQiCMS as an enterprise-level content management system usually adopts the efficient and concise format of Unix timestamps for data storage.However, for users visiting the website, a string of numeric timestamps is obviously not intuitive and friendly.How can I convert these timestamps into a readable date format in AnQiCMS templates?AnQiCMS provides a very practical built-in tag and filter

2025-11-08

How to debug variable content and type in AnQiCMS template (using dump filter)?

When using AnQiCMS to build websites and customize templates, we often encounter situations where the content displayed on the page is not as expected, or it seems that a variable is not passing correctly.At this moment, it is particularly important to be able to quickly view the actual content and data type of variables in the template.Fortunately, AnQiCMS's template engine (which adopts a syntax similar to Django) provides us with a very practical tool—the `dump` filter, which helps us easily reveal the mysteries of variables.### Core Function: `dump`

2025-11-08

How to call data from other sites in a multi-site environment using the siteId parameter?

In the multi-site environment of AnQiCMS, data intercommunication is a key factor in improving operational efficiency and achieving content integration.Sometimes, we may need to display the content of a sub-site on the main site, or call the contact information of another brand site in a business site.At this time, the `siteId` parameter provided by AnQiCMS is particularly important, as it helps us easily implement cross-site data calls and display.### Understanding the multi-site advantages of AnQiCMS AnQiCMS was initially designed with the need for multi-site management in mind

2025-11-08

How to traverse an array and control display style with loop tags (for) in AnQiCMS templates?

During website operation, we often need to display various list data, such as article lists, product lists, image galleries, or navigation menus.AnQiCMS (AnQiCMS) provides a powerful mechanism based on the Go language template engine, where the `for` loop tag is the core tool to meet this requirement.By flexibly using the `for` tag and its auxiliary functions, we can easily traverse array or list data and accurately control the display style of each element according to actual needs.

2025-11-08

How to set up a canonical link (CanonicalUrl) in article content and ensure it displays correctly on the page?

In website operation, we often encounter situations where content is similar or repetitive, which may not only scatter the search engine's crawling budget, but also dilute the page weight, affecting SEO performance.Fortunately, AnQi CMS provides us with the function to set canonical links (Canonical URL), which can effectively help us solve these problems and clearly inform search engines which page is the 'original' or 'preferred' version of the content.The value of understanding the significance of canonical URL in simple terms

2025-11-08