In website content operation, the compliance and security of content are of utmost importance.Especially for platforms with user-generated content (UGC) or when publishing articles, product information, we often need to quickly determine whether a string of text contains specific sensitive words.AnQiCMS as an efficient corporate-level content management system also provides very flexible and convenient tools at the template level, allowing you to easily meet this requirement.
虽然AnQiCMS在后台管理系统内置了强大的敏感词过滤功能,可以在内容发布时自动进行检测和处理,但有时我们可能希望在前端模板中进行额外的判断,比如在用户提交评论前给出即时提示,或者根据内容是否包含敏感词来调整页面元素的显示。This is where 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 returns a boolean value (TrueorFalse),Let you easily make judgments in template logic.
Suppose we have a user input comment contentuserComment, want 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 users. Then, throughuserComment|contain:"广告"this expression,containthe filter will checkuserCommentDoes the string contain the substring "advertising". If it does, the condition judgment will be true, and a red warning will be displayed on the page.
Build a list of sensitive words and perform batch detection
In practical operation, our sensitive words are often not just one.This is where we can first define a list of sensitive words, and then detect each sensitive word by iterating over it.listThe filter helps us conveniently create arrays.
Firstly, you can define a sensitive word array:
{% set sensitiveWords = '["敏感词一","不文明用语","非法内容","广告"]'|list %}
Here, we use|listThe filter converts a string-formatted JSON array into an array object that AnQiCMS template can recognize.
Next, combiningforloop 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 at the beginning of the loop.falseOnce.forLoop in.contentStringDetect any one of them.sensitiveWordsList of words,hasSensitiveWordwill be set to,true, and be recorded,foundWord. Thus, after the loop ends, we can decide which prompt message to display based on thehasSensitiveWordvalue.
Application scenarios and precautions
This method of detecting sensitive words in AnQiCMS templates is very useful in many scenarios:
- User real-time feedback:In comment sections, message boards, or any user input forms, detect user input in real-time and provide immediate feedback to enhance user experience.
- Content review assistance:For content editors or moderators, potential sensitive words can be highlighted in the frontend preview, which speeds up the review process.
- Personalized content display:Based on whether the content includes specific words, decide whether to display certain ads, related recommendations, or warning information.
It is important to note that although the front-end template provides flexible detection capabilities, it should not replace the backend system-level sensitive word filtering.The "Content Security Management" and "Sensitive Word Filtering" of AnQiCMS backend are the first and most important line of defense to ensure the compliance of website content.The detection of front-end templates is more about supplementing and assisting the user experience.
In addition,containThe filter isCase sensitive:English. If you need to perform case-insensitive detection, you may need to pass thecontainBefore the filter, convert both strings and sensitive words to a uniform case (e.g., all to lowercase), which may require the backend to provide the corresponding handling function or custom filter support.
In general, the template system of AnQiCMS is designed to be very flexible, throughcontainsuch practical filters, combined withlist/set/ifandforTags such as [auto], you can implement powerful and detailed text content detection features in the front-end template, further enhancing the user experience of the website and the precision of content management.
Common Questions (FAQ)
1. Can this method be used to replace sensitive words?A:containThe filter is mainly used to determine if a piece of text contains specific keywords and returns a boolean value. If you need to replace sensitive words with asterisks or other substitute text, you can use the template provided in AnQiCMS.replacea filter. For example,{{ contentString|replace:"敏感词一,***" }}It can replace 'sensitive word one' with '***'.
2. Will a very large list of sensitive words affect the page loading speed?
3. Can it detect sensitive words in multiple languages?A: Yes, it can.containThe filter is based on string matching.If the sensitive words in your list are in the corresponding language, and the text to be checked is also in that language, then effective detection can be performed.{% set englishSensitiveWords = '["spam","illegal","badword"]'|list %}Perform the detection.