How to quickly judge whether a string of text in the AnQiCMS template contains specific sensitive words?

Calendar 👁️ 74

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.

Related articles

How to display the links and titles of the previous and next related articles on the document detail page of Anqi CMS?

In Anqi CMS, adding links and titles of "Previous Article" and "Next Article" to the document detail page is a very practical feature to enhance user experience and content consistency.This not only encourages users to browse more content, but also helps optimize the internal links of the website.AnQi CMS provides simple and efficient template tags, allowing us to easily meet this requirement.

2025-11-07

How to use the `archiveFilters` tag to add advanced filtering functionality to the document list of AnQi CMS?

Manage content in Anqi CMS, especially when the website content becomes rich, how to help visitors quickly find the information they need has become the key to improving user experience.If your website offers a diverse range of products or services, or contains a large number of articles with different attributes, then an efficient filtering function will be essential.Today, let's delve into a very useful tag in Anqi CMS, `archiveFilters`, which helps us easily add advanced filtering features to the document list.The foundation of the flexible content model

2025-11-07

How to use the `archiveList` tag to flexibly build various document lists and pagination display in Anqi CMS?

Manage and display content is a core operation in Anqi CMS, where the `archiveList` tag plays a crucial role.It can not only help us flexibly present lists of various documents such as articles, products, etc., but also seamlessly connect with the pagination function, meeting various complex content display needs.## Getting to Know the `archiveList` Tag: A Tool for Displaying Content The `archiveList` tag is a core tool in the Anqi CMS template used to retrieve document lists.

2025-11-07

How to display detailed information of a specific document in the Anqi CMS template, including the title, content, and thumbnail?

In AnQi CMS, sometimes we may need to display detailed information of a preset document at a specific location on the website, such as a recommendation area on the homepage or the '精选内容' module in the sidebar, including its title, content, and an eye-catching thumbnail.To achieve this goal, Anqi CMS provides a very flexible and intuitive way.

2025-11-07

How to use the `contain` filter to check if the user's input text contains the preset brand name?

In daily website content operations, we often need to standardize the management of user input or content generated by the system, especially when it involves brand names.Maintaining the consistency and accuracy of the brand name is crucial, not only for enhancing the brand image, but also for the SEO performance of the website, legal compliance, and user experience.AnQi CMS provides a flexible and powerful template engine, where the `contain` filter is a very practical tool that can help us efficiently check if the text contains the preset brand name.Why check the brand name in the content

2025-11-07

How to determine if a specific value exists in an array (slice) while developing an AnQi CMS template?

During the development of Anqi CMS templates, we often encounter scenarios where we need to determine whether an array (slice) contains a specific value.For example, you may need to dynamically adjust the display of content based on whether the user tag exists in a predefined tag list;Or when handling complex business logic, determine whether a permission ID exists in the current user's permission set.For this requirement, AnQiCMS template engine provides a concise and powerful solution, allowing developers to handle these logic in an elegant way.In the AnQiCMS template system

2025-11-07

Can `contain` filter be used in AnQiCMS template to check if a key exists in a map or struct?

In Anqi CMS template development, flexible handling of data structures is the key to dynamic content display.When we need to determine whether a complex data type, such as a key-value pair (map) or a structure (struct), contains a specific key name, the built-in `contain` filter provides a convenient and efficient solution.

2025-11-07

What is the difference in the judgment logic of the `contain` filter when processing Chinese string and English string?

In AnQi CMS template design, we often use various filters to process and judge data.Among them, the `contain` filter is a very practical tool that can help us quickly determine whether a text, array, or object contains specific keywords.Many users may be curious about whether there is a difference in the judgment logic of the `contain` filter when processing Chinese and English strings.

2025-11-07