AnQiCMS provides a rich set of filters in the template for data processing and display, wherecontainThe filter is a practical tool often used to determine if the content contains specific keywords.When using such filters, we often encounter issues related to case sensitivity, which directly affects the accuracy of search and filtering results.

Knowcontainfilter usage and its basic methods

containThe filter plays the role of a "content detector" in the AnQi CMS template, it can determine whether a string, array, key-value pair (map), or structure (struct) contains a specific keyword and returns a boolean value (TrueorFalse)。Its syntax is concise and clear:

{{ obj|contain:关键词 }}

For example, if we want to check if a piece of text contains 'CMS':

{% set content = "欢迎使用安企CMS(AnQiCMS)" %}
{% if content|contain:"CMS" %}
    <p>内容中包含 "CMS"</p>
{% else %}
    <p>内容中不包含 "CMS"</p>
{% endif %}

This code will output 'The content contains 'CMS''. Because the stringcontentIndeed, there is the substring 'CMS'.

containThe filter is not limited to strings, it can also be applied to other data types:

  • Determine if a value exists in an array:
    
    {% set tags = ["安企CMS", "AnQiCMS", "内容管理系统"] %}
    {% if tags|contain:"AnQiCMS" %}
        <p>数组中包含 "AnQiCMS"</p>
    {% endif %}
    
  • Check if a key exists in a map or struct:
    
    {% set productInfo = {"Title":"安企CMS产品", "SKU":"AQ-CMS-001"} %}
    {% if productInfo|contain:"Title" %}
        <p>产品信息中包含 "Title" 键名</p>
    {% endif %}
    

In-depth discussion: Case sensitivity

When we usecontainThe core issue when the filter performs keyword matching is how it handles case sensitivity. According to the conventional behavior and documentation of the Anqi CMS template filter,containFilter is enabled by defaultCase sensitive:.

This means, 'CMS' and 'cms' incontainThe filter seems to be two completely different strings. If your original text is 'AnQiCMS' and you try to match it withcontain:"cms"the result will beFalse.

For example:

{% set source_text = "欢迎使用AnQiCMS内容管理系统" %}
{% if source_text|contain:"cms" %}
    <p>找到关键词 "cms" (大小写敏感)</p>
{% else %}
    <p>未找到关键词 "cms" (大小写敏感)</p>
{% endif %}

This code will output 'Keyword 'cms' not found (case-sensitive)', becausesource_textthere is no lowercase 'cms', only uppercase 'CMS'.

Currently, the template system of AnQi CMS,containFilterdoes not provide direct parameters or configuration options to switch case sensitivity.This means we cannot ignore case like some programming languages do, through aignoreCaseparameter to tell the filter to ignore case directly.

Flexible handling: Implement case-insensitive search

AlthoughcontainThe filter itself does not provide a direct case-insensitive configuration, but we can cleverly achieve this goal by combining other template filters. The most common method is to performcontainBefore judgment, convert the source string and the keyword to be matched to the same case (usually lowercase).

The Anqi CMS template is built-in.lowerFilter, which can convert all letters in an English string to lowercase. We just need tocontainapplylowerthe filter:

{% set source_text = "欢迎使用AnQiCMS内容管理系统" %}
{% set search_keyword = "cms" %}

{# 将源文本和搜索关键词都转换为小写,然后再进行匹配 #}
{% if source_text|lower|contain:search_keyword|lower %}
    <p>内容包含关键词 "{{ search_keyword }}"(大小写不敏感)</p>
{% else %}
    <p>内容不包含关键词 "{{ search_keyword }}"(大小写不敏感)</p>
{% endif %}

Run this code, it will output “The content contains the keyword “cms” (case insensitive), because “AnQiCMS” is converted to “anqicms”, “cms” is still “cms”, and both can be matched successfully.”}]

The advantages of this method are simplicity, efficiency, and full implementation at the template level, without modifying the backend code.

Application scenarios and practical recommendations

  • Scenarios requiring case sensitivity:When you need an exact match, such as according to product SKU (stock unit), user ID, specific code, or abbreviation,containThe default case sensitive behavior of the filter is very useful. It helps ensure the accuracy and uniqueness of data.

  • Scenarios where case insensitive is needed:When handling user input for search queries, article tags, comment content, or any content that may contain user input in varying cases, case-insensitive matching is usually required.For example, when a user searches for "marketing strategy

In short, the Anqi CMS iscontainThe filter is case-sensitive by default, although there is no direct option for case-insensitive configuration, but through flexible uselower(or}upper)Case-insensitive conversion filter, we can easily achieve case-insensitive keyword matching to meet the content filtering needs of different scenarios.


Common Questions (FAQ)

1.containFilter can identify the case of Chinese characters?In the Chinese context, Chinese characters themselves do not have the distinction of uppercase and lowercase like English letters. Therefore,containThe filter performs an exact character-level match when processing strings containing Chinese characters, without involving case conversion. For example,"你好世界"|contain:"世界"it will beTruewhilelowerorupperThe filter usually has no effect on Chinese characters.

2. If I need fuzzy matching (for example, matching part of a word),containIs the filter applicable? containThe filter performs an exact substring match.It will check if the keywords exist as a continuous sequence in the target string.containThe filter itself cannot meet the requirements, which usually requires implementing backend code or integrating more advanced search features.

3.containDoes the filter support regular expressions for more complex matching?English CMS template ofcontainThe filter does not currently support regular expressions as a match pattern.It only accepts plain strings as keywords for literal matching.If you need to match content based on regular expressions, it may be necessary to perform data preprocessing in the backend controller layer, or consider other custom tags or filters that provide regular expression matching capabilities.