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 becomes the key to achieving fine-grained control.

AnQi CMS provides powerful functionscontainA filter that helps us determine whether a string, array, or key-value pair contains specific content. After the filter performs the judgment, it will return a boolean value (TrueorFalse),perfectly fits the need for subsequent complex logical judgment.

UnderstandingcontainFilter

First, let's understandcontainthe basic usage of the filter.containThe filter's role is to check whether a target object (which can be a string, array, key-value pair, or structure) contains the specified 'keyword'. Its basic syntax is{{obj|contain:关键词}}.

For example, if you want to check if the text contains the word “AnQiCMS”:

{{"欢迎使用安企CMS(AnQiCMS)内容管理系统"|contain:"AnQiCMS"}}

This code will output directlyTrueBecause it found "AnQiCMS" in the string.

tocontainThe judgment result is stored in a variable

When we need to basecontainthe judgment result further operation, just output directly.TrueorFalseIt is not enough. At this time, the Anqi CMS template engine providessetthe tag comes into play.settags allow us to declare and assign variables in the template, we cancontainThe judgment result of the filter is assigned to this variable.

Here is an example of syntax:

{% set 变量名 = 表达式 %}

CombinecontainYou can store the judgment result of the filter in the variable like this:

{% set content_string = "安企CMS是一款基于Go语言开发的企业级内容管理系统。" %}
{% set has_go_keyword = content_string|contain:"Go语言" %}

Now,has_go_keywordThis variable stores a boolean value. Ifcontent_stringIf it contains “Go language”, thenhas_go_keywordThe value isTrueOtherwise, it isFalse.

Use variables to make complex logical judgments

OncecontainThe judgment result is stored in a variable, and we can combine it with the Anqicms template'sifLogical judgment tag, to implement more refined condition control.

Continuing with the above example:

{% set content_string = "安企CMS是一款基于Go语言开发的企业级内容管理系统。" %}
{% set has_go_keyword = content_string|contain:"Go语言" %}

{% if has_go_keyword %}
    <p>这段内容提到了Go语言,可能是一篇技术类文章。</p>
{% else %}
    <p>这段内容没有提及Go语言,可能是通用性文章。</p>
{% endif %}

This code will be based onhas_go_keywordThe value, selectively displays different paragraphs. If the content contains 'Go language', the first paragraph of text will be displayed; otherwise, the second paragraph will be displayed.

This is just a simpleif-elseExample, you can build a more complex structure according to your actual needs.if-elif-elseOr you can combine this variable with other conditions for judgment.

Application scenario expansion

tocontainThe ability to store results in variables can significantly improve the flexibility and maintainability of templates in various scenarios:

  1. Dynamic content display module:Assuming your article detail page needs to display different sidebar advertisements or recommended content based on whether the content contains specific keywords (such as "product review","new product launch"), you can first usecontainJudge, then store the result in a variable, and use it againifTo control the display of the module.

    {% set article_title = archive.Title %} {# 假设 archive.Title 是当前文章标题 #}
    {% set is_review_article = article_title|contain:"评测" %}
    
    
    {% if is_review_article %}
        <div class="sidebar-promo">
            <h4>最新评测产品推荐</h4>
            {# ... 显示评测相关的推荐内容 ... #}
        </div>
    {% endif %}
    
  2. Filter list data:In a custom list (for examplearchiveListIn the loop, you may want to make additional judgments on each item, such as only displaying documents whose titles do not contain "Expired".

    {% archiveList archives with type="list" limit="10" %}
        {% for item in archives %}
            {% set is_expired = item.Title|contain:"已过期" %}
            {% if not is_expired %}
                <li><a href="{{item.Link}}">{{item.Title}}</a></li>
            {% endif %}
        {% endfor %}
    {% endarchiveList %}
    
  3. Check the existence or specific value of a custom field:AnQi CMS supports custom content model fields. Sometimes, you may need to judge a specific custom field (such asproduct_featuresDoes it exist, or does its value contain a specific attribute?containA filter can be used to check key-value pairs (map) or structures (struct) for the existence of a specific key name.

    {% archiveParams custom_params with id=archive.Id sorted=false %} {# 获取文章的自定义参数 #}
    {% set has_features_field = custom_params|contain:"product_features" %}
    
    
    {% if has_features_field %}
        <p>产品特性:{{custom_params.product_features.Value}}</p>
    {% else %}
        <p>该产品未配置特性信息。</p>
    {% endif %}
    

    here,custom_params|contain:"product_features"It is judging whethercustom_paramsThismapDoes it exist namedproduct_featureskey?

By usingcontainThe judgment result of the filter is stored in the variable, which not only makes the template logic clearer, but also provides a powerful tool for implementing the dynamic and intelligent content display of the safe CMS website.This method avoids repeated judgment logic, improves the reusability and readability of the template, and allows your content operation strategy to be implemented more flexibly.


Frequently Asked Questions (FAQ)

  1. containIs the filter case sensitive?Yes, according to the default string handling mechanism of Go language,containthe filter is usually case sensitive. For example,"AnQiCMS"|contain:"cms"will returnFalseIf you need to perform case-insensitive judgment, you may need to performcontainBefore making a judgment, first convert the target string and keywords to the same case (for example, both to lowercase) and then compare them.

  2. exceptsetTags, there are also other ways to convertcontainDoes the result store in the variable?The AnQi CMS template engine also supportswithtags to define variables, usually used toincludeTags can pass local variables or declare temporary variables within a certain code block. AlthoughwithTags can also be used to storecontainthe result, butsetTags are usually more concise and commonly used for global or local variable assignment in templates. For example:{% with my_result = "string"|contain:"keyword" %}...{% endwith %}.

  3. containCan filters be used to judge numeric or boolean values? containThe filter is mainly used to determine whether a string contains a substring, or whether a specific element or key name exists in an array/slice, key-value pair, or structure.It does not directly determine whether the type of the variable itself is numeric or boolean, nor is it directly used for comparison of numeric or boolean values.ifTags can be used with comparison operators (such as==/>/<To be performed. For example:{% if archive.Views > 1000 %}.