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

Calendar 👁️ 85

In daily website content operations, we often need to standardize the management of user input or system-generated content, especially when it involves brand names.Maintaining brand name consistency and accuracy is crucial not only for enhancing 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, among whichcontainA filter is a very practical tool that can help us efficiently check if the text contains preset brand names.

Why do we need to check brand names in the content?

Under the scenario of website content operation, checking the frequency and accuracy of the brand name appearance has multiple meanings:

  1. Brand consistencyEnsure that all published content uses the correct, unified brand name spelling (for example, always use "AnQiCMS" instead of "AnQi CMS" or "anqicms"), which is crucial for building a professional brand image.
  2. SEO optimization: Search engines pay attention to the consistency of keywords when understanding and indexing web content.Brand name as a core keyword, its unified use helps to improve the search engine ranking of related pages.
  3. Compliance and risk controlSome industries or specific situations may require avoiding the mention of competitors' brand names or ensuring the lawful use of one's own brand name.By means of the inspection mechanism, it can effectively avoid potential legal risks.
  4. Content QualityThe use of standardized brand names is a sign of high-quality content, avoiding reading obstacles or misunderstandings caused by casual spelling.

containThe wonder of filters

The Anqi CMS template engine provides a rich set of filters,containIt is one of them. Its core function is to judge whether a string, array, key-value pair, or structure contains the specified "keyword" and returns a boolean value (TrueorFalseThis makes it an ideal choice for checking if a brand name exists in the text.

Review of basic usage:

{{obj|contain:关键词}}

here,objis the content you want to check (for example, the text entered by the user), and关键词Then it is the brand name you want to search for.

How to utilizecontainThe filter checks if the text entered by the user contains the preset brand name?

Assuming we have a content publishing system, users can enter text in the article content or product description.We hope to check if these texts contain our company's own brand name 'AnQi CMS'.

Step 1: Get the text to be checked.

This usually comes from the article content field, custom fields, or form data submitted by users.In AnQi CMS templates, these data are usually obtained through tags or variables.For example, on the detail page of an article, you can access the content of the article:

{% archiveDetail articleContent with name="Content" %}
{# articleContent 现在包含了文章的完整内容 #}

Or, if it is a custom form field, such as the product name filled in by the user in the commentproduct_name:

{# 假设有一个变量 input_text 存储了用户输入 #}
{% set user_input_text = guestbook_form.product_name.Value %}

Second step: usecontainFilter to check

Now, we can compare the obtained text content with the preset brand name.

Example 1: Check a single brand name

Assuming the brand name we preset is "AnQi CMS". We can check it like this in the template:

{% set user_input_text = articleContent %} {# 或者其他你想要检查的文本变量 #}
{% set hasAnqiCMS = user_input_text|contain:"安企CMS" %}

{% if hasAnqiCMS %}
    <p style="color: green;">内容中包含品牌名称“安企CMS”,符合规范。</p>
{% else %}
    <p style="color: red;">注意:内容中未检测到品牌名称“安企CMS”。请确保品牌名称正确。</p>
{% endif %}

In this example,hasAnqiCMSThe variable will be based onuser_input_textwhether it contains "AnQi CMS".TrueorFalse. Then, we can display different prompts based on this boolean value.

Example 2: Check multiple brand names (or variants)

In actual operation, the brand name may have multiple spellings or related keywords that need to be checked simultaneously.For example, in addition to "AnQiCMS", it may also be necessary to check "AnQiCMS" or "AnQi Content Management System".

due tocontainThe filter checks one keyword at a time, we can combine multiple check conditions using logicorOr iterate through a list of brand names:

Method A: Use logicorCombine conditions

{% set user_input_text = articleContent %}
{% set hasAnyBrandName = user_input_text|contain:"安企CMS" or user_input_text|contain:"AnQiCMS" or user_input_text|contain:"安企内容管理系统" %}

{% if hasAnyBrandName %}
    <p style="color: green;">内容中包含预设的品牌名称或其变体。</p>
{% else %}
    <p style="color: red;">注意:内容中未检测到任何预设的品牌名称或其变体。</p>
{% endif %}

This method is suitable when there are not many brand names to check.

Method B: Traverse the list of brand names (more flexible)

If the brand name list is long, or needs to be loaded dynamically from the configuration, we can first define a brand name array and then iterate to check. AlthoughcontainThe filter itself does not directly receive an array as keywords, but combined with loops andsettags can be implemented:

{% set brand_names_to_check = ["安企CMS", "AnQiCMS", "安企内容管理系统"] %}
{% set user_input_text = articleContent %}
{% set found_brand = false %}

{% for brand_name in brand_names_to_check %}
    {% if user_input_text|contain:brand_name %}
        {% set found_brand = true %}
        {% break %} {# 找到一个即可停止循环 #}
    {% endif %}
{% endfor %}

{% if found_brand %}
    <p style="color: green;">内容中包含预设的品牌名称或其变体。</p>
{% else %}
    <p style="color: red;">注意:内容中未检测到任何预设的品牌名称或其变体。</p>
{% endif %}

This method is more scalable and convenient for managing a large list of brand names.

Practice and **recommendations

  • Application scenario:Except for the content of the article,containThe filter can be applied to comment review, user messages, custom form submissions, automatic checking of website titles or descriptions, and other scenarios.
  • Feedback mechanism: After finding or not finding the brand name, clear feedback can be provided.In the background, this may mean adding a tag to the content;On the front end, reminders or guides to correct the user can be displayed.
  • Combine with other features:containFilters are usually combined with other control flow tags (such asif/set/for) to build more complex logic.
  • Handle case sensitivity carefully: Please note, the security of the Anqi CMS.containThe filter isCase sensitiveThis means that 'AnqiCMS' and 'Anqi cms' are considered different keywords.If you need to perform a case-insensitive check, you can consider converting the text to be checked and the keywords to the same case (e.g., all to lowercase) before the check, but this requires the corresponding Safe CMS template engine to provide it.lowerorupperThe filter is supported. According to the document,lowerandupperThe filter is supported, so it can be implemented like this:
    
    {% set user_input_text_lower = user_input_text|lower %}
    {% set brand_name_lower = "安企CMS"|lower %}
    {% set hasAnqiCMS_case_insensitive = user_input_text_lower|contain:brand_name_lower %}
    
  • Avoid over-matching:containThe filter checks for substrings. For example, checking 'Apple' might match 'Pineapple'.In some cases, this may not be the result you want. If you need more precise word matching, you may need to combine more complex logic or regular expressions (if the Anqi CMS template engine supports it) to achieve this.

By flexible applicationcontainThe filter can greatly improve the automation and standardization of website content management, ensure the consistency of the brand image, and optimize the user experience during content consumption.


Frequently Asked Questions (FAQ)

Q1:containIs the filter case-sensitive?A1: Yes,containThe filter is case sensitive. For example, checking if “AnQi CMS” contains “cms” will returnFalseIf you need to perform a case-insensitive check, it is recommended that you convert the text to be checked and the target keywords to the same case before using the filterlowerorupperFilter to convert to uniform case and then proceedcontainCheck.

Q2: How to check if the user's input text contains any of the predefined brand names?A2: You can use logicorCombine multiplecontainCheck conditions, for exampleuser_input_text|contain:"BrandA" or user_input_text|contain:"BrandB". If the list of brand names is long, a more flexible approach is to first store all the brand names in an array variable, then throughforLoop through this array and execute for each brand namecontainCheck and stop the loop once a match is found

Q3: Besides strings,containCan the filter check other types of data?A3: Yes, according to the Anqi CMS documentation,containFilters can be used for strings and can also judge a keyword.

Related articles

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

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

2025-11-07

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 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

How to store the judgment result of the `contain` filter in a variable for subsequent complex logic judgment?

In AnQi CMS template development, we often need to dynamically display or hide certain elements based on specific content conditions, or execute different logical branches.It is straightforward to output the result of a judgment directly in a template, but when you need to perform more complex logical branches based on this judgment, direct output seems inadequate.At this point, storing the judgment result in a variable has become the key to achieving fine-grained control.

2025-11-07