In the daily operation of website content, we often encounter such a scenario: we need to determine whether a document, a page title, or any piece of text content contains the multiple keywords we preset.For example, we may want to know whether an article mentions both 'Anqi CMS' and 'Content Operation', or at least mentions 'Go language' or 'High performance'.At this moment, many friends will naturally think of the powerful template engine of AnQiCMScontainfilter.
So, when you need 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 features, we can clearly say,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, you cannot directly pass a list of keywords tocontaina filter and expect it to return a comprehensive result.
However, this does not mean that AnQiCMS cannot implement multi-keyword judgment.AnQiCMS' template engine is renowned for its flexible logic control and rich combination of filters. We can巧妙ly utilize these features, through some simple logical combinations, to achieve effects equivalent to batch processing.
UnderstandingcontainThe core function of the filter
First, let's briefly review.containThe usage of a filter. It is used to determine whether a string, array (slice), key-value pair (map), or struct contains the specifieda singleThe keyword returns 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 one by one, and then logically combine these results according to business requirements.This usually involves loops and conditional judgments.
We will demonstrate how to implement two common logical needs:
1. Check if the string containsany one of themKeyword (logical OR)
Assume we have a text that needs to be judged whether it contains any keyword from the keyword list. As long as there is a match, it is considered to meet the conditions.
Implementation approach:
- Define a list containing all the keywords to be checked.
- Initialize a boolean variable to mark whether any keyword is found, defaulting to
False. - Traverse the keyword list and use a filter to check each keyword
contain. - If any keyword is found, set the marker variable to
True, and can exit the loop early. - The result is displayed 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>
BecausetargetStringcontains "Go language", so it will output "The string contains the keywords in the list of"any one of themkeyword.”
2. Check if the string containsallkeyword (logical AND)
A common requirement is that a string is considered valid only when it contains all the keywords from the list.
Implementation approach:
- Define a list containing all the keywords to be checked.
- Initialize a boolean variable to mark whether all keywords are found, default to
True. - Traverse the keyword list and use a filter to check each keyword
contain. - If any keywordis notfound, set the mark variable to
False, and can exit the loop early. - The result is displayed 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 of"allkeywords."If you changekeywordsListto["Go语言", "PHP"]The string does not contain all the keywords in the list.
Flexible access to the keyword list
In the above example, we directly hard-coded the list of keywords in the template. But in practice, these keywords may come from more dynamic sources, such as:
- Split from string:If the keywords are stored in a comma-separated string (such as the keyword settings in the background), you can use
splitthe filter to convert it to a list.{% set rawKeywords = "Go语言,高性能,内容管理系统" %} {% set keywordsList = rawKeywords|split:"," %} - Data retrieved from the backend:The keyword list may be passed directly to the template as an array from the backend.
By these flexible mechanisms, you can prepare a keyword list according to your actual needs, and then combine it with loops andcontainfilters to easily judge a batch of keywords.
Summary
Although AnQiCMS'scontainThe filter itself does not provide a 'batch processing' feature for multiple keywords, but it has powerful template language features such asforloop,ifConditional judgment as well assetVariable assignment allows you to construct the complex logic you need in a concise and efficient manner.This modular design is a reflection of the flexibility and customizability of AnQiCMS, which encourages us to exercise creativity and combine existing features to meet specific operational needs.
Frequently Asked Questions (FAQ)
Q1:containDoes the filter support regular expression (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 data on the backend, or consider combining other string processing features in the template for more refined matching logic.
Q2: How should I perform case-insensitive keyword matching?A2:containThe filter is case sensitive. To perform a case-insensitive match, you can performcontainCheck before, convert the target string and keywords to the same case (for example, both to lowercase). AnQiCMS provideslowerThe filter can achieve this function.Example: {% if targetString|lower|contain:keyword|lower %}
Q3: Besides strings,containCan the filter judge 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 structure (struct).This makes it very practical in different data structures, but under the theme of this article, we mainly focus on the multi-keyword judgment of strings.