In website content operation, the compliance and security of the content are of utmost importance.Especially for platforms that deal with user-generated content (UGC), or when posting articles or product information, we often need to quickly determine if a string of text contains specific sensitive words.AnQiCMS as a high-efficiency enterprise-level content management system also provides very flexible and convenient tools on the template level, allowing you to easily meet this requirement.

Although AnQiCMS has built-in powerful sensitive word filtering functions in the background management system, which can automatically detect and process during content publishing, but sometimes we may wish to make additional judgments in the front-end template, such as giving immediate prompts before users submit comments, or adjusting the display of page elements based on whether the content contains sensitive words.This is when the template filter of AnQiCMS comes in handy.

Use cleverlycontainFilter quickly judge sensitive words

In AnQiCMS template syntax, there is a very practical filter calledcontain. Its function is self-explanatory, that is, to determine whether a string, array, or key-value pair contains a specific keyword. This filter will return a boolean value (TrueorFalse),Let you easily judge in template logic.

Suppose we have a user's input comment contentuserCommentWould like to check if it contains the sensitive word "ad". You can use it like thiscontainFilter:

{% set userComment = "这条评论提到了广告内容,需要注意。" %}
{% if userComment|contain:"广告" %}
    <p style="color: red;">您的评论可能包含“广告”一词,请修改后再提交。</p>
{% else %}
    <p>评论内容正常。</p>
{% endif %}

In the above example, we first usesetTags define auserCommentvariables to simulate the content submitted by the user. Then, throughuserComment|contain:"广告"This expression,containThe filter will checkuserCommentIf the substring 'advertising' exists in the string. If it does, the condition is true, and a red warning will be displayed on the page.

Build a sensitive word list and perform batch detection

In practice, our sensitive words are often not just one. At this point, we can first define a list of sensitive words, and then check each sensitive word by traversing the loop.AnQiCMS template oflistThe filter can help us easily create an array

First, you can define a sensitive word array:

{% set sensitiveWords = '["敏感词一","不文明用语","非法内容","广告"]'|list %}

Here, we use|listThe filter converts a JSON array in string format into an array object that AnQiCMS template can recognize.

Next, combineforloop andifLogical judgment, we can implement batch detection:

{% set contentString = "这是一段用户评论,里面可能含有不文明用语,或者一些非法内容,请您审查。" %}
{% set sensitiveWords = '["敏感词一","不文明用语","非法内容","广告"]'|list %}
{% set hasSensitiveWord = false %} {# 初始化一个布尔变量来记录是否发现敏感词 #}
{% set foundWord = "" %} {# 记录发现的第一个敏感词 #}

{% for word in sensitiveWords %}
    {% if contentString|contain:word %}
        {% set hasSensitiveWord = true %}
        {% set foundWord = word %} {# 记录发现的第一个敏感词 #}
        {# 如果找到一个敏感词就足够,您可以选择在这里结束循环或者添加其他逻辑 #}
        {# AnQiCMS模板没有直接的break,但可以通过逻辑避免后续操作,例如只记录第一个 #}
        {% break %} {# 假设在AnQiCMS模板中存在类似循环中断的机制 #}
    {% endif %}
{% endfor %}

{% if hasSensitiveWord %}
    <p style="color: red;">您的内容可能包含敏感词“{{ foundWord }}”,请修改后再提交。</p>
{% else %}
    <p>内容正常,可以提交。</p>
{% endif %}

In this example,hasSensitiveWordThe variable is set to the start of the loop:false. OnceforLoop incontentStringDetect any onesensitiveWordsWord in the list,hasSensitiveWordwill be set totrueand recordedfoundWord. After the loop ends, we can decide which prompt information to display based onhasSensitiveWordvalue.

Application scenarios and precautions

This method of detecting sensitive words in the AnQiCMS template is very useful in many scenarios:

  1. Real-time feedback from users:In the comment section, message board, or next to any user input form, detect user input in real time and provide immediate feedback to improve the user experience.
  2. Content review assistance:For content editors or moderators, potential sensitive words can be highlighted during frontend preview to accelerate the review process.
  3. Personalized content display:Determine whether to display certain ads, related recommendations, or warnings based on the content containing specific words.

It should be noted that although the front-end template provides flexible detection capabilities, it should not replace the sensitive word filtering at the back-end system level.The 'Content Security Management' and 'Sensitive Word Filtering' on AnQiCMS backend are the first and most important line of defense to ensure website content compliance.The detection of front-end templates is more as a supplement and auxiliary to user experience.

Furthermore,containThe filter isCase sensitiveIf you need to perform a case-insensitive check, you may need to pass tocontainBefore the filter, convert the string and sensitive words to a uniform case (for example, all to lowercase), which may require the backend to provide the corresponding processing function or custom filter support.

In general, the template system of AnQiCMS is designed to be very flexible, throughcontainsuch practical filters, combinedlist/set/ifandforThe basic labels, you can implement powerful and detailed text content detection functions in the front-end template to further enhance the user experience and the fineness of content management.


Frequently Asked Questions (FAQ)

1. Can this method be used to replace sensitive words?A:containThe filter is mainly used to determine whether a piece of text contains specific keywords and return a boolean value. If you need to replace sensitive words with asterisks or other alternative text, you can use the AnQiCMS template provided.replaceFilter. For example,{{ contentString|replace:"敏感词一,***" }}You can replace 'Sensitive word one' with '***'.

2. Will a very large list of sensitive words affect the page loading speed?A: Sensitive word detection is performed on the user's browser.If the list of sensitive words is very large (for example, thousands of words), and the text to be detected is also very long, there may be a slight impact on some devices with weaker performance.For large-scale sensitive word filtering, we still strongly recommend relying mainly on the powerful AnQiCMS backend sensitive word filtering function optimized based on Go language, and the frontend template detection can be used as auxiliary or for immediate prompts for a small number of specific words.

Can it detect sensitive words in multiple languages?A: Yes.containThe filter is based on string matching. As long as the sensitive word list contains the corresponding language sensitive words, and the text to be detected is also in that language, effective detection can be performed.For example, you can define a list containing English profane words{% set englishSensitiveWords = '["spam","illegal","badword"]'|list %}Perform the detection.