How to embed a comment list on the page and control its pagination display?

Calendar 👁️ 68

In website operation, user interaction is the key to enhancing site activity and content value.AnQiCMS provides a set of intuitive and powerful features, helping us easily embed comment functions in web pages, and flexibly control the display of comment lists, especially its pagination management, which is crucial for web pages with a large number of user comments.

Enable the comment feature to make the content come alive

Firstly, to allow users to post comments, we need to ensure that the backend comment function is enabled.Once the comment feature is activated, we can integrate the comment list into the page template where we want to display comments.Generally, comment lists appear at the bottom of content pages such as article detail pages, product detail pages, and so on.

In the AnQiCMS template system, the display of the comment list mainly depends oncommentListThe tag allows us to flexibly call and display comment data according to different needs.For example, to display comments on a document (such as an article or product) detail page, we usually need to know the unique identifier ID of the current document.Assuming our document detail page has passedarchiveDetailthe tag obtained the document information, then the document ID can be accessed througharchive.IdGet it.

A basic comment list code structure may look like this:

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
    <div>
        <span>{{item.UserName}}</span>
        <span>{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}</span>
        {% if item.Parent %}
            <p>回复 {{item.Parent.UserName}}:</p>
            <blockquote>{{item.Parent.Content}}</blockquote>
        {% endif %}
        <p>{{item.Content}}</p>
        {% if item.Status != 1 %}
            <p style="color: gray;">评论待审核</p>
        {% endif %}
        {# 这里还可以添加点赞、回复等操作按钮 #}
    </div>
    {% else %}
    <p>暂无评论,快来发表您的看法吧!</p>
    {% endfor %}
{% endcommentList %}

In the code above,commentListTag througharchiveId=archive.IdSpecifies which document's comments to retrieve.type="list"Means displayed in list form,limit="6"It limited the number of comments displayed per page. We traversedcommentsobjects, displaying each comment author's username, posting time, and comment content. Particularly, through the judgmentitem.ParentWhether it exists, we can elegantly handle the display of reply comments, as well asitem.Statusdetermine whether the comment has passed the review.

Of course, having a comment list is essential for the form where users post comments. Usually, we create a form that submits to the preset comment release interface of AnQiCMS./comment/publish. The form must includearchive_id(Document ID),user_name(nickname of the commenter) andcontent(comment content) and other fields, according to actual needs, you can also addparent_idused to reply to a specific comment.

To implement pagination of the comment list, optimize the user experience

When the number of comments on the page increases gradually, without pagination functionality, the page will become very long, load slowly, and users will also find it difficult to browse historical comments.AnQiCMS provides a simple way to add pagination control to the comment list, thereby significantly improving user experience and page performance.

To enable pagination for the comment list, we need tocommentListput in the tag.typethe parameter to"page"Instead of the default,"list". For example:

{% commentList comments with archiveId=archive.Id type="page" limit="10" %}
    {# ... 评论列表内容,与上方示例类似 ... #}
{% endcommentList %}

Herelimit="10"It is no longer just a simple limit on the number of displayed comments, but specifies the number of comments per page.

ThencommentListtags, we need to introducepaginationtags to generate pagination navigation.paginationThe label will read the previoustype="page"list label (in this case iscommentList) generates pagination data, and according to this renders page number links.

This is a complete example that integrates a comment list and pagination navigation:

”`twig {# Get the current document ID to ensure that comments are associated with the correct document #} {% archiveDetail archiveInfo with name=“Id” %}

User comments (Total {{count}} comments)

{% commentList comments with archiveId=archiveInfo type="page" limit="5" %} {% for item in comments %}

{{item.UserName}}YU{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}Publish

{% if item.Parent %}

Reply{{item.Parent.UserName}}:

{{item.Parent.Content}}
{% endif %}
{% if item.Status == 1 %} {# Only show reviewed comment content #}

{{item.Content}}

{% else %}

This comment is waiting for review.

{% endif %}
{# You can add like, reply buttons here, implemented through JavaScript #}
{% empty %}

No comments yet, looking forward to your insightful views!

{% endfor %} {% endcommentList %}
{# 评论分页导航 #}
<div class="pagination-nav">
    {% pagination pages with show="5" %}
        <ul>
            {# 首页链接 #}
            <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>

{# 评论提交表单 #}
<div class="comment-form">
    <h4>发表评论</h4>
    <form method="post" action="/comment/publish">
        <input type="hidden" name="archive_id" value="{% archiveDetail with name="Id" %}">
        <input type="hidden" name="return" value="html"> {# 可选,指定后端返回HTML或JSON #}
        {# 这里可以添加 CAPTCHA 验证码的隐藏字段和图片刷新JS,参考

Related articles

How to enable Markdown rendering and correctly display mathematical formulas and flowcharts in AnQiCMS?

In AnQiCMS, if you want to make your content more expressive, especially when it comes to displaying complex mathematical formulas or clear flowcharts, it is a very effective solution to flexibly use the Markdown editor.The new version of AnQiCMS has provided good support for this.Next, we will explore together how to enable these features in AnQiCMS, making your website content come alive.

2025-11-09

How to display additional field data in the front-end template after customizing the content model?

AnQiCMS with its flexible content model function has brought great convenience to us in managing various types of website content.Whether it is an article, product, event, or any other content type that requires special fields, we can easily customize fields in the background to meet unique business needs.But when we eagerly create new fields in the background and fill them with data, new problems also arise: How can we accurately display these newly added custom field data on the front page of the website for visitors?Don't worry, AnQiCMS

2025-11-09

How to implement the function of displaying and switching content in multiple languages?

Make your website go global: Anqi CMS multilingual content strategy and implementation In today's globalized digital age, many businesses and content creators hope that their websites can reach a wider audience.This means providing content display and switching functions in multiple languages to meet the reading habits of users in different countries and regions.AnQiCMS (AnQiCMS) is an efficient content management system that fully considers this requirement, providing a flexible solution to help you easily implement the multilingual function of the website.In AnQi CMS, implement multi-language display and switching of content

2025-11-09

How to retrieve and display the SEO title and description of the current document in the template?

In website operation, Search Engine Optimization (SEO) is a key element to enhance website visibility.And the page title (Title) and description (Description) as the first information seen by search engines and users browsing, its importance is self-evident.AnQiCMS (AnQiCMS) knows this and therefore provides a flexible and powerful mechanism to retrieve and display these SEO elements in the template.### AnQi CMS's TDK (Title, Description,

2025-11-09

How to loop through and display all subcategories under a specified category in a template?

How to elegantly display all subcategories under a specified category in Anqi CMS template?For a content-rich website, a clear classification structure is the key to improving user experience and search engine friendliness.AnQiCMS (AnQiCMS) takes advantage of its flexible template engine and powerful tag system, allowing you to easily traverse and display all subcategories under a specified category on the website front end, whether it is a direct subcategory or a multi-level nested subcategory, you can handle it with ease.Today, let's discuss how to implement this feature in Anqi CMS templates.

2025-11-09

How to display a second-level dropdown menu in the navigation menu and associate different content?

AnQi CMS provides a直观and powerful navigation menu management system, allowing you to easily build a clear, user-friendly navigation structure for your website, including multi-level dropdown menus.This not only improves the user experience of the website, but also helps search engines better understand the hierarchy of your website content, thus optimizing SEO performance. ### Understanding the navigation system of Anqi CMS In Anqi CMS, the settings and management of the navigation menu are concentrated in the "Website Navigation Settings" function in the background.This is the core area where you build your website menu

2025-11-09

How to specify an independent template file for content display for a specific category or single page?

In a content management system, we often need to present certain specific content of the website in a unique way, which not only meets the specific needs of branding or marketing, but also brings an optimized browsing experience for visitors.Our CMS is well-versed in this, providing flexible template customization features, allowing us to easily specify a unique template file for a category page, or even a single content page.Why do you need to customize the template for a specific page?

2025-11-09

How to use Tag tags to display related article lists on the page?

In website content operations, how to efficiently organize content, improve user experience, and optimize search engine rankings is a key focus for every operator.AnQiCMS as an enterprise-level content management system provides powerful and flexible functions to meet these needs, among which the Tag label function is an indispensable part.Tag label, simply put, is a keyword or thematic word, which can link scattered article content on a website according to common themes or focus points.Reasonably utilizing Tag tags can not only make it easier for users to discover content of interest

2025-11-09