containFilter.

Then, when it is necessary to determine whether multiple keywords exist in a string,containDoes the filter have a batch processing mechanism? After a deep understanding of the AnQiCMS template engine function, we can definitely say that, containThe filter itself is designed to handle the detection of a single keyword; it does not have a built-in mechanism for batch processing multiple keywords to judge them all at once.That is to say, you cannot directly pass a list of keywords tocontaina filter and expect it to return a comprehensive result.

UnderstandingcontainThe core function of the filter

First, let's briefly review.containThe usage of a filter. It is used to determine if a string, array (slice), key-value pair (map), or structure (struct) contains the specifiedsingleKeyword, and return a boolean value (TrueorFalse).

Basic usage example:

{% set targetString = "欢迎使用安企CMS,一个基于Go语言开发的高性能内容管理系统。" %}

{% if targetString|contain:"AnQiCMS" %}
    <p>目标字符串包含“AnQiCMS”</p>
{% else %}
    <p>目标字符串不包含“AnQiCMS”</p>
{% endif %}

This code will output ‘The target string contains ‘AnQiCMS’’, becausetargetStringit indeed contains this keyword.

Implement the logic strategy for multi-keyword judgment

SincecontainThe filter can only check one keyword at a time, so to implement multi-keyword judgment, we need to check each keyword individually, and then logically combine these check results according to business requirements.This usually involves loops and conditional judgments.

We will demonstrate how to implement this by using two common logic requirements:

1. Check if a string containsany of the followingKeyword (Logical OR)

Assume we have a text that needs to be judged for whether it contains any of the keywords in the list. As long as there is a match for any keyword, it is considered to meet the condition.

Implementation idea:

  • Define a list containing all keywords to be checked.
  • Initialize a boolean variable to mark whether any keyword is found, defaulting toFalse.
  • Traverse the keyword list, usingcontainFilter for checking.
  • If any keyword is found, set the flag variable toTrue, and can exit the loop early.
  • Display the result based on the value of the marked variable.

Code example:

{% set targetString = "安企CMS,高效的内容发布利器,Go语言架构。" %}
{% set keywordsList = ["Go语言", "性能", "PHP", "WordPress"]|list %} {# 使用list过滤器创建关键词列表 #}
{% set foundAnyKeyword = false %} {# 初始化标记变量 #}

{% for keyword in keywordsList %}
    {% if targetString|contain:keyword %}
        {% set foundAnyKeyword = true %} {# 找到任意一个关键词就设为true #}
        {% break %} {# 找到一个就足够了,跳出循环 #}
    {% endif %}
{% endfor %}

{% if foundAnyKeyword %}
    <p>该字符串包含关键词列表中的**任意一个**关键词。</p>
{% else %}
    <p>该字符串不包含关键词列表中的任何关键词。</p>
{% endif %}

<p>(目标字符串:“{{ targetString }}”,待检查关键词:“{{ keywordsList|join:", " }}”)</p>

In this example, becausetargetStringcontains “Go language”, so it will finally output “The string contains keywords in the list of”}]any of the followingKeyword.”

2. Check if the string containsallKeyword (logical AND)

An alternative common requirement is that a string is considered valid only when it contains all the keywords from the list.

Implementation idea:

  • Define a list containing all keywords to be checked.
  • Initialize a boolean variable to mark whether all keywords are found, default asTrue.
  • Traverse the keyword list, usingcontainFilter for checking.
  • If any of the keywordsarefound, set the marker variable toFalse, and can exit the loop early.
  • Display the result based on the value of the marked variable.

Code example:

{% set targetString = "安企CMS是一个基于Go语言开发的高性能内容管理系统。" %}
{% set keywordsList = ["Go语言", "高性能", "内容管理系统"]|list %} {# 定义关键词列表 #}
{% set allKeywordsMatched = true %} {# 初始化标记变量,默认为true #}

{% for keyword in keywordsList %}
    {% if not (targetString|contain:keyword) %} {# 如果有一个关键词不包含 #}
        {% set allKeywordsMatched = false %} {# 就将标记设为false #}
        {% break %} {# 找到一个不匹配的就足够了,跳出循环 #}
    {% endif %}
{% endfor %}

{% if allKeywordsMatched %}
    <p>该字符串包含关键词列表中的**所有**关键词。</p>
{% else %}
    <p>该字符串未包含关键词列表中的所有关键词。</p>
{% endif %}

<p>(目标字符串:“{{ targetString }}”,待检查关键词:“{{ keywordsList|join:", " }}”)</p>

In this example,targetStringPerfect match for all keywords, so it will output "The string contains the keywords in the list,"allif you changekeywordsListchange it to["Go语言", "PHP"]If not, it will output 'The string does not contain all the keywords in the list.'

Flexiblely get the keyword list

In the above example, we directly hardcoded the list of keywords in the template. However, in practical applications, these keywords may come from more dynamic sources, such as:

  • Split from string:If keywords are stored in a comma-separated string (such as the keyword settings on the backend), you can usesplitthe filter to convert it to a list.
    
    {% set rawKeywords = "Go语言,高性能,内容管理系统" %}
    {% set keywordsList = rawKeywords|split:"," %}
    
  • Obtained from backend data:Keyword list may be passed directly as an array from the backend to the template.

Through these flexible mechanisms, you can prepare a keyword list according to actual needs, and then combine it with loops andcontainfilters to easily judge multiple keywords in bulk.

Summary

Although the AnQiCMScontainThe filter itself does not provide the function of 'batch processing' multiple keywords, but it has powerful template language features such asforlooping,ifcondition judgment andsetVariable assignment, which allows you to build the complex logic you need in a concise and efficient manner.This modular design is a demonstration of AnQiCMS's flexibility and customizability, encouraging us to exercise creativity and combine existing features to meet specific operational needs.


Common Questions (FAQ)

Q1:containDoes the filter support regex (Regex) matching?A1: AnQiCMS built-incontainThe filter currently does not support regular expression matching.It performs a simple substring search.If you need to perform complex pattern matching, you may need to process the data on the backend, or consider combining other string processing functions in the template for more refined matching logic.

Q2: How do I perform case-insensitive keyword matching?A2:containThe filter is case-sensitive. To achieve case-insensitive matching, you can in the processcontainBefore checking, convert both the target string and the keywords to the same case (e.g., all to lowercase). AnQiCMS provideslowera filter that can achieve this function.Example: {% if targetString|lower|contain:keyword|lower %}

Q3: Besides strings,containCan the filter also determine other types of data?A3: Yes,containThe filter can not only judge strings but also be used to check if a value exists in an array (slice), or if a key exists in a key-value pair (map) or a struct (struct).This makes it very practical in various data structures, but under the theme of this article, we mainly focus on multi-keyword judgment in strings.