In the AnQiCMS template, can the `contain` filter flexibly configure case-sensitive keywords?

Calendar 👁️ 87

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.

Related articles

When do you need to judge whether multiple keywords exist in a string, does the `contain` filter have a batch processing mechanism?

In the daily operation of website content, we often encounter such a scenario: we need to judge whether a document, a page title, or any text content contains multiple keywords that we have preset.For example, we might want to know if an article mentions both 'AnQi CMS' and 'Content Operation', or at least mentions 'Go language' or 'High performance'.At this time, many friends will naturally think of the powerful `contain` filter in the AnQiCMS template engine.Then, when you need to judge whether multiple keywords exist in a string

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

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

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

How to count the total number of times a specific keyword appears in the content of AnQiCMS articles?

In content operation, the reasonable layout and statistics of keywords are an indispensable part of optimizing search engine performance and improving user experience.A precise keyword distribution can not only help search engines better understand your content, but also allow users to find the information they need faster.AnQiCMS (AnQiCMS) relies on its powerful template engine to provide us with a flexible way to count the occurrences of specific keywords in article content, thereby assisting our content strategy.

2025-11-07

The `count` filter is it for exact match or partial match when calculating the number of occurrences of a value in an array?

In Anqi CMS template development, the `count` filter is a very practical tool that can help us easily count the number of times a specific value appears.However, when using this filter, many users may wonder: when it calculates the number of times a value appears, is it performing an exact match, or a more flexible partial match?The answer is not one-size-fits-all, but varies depending on the data type.

2025-11-07

How to use the `count` filter to analyze the keyword density on AnQiCMS templates?

In website operation and Search Engine Optimization (SEO), keyword density is a common but easily misunderstood concept.It refers to the ratio of the number of times a keyword appears in the web content to the total number of words, usually expressed as a percentage.Although the algorithms of search engines are more complex now, it is no longer the era when simply stacking keywords could achieve good rankings, but moderately paying attention to keyword density can help us ensure that the content theme is clear and convey the core information of the page to the search engine.

2025-11-07

How does the `count` filter handle the counting logic for Chinese character strings?

In daily content operations, we often encounter scenarios where we need to perform statistical analysis on content, such as counting the number of times specific keywords appear, checking the frequency of repetition of certain elements in the content, etc.AnQiCMS provides many practical template filters to help us achieve these functions, where the `count` filter is a powerful tool for counting.When dealing with content containing Chinese characters, it is particularly important to understand how the `count` filter performs counting.

2025-11-07