When displaying user comments in AnQiCMS templates, although the system has already performed initial and even deep sensitivity word detection and filtering on the comment content, sometimes we may still need to perform further refinement at the front-end display level, or present filtered words in a specific way (such as replacing them with asterisks) to enhance user experience or explicitly indicate that the content has been processed.

AnQiCMS's sensitive word filtering mechanism: Behind the scenes hero

This system-level filtering has multiple advantages: it is efficient, centrally managed, and can intercept inappropriate content at the source, reducing operational risks.Users can configure and maintain sensitive word libraries, and even define different processing strategies (such as direct deletion, replacement, review, etc.), in the background management interface of AnQiCMS, usually in modules such as 'Function Management' or 'Content Security Management'.

Template level secondary review and display optimization

Although AnQiCMS has a solid defense on the backend, in certain specific scenarios, we may wish to have a 'second check' at the template level, or handle and display sensitive words in comments in a more flexible way. For example:

  1. Visual prompt:Sensitive words to be filtered will be replaced with:***Or other symbols, clearly informing the user that the comment content contains sensitive information.
  2. Content truncation:Shorten comments containing a large number of sensitive words and prompt to review the full content.
  3. Dynamic review:For some mild sensitive words, on the backend it may just be marked, and on the frontend it is decided whether to display directly or partially obscure based on the mark.

AnQiCMS's template engine provides rich filter functions to help us meet these needs.

Using AnQiCMS template filters to implement comment sensitive word processing

Assuming that the backend of AnQiCMS only replaces sensitive words with blank characters or retains the original word but provides some kind of marker.而我们希望在前端,将这些词汇明确地替换成星号。commentListLabeling the comment content and combiningreplaceFiltering the comment content.

Firstly, in your comment list template file (usually)comment/list.htmlor in the article detail page where comments are displayed) we will usecommentListtags to traverse comments:

{# 假设我们正在某个文档的详情页,archive.Id 可获取当前文档ID #}
{% commentList comments with archiveId=archive.Id type="list" limit="10" %}
    {% for item in comments %}
    <div>
        <p><strong>{{item.UserName}}</strong> 发表于 {{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}</p>
        <div>
            {# 这里是对评论内容进行处理的核心部分 #}
            {% set cleanContent = item.Content %}
            {# 假设“垃圾”、“广告”、“敏感词”是我们需要替换的词汇 #}
            {% set cleanContent = cleanContent|replace:"垃圾,***" %}
            {% set cleanContent = cleanContent|replace:"广告,***" %}
            {% set cleanContent = cleanContent|replace:"敏感词,***" %}
            
            {{ cleanContent|safe }} {# 使用safe过滤器确保HTML内容正确渲染 #}
        </div>
        {# 其他评论操作,如回复、点赞等 #}
    </div>
    {% else %}
    <p>暂无评论,快来发表您的看法吧!</p>
    {% endfor %}
{% endcommentList %}

In the above code snippet:

  1. We first use{% commentList comments ... %}loop through the comments data obtained.
  2. Inside the loop,{{item.UserName}}and{{item.CreatedTime}}display the name of the comment user and the publication time separately.
  3. The core lies in the processing ofitem.Content. We defined a variablecleanContentto store the processed comment content.
  4. We usedreplacefilters consecutively, and their usage isobj|replace:"旧词,新词"In this example, we will replace some preset sensitive words such as 'rubbish', 'ad', and 'sensitive word' with***You can add more as needed,replaceThe filter is used to process different sensitive words.
  5. Finally, use{{ cleanContent|safe }}to render the comment content.safeThe filter is necessary because the comment content may contain HTML tags,safeTell the template engine that this content is safe and does not require escaping; it can be output directly as HTML.

Caution:

  • This template levelreplaceFilter needs youTo know in advanceThe list of sensitive words to be replaced, and added manually one by one in the template. This method is inefficient for a large number of dynamic sensitive words, and has a high maintenance cost.
  • This method is more suitable for handling a small, fixed set of sensitive words that need to be displayed in a specific way on the front end (such as***) for secondary processing.
  • Strongly recommended:依赖AnQiCMS的后台敏感词过滤功能作为主要防线。The template-level filtering is more of a supplementary display optimization method rather than a substitute for system-level security protection.

Summary

The Auto CMS provides a solid content management foundation and security.Through the built-in sensitive word filtering mechanism of the system, the security and compliance of the website content has been effectively guaranteed.replaceCan also provide flexible solutions.The key is to understand the different responsibilities between system-level filtering and template-level display optimization, and combining their use to provide users with a safe, high-quality and smooth experience commenting environment.


Common Questions (FAQ)

Q1: AnQiCMS的敏感词过滤是自动进行的吗?我需要手动检查每条评论吗? A1:Yes, AnQiCMS has built-in sensitive word filtering function, the system will automatically detect and process the comment content according to the sensitive word library you configure in the background.You can set different handling strategies, such as automatic replacement, intercept submission, or enter the manual review queue.Therefore, in most cases, you do not need to manually check each comment unless you have set comments to require manual review.

Q2: 我能否自定义敏感词列表和过滤规则? A2:Of course, you can.AnQiCMS in the background provides a flexible sensitive word management interface, you can add, modify or delete sensitive words according to the specific needs of the website, and set different processing rules for different sensitive words or types of sensitive words, such as replacing them with specific characters, directly deleting comments, or marking them for review.

Q3: The filter (such asreplace) can completely replace the background sensitive word filtering function? A3:Not allowed.The filters in the template are mainly used for displaying and formatting front-end content, and they are executed when the content is read from the database and prepared to be presented to the user.The AnQiCMS background sensitive word filtering is at the system level, which intervenes in the processing at the time of comment submission and before content is stored, and is the first and most critical defense line for content security.The template filter should only be used as a supplementary means of optimizing the front-end display effect and should not be the main guarantee mechanism for the website's content security.