How to safely and effectively display user comment lists and message forms in a template?

Calendar 👁️ 85

In modern website operations, user-generated content (UGC) is an important component for enhancing website activity and user stickiness.User comments and online message functions not only promote community interaction, provide valuable feedback, but also help enhance the richness of content and the activity of search engines.AnQiCMS as a content management system focusing on efficiency, security, and customization, provides strong and flexible support for us to safely and effectively display these features in templates.

Ensure that the comment list and message form are presented beautifully while also considering safety and functionality, we need to deeply understand the characteristics of the AnQiCMS template system and adopt **practical** practices.

First, display the user comment list in the template

User comments are an indispensable part of articles or product pages. AnQiCMS provides special tags for managing and displaying these comments.

1. Call the comment list tag

To display comments on the page, we mainly usecommentListtags. This tag can flexibly pull comment data according to different requirements (such as specifying article ID, display type, etc).

For example, on an article detail page, to display the comments of the current article, you can use it like this:

{% commentList comments with archiveId=archive.Id type="page" limit="10" %}
    {% for item in comments %}
        <div>
            {# 评论用户和时间 #}
            <span>{{item.UserName}}</span>
            <span>{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}</span>

            {# 评论内容及其安全处理 #}
            <div>
                {# 检查评论审核状态,仅显示已审核内容,或给出审核中提示 #}
                {% if item.Status == 1 %}
                    {# 对于用户提交的评论内容,出于安全考虑,如果希望保留HTML格式,需要慎用`|safe`。
                       AnQiCMS在后台有敏感词过滤和内容安全管理,但模板层面仍建议根据内容信任度进行处理。
                       如果评论内容允许简单的HTML(如加粗),且后台已充分过滤,可以使用`|safe`;
                       如果只希望显示纯文本,则应使用`|striptags`过滤器。 #}
                    {{item.Content|safe}}
                {% else %}
                    <p>您的评论正在审核中...</p>
                {% endif %}
            </div>

            {# 回复功能 #}
            {% if item.Parent %}
                <blockquote>
                    <p>回复 {{item.Parent.UserName}}:</p>
                    <p>{{item.Parent.Content|striptags}}</p>
                </blockquote>
            {% endif %}

            {# 点赞按钮(通常需要JS配合实现交互) #}
            <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>
        </div>
    {% endfor %}
{% endcommentList %}

here,archiveId=archive.IdIt will automatically retrieve the article ID of the current page.type="page"This means enabling pagination feature,limit="10"Set the display of 10 comments per page.

2. Safely present comment content

User comment content (item.Content) is the place in user-generated content that needs to be most concerned with security.Although the AnQiCMS backend provides 'Content Security Management' and 'Sensitive Word Filtering' features, as a template developer, we still need to be cautious in handling the front-end display.

  • Review status (item.Status): Always checkitem.Status. Only when the status is1(Approved) will the full comment content be displayed. For other statuses, 'Comment under review' can be displayed or not displayed.
  • HTML content and XSS protection: If user comments are allowed to input HTML tags (such as bold, italic), and you trust the background filtering mechanism, you can use|safeThe filter allows the browser to parse this HTML. However, if the comment content is plain text, or you do not want any HTML to be rendered, it is strongly recommended to use|striptagsA filter to strip all HTML tags, thus effectively preventing cross-site scripting attacks (XSS).
  • Reply function: The comment structure of AnQiCMS supports multi-level replies (item.Parent)。In displaying the reply content, attention should also be paid to security, and it can be nested as needed.

3. Integrated comment pagination

If the number of comments is large, pagination is the key to improving user experience. CombinedcommentListlabel'stype="page"Parameters, we can make use ofpaginationtags to generate pagination navigation:

{% pagination pages with show="5" %}
    <ul class="pagination">
        {# 首页链接 #}
        <li class="{% if pages.FirstPage.IsCurrent %}active{% endif %}"><a href="{{pages.FirstPage.Link}}">首页</a></li>
        {# 上一页链接 #}
        {% if pages.PrevPage %}
            <li><a href="{{pages.PrevPage.Link}}">上一页</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}}">下一页</a></li>
        {% endif %}
        {# 末页链接 #}
        <li class="{% if pages.LastPage.IsCurrent %}active{% endif %}"><a href="{{pages.LastPage.Link}}">末页</a></li>
    </ul>
{% endpagination %}

4. Comment submission form

Comment submission is usually aPOSTrequest, the target address is/comment/publish. The form must includearchive_id(article ID),user_name(Username) andcontent(Comment content). If replies are supported, you also needparent_id(Parent comment ID).

`twig

<input type="hidden" name="archive_id" value="{% archiveDetail with name="Id" %}">
<input type="hidden" name="parent_id" value="" id="comment-parent-id"> {# 用于回复特定评论 #}
<input type="hidden" name="return" value="html"> {# 可选:指定返回格式 #}

<div>
    <label for="user_name">您的昵称:</label>
    <input type="text" id="user_name" name="user_name" required placeholder="请填写您的昵称">
</div>
<div>
    <label for="comment_content">评论内容:</label>
    <textarea id="comment_content" name="content" rows="5" required placeholder="留下您的真知灼见..."></textarea>
</div>

{# 验证码增强安全性 #}
<div style

Related articles

How to customize a template to achieve personalized display of single-page content (such as "About Us")?

In website operation, personalized display is crucial for shaping brand image and enhancing user experience.Especially single pages like "About Us" and "Contact Information", which carry the core information of the enterprise, can better convey the brand story and enhance the user's trust through customized design.AnQiCMS (AnQiCMS) provides a flexible way to achieve this goal, even for users with little technical background.### Understanding the Single Page Content Mechanism of Anqi CMS In Anqi CMS, single page content (such as "About Us")

2025-11-09

How to retrieve and display detailed information of a specified category (such as title, description, Banner image)?

In website operation, we often need to create unique and attractive display pages for specific categories, or flexibly call some detailed information of certain categories at different locations.For example, you may wish to display a dedicated Banner image at the top of a product category page, or list the title and introduction of a specific service category in the sidebar.AnQiCMS as an efficient and customizable content management system provides a very intuitive way to meet these needs.To obtain and display detailed information about a specified category, such as the title, description, and Banner image

2025-11-09

How the generation and processing method of thumbnails (such as cropping, padding) affects the front-end image display?

Optimize website image display: In-depth analysis of Anqi CMS thumbnail processing method In content management, images are an indispensable element to attract users and convey information.Whether a website image is clear, beautiful, and loads quickly directly affects the user experience and the overall professionalism of the page.And the thumbnail as the first impression of the image display, its generation and processing method is particularly important.AnQiCMS (AnQiCMS) provides a flexible thumbnail generation and processing mechanism for image management, allowing us to finely control the display effect of front-end images according to the needs of different scenarios.### Thumbnail

2025-11-09

How does AnQiCMS handle images in document content (such as remote image download, Webp format conversion)?

## Mastering the Whole Picture: How AnQi CMS intelligently handles document content images, enhancing website loading speed and user experience In today's content-king digital age, the expressiveness, loading speed, and search engine friendliness of website images are crucial for user experience and SEO.A high-efficiency content management system should provide powerful and convenient functions in image processing.AnQiCMS (AnQiCMS) is well-versed in this field, helping users easily optimize website images through a series of intelligent image processing mechanisms. No matter where the images come from or in what format they exist, they will be properly handled

2025-11-09

How to use友情链接 tags to display partner websites at the bottom of the website or other areas?

In website operation, the friendship link of cooperative websites is not only an important part of search engine optimization, but also an effective way to expand brand influence and promote mutual traffic redirection.Display these important cooperative partnerships prominently on the website, such as at the bottom or sidebar, which can effectively enhance user trust and the authority of the website.AnQiCMS (AnQiCMS) provides a convenient friend link management function and flexible tag calling method, allowing you to easily achieve this goal.### Overview of the friendship link backend management Before using the `linkList` tag

2025-11-09

How to configure pagination tags to provide user-friendly content list navigation?

In Anqi CMS, as the content on our website becomes richer and richer, how to efficiently organize and present this information so that visitors can easily find what they need among the vast amount of content has become a key issue.Excellent content list navigation, especially the pagination feature, not only enhances user experience, but is also an indispensable part of search engine optimization (SEO).The AnQi CMS provides powerful and flexible pagination tags, allowing you to easily configure user-friendly content list navigation.### The Foundation of Content List: Configure Content Retrieval Tag In Anqi CMS, all content lists, whether articles

2025-11-09

How to use if and for tags in templates to implement conditional judgment and loop display of data?

When building a website with AnQiCMS, the template is the key to content display.By flexibly using template tags, we can present the content of the back-end management in various forms to the users.Among them, `if` and `for` tags serve as the core of template logic control, helping us to implement conditional judgments and the cyclic display of data, making the website content more dynamic and rich.AnQiCMS's template engine borrows the syntax style of Django, which makes learning and using these tags very intuitive.It allows us to write logic directly in the template

2025-11-09

How to ensure that the date and time are displayed correctly on the front end using the timestamp formatting tag?

In website content operation, the correct display of date and time information is crucial, as it not only affects the user's reading experience but also directly relates to the timeliness and accuracy of the information.AnQiCMS (AnQiCMS) understands this point and provides a powerful and flexible timestamp formatting tag, allowing developers and operators to easily convert the original timestamp stored on the backend into a date and time format that is intuitive and easy to read for front-end users.Why do we need timestamp formatting?\n\nWe know that the data stored on the website backend, especially publishing time, update time, comment time, etc.

2025-11-09