AnQiCMS provides a rich set of filters in the template for data processing and display, among whichcontainThe filter is a practical tool often used to determine if 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.

Get to knowcontainFilters and their basic usage

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

{{ obj|contain:关键词 }}

For example, if we want to check if a 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 stringcontentThe string indeed contains the substring "CMS".

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

  • Check if a certain value exists in the 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 usecontainWhen a filter performs keyword matching, a core issue is how it handles case. According to the regular behavior and documentation of the Anqi CMS template filter,containThe filter is by defaultCase sensitive.

This means, 'CMS' and 'cms' are 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 “The keyword “cms” (case-sensitive) was not found” becausesource_textthere is no lowercase “cms”, only uppercase “CMS”.

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

Adaptability: Implement case-insensitive search

ThoughcontainThe 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 keywords to be matched to the same case (usually lowercase).

The Anqi CMS template is built in.lowerFilter that converts all letters in a string to lowercase. We just need to applycontainbeforelowerfilter:

{% 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 the match will be successful.”

The advantages of this method are simple, efficient, and fully implemented at the template level, without modifying the backend code.

Application scenarios and practical suggestions

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

  • Scenarios where case-insensitive search is needed:When handling user input search queries, article tags, comments, or any content that may contain user arbitrarily inputted case, it is usually necessary to perform case-insensitive matching.For example, when a user searches for "marketing strategy", it is also important to match with "marketing strategy" or "marketing strategy", at this time, the method of converting to lowercase before matching is particularly important.

In short, the Anqi CMS'scontainThe filter is case-sensitive by default, although there is no direct option for case-insensitive configuration, but it can be flexibly usedlower(orupper)Case-insensitive converter, we can easily implement case-insensitive keyword matching to meet the content filtering needs of different scenarios.


Frequently Asked Questions (FAQ)

1.containCan the filter recognize the case of Chinese characters?In the Chinese context, Chinese characters themselves do not have the case distinction 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:"世界"willTruewhilelowerorupperThe filter usually has no effect on Chinese characters.

2. If I need to perform fuzzy matching (such as matching part of a word),containIs the filter applicable? containThe filter performs an exact substring match. It checks if the keyword exists as a continuous sequence within the target string.If you need more complex fuzzy matching (such as based on edit distance, semantic similarity, etc.),containThe filter itself cannot satisfy this, which usually requires backend code or integration with more advanced search features to achieve.

3.containDoes the filter support regular expressions for more complex matching?Safe CMS template'scontainThe filter does not directly support regular expressions as matching patterns.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.