containFilter is a very practical tool that can help us efficiently check if the text contains predefined brand names.

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

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

  1. Brand consistency:Ensure that all published content uses the correct and consistent brand name spelling (for example, always "AnQi CMS" rather than "AnQi CMS" or "anqicms"), which is crucial for building a professional brand image.
  2. SEO optimizationEnglish search engines pay attention to the consistency of keywords when understanding and indexing web content.The brand name as a core keyword, its unified use helps to improve the search engine ranking of related pages.
  3. Compliance and Risk Control:Some industries or specific situations may require avoiding the mention of competitors' brand names, or ensuring the legal use of one's own brand name.Through the checking mechanism, potential legal risks can be effectively avoided.
  4. Content Quality: Proper use of brand names is a sign of high-quality content, which avoids reading obstacles or misunderstandings caused by随意拼写 spellings.

containThe magic of filters

The template engine of Anqi CMS provides a rich set of filters,containIt is one of them. Its core function is to determine 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 brand names exist in text.

Basic usage review:

{{obj|contain:关键词}}

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

How to make use ofcontainDoes the filter check if the text entered by the user contains the preset brand name?

The user can enter text in the article content or product description in the content publishing system.We hope to check if these texts contain our company's own brand name "AnQi CMS".

Step 1: Get the text content to be checked

This usually comes from the article content field, custom fields, or user submitted form data.In the template of AnQi CMS, these data are usually obtained through tags or variables.

{% 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 commentsproduct_name:

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

Step 2: UsecontainFiltered by the filter

Now, we can compare the text content obtained 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,hasAnqiCMSVariables will depend onuser_input_textwhether it contains “AnQi CMS” or notTrueorFalse. Then, we can display different prompt messages based on this boolean value.

Example 2: Check multiple brand names (or variants)

In actual operation, brand names may have various spellings or related keywords that need to be checked simultaneously.For example, in addition to "AnQiCMS

Due tocontainThe filter checks one keyword at a time, we can combine multiple check conditions or iterate over a list of brand names:orOr use logic to combine conditions:

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 the number of brand names to be checked is not many.

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

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

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

  • [en] Application scenarios:In addition to the content of the article,containfilters can also be applied to scenarios such as comment review, user messages, custom form submissions, automatic checking of website titles or descriptions, and more.
  • Feedback MechanismEnglish: Find or not find the brand name, and provide clear feedback.In the background, this may mean adding a tag to the content; on the front end, it can display a reminder or guide the user to correct it.
  • Combine with other features:containFilters are usually combined with other control flow tags (such asif/set/for) to be used together, in order to construct more complex logic.
  • Handle case sensitivity with carePlease note, the security CMS ofcontainThe filter iscase-sensitiveEnglish.This means that "AnQi CMS" and "AnQi cms" are considered as different keywords.loweroruppersupports filters. According to the document,lowerandupperfilters are supported, so it can be implemented in this way:
    
    {% 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 will check the substring.For example, checking “Apple” might match “Pineapple”.In some cases, this may not be the result you want.If you need more precise whole-word matching, it may be necessary to combine more complex logic or regular expressions (if the security CMS template engine supports it) to achieve this.

By using flexibilitycontainFilter, we can greatly improve the automation and standardization of website content management, ensure the consistency of brand image, and optimize the user experience during content consumption.


Common Questions (FAQ)

Q1:containFilter is case sensitive?A1: Yes,containFilter is case sensitive. For example, checking if “安企CMS” contains “cms” will returnFalseIf a case-insensitive check is needed, it is recommended to first convert the text to be checked and the target keywords to lowercase before using the filter.lowerorupperFilter and convert the size to a uniform case, thencontaincheck.

Q2: How to check if the user's input text contains any of the predefined brand names?A2: You can use logicorto combine multiplecontaincheck conditions, such asuser_input_text|contain:"BrandA" or user_input_text|contain:"BrandB"。If the brand name list is long, a more flexible approach is to first store all brand names in an array variable, thenforloop through this array and perform actions on each brand name,containCheck, stop the loop once a match is found.

Q3: Besides strings,containCan the filter check other types of data?A3: Yes, according to the document of Anqi CMS,containThe filter can be used not only for strings, but also to judge a keyword