When building and managing website content, we often encounter such needs: to decide whether to display a "hot" tag based on whether the article title contains a certain keyword; or to adjust the display style by checking if a certain feature is mentioned in the product description.These seemingly subtle dynamic adjustments can greatly enhance the intelligence and user experience of the website.In AnQiCMS, due to its flexible Django template engine syntax, implementing such a judgment is not complicated.

AnQiCMS's template system provides rich filters and logical tags, allowing content operators to implement complex logical judgments at the front-end template level without delving into the backend code.Today, let's talk about how to judge whether a string or an array contains a specific keyword in AnQiCMS template and display content intelligently according to the result.

core tools:containFilter

AnQiCMS's template engine provides a very practicalcontainFilter.This filter helps us quickly determine whether a string contains a specific word or if an array includes a certain element.TrueorFalsesuch a boolean value, which is convenient for us to make subsequent logical judgments.

For example, if you want to check if a piece of text contains the word 'CMS', you can write it directly like this:

{{ "欢迎使用安企CMS(AnQiCMS)"|contain:"CMS" }}

After this code is executed, it will output directly.True.

If your data is an array, such as a list of article tags, and you want to know if it contains the tag 'SEO optimization', you can first convert the comma-separated string into an array, then usecontainThe filter makes the following judgment:

{% set tags_string = "安企CMS,SEO优化,多站点管理" %}
{% set tags_array = tags_string|split:"," %} {# 使用 split 过滤器将字符串按逗号分隔成数组 #}
{{ tags_array|contain:"SEO优化" }}

This will also be outputTrue.

Even,containThe filter can also be used to check if a key exists in a key-value pair (map) or a structure (struct). For example, if you have an object storing product information and want to know if it contains the key “version”:

{% set product_info = {name: "AnQiCMS", version: "3.0"} %}
{{ product_info|contain:"version" }}

This will also be outputTrue.

CombineifTag, intelligent display content

It is obviously not enough to know True or False, we also need to take action based on this result. At this time, the template ofifThe logical judgment tag comes into play. We can use.containThe result of the filter directly in.ifthe statement to achieve conditional display of content.

Imagine, you want to add an eye-catching "New Arrival" corner label to all articles with the title containing "New Product". You can achieve this in the article list template.

{% archiveList articles with type="list" limit="5" %}
    {% for item in articles %}
        <li>
            {% if item.Title|contain:"新品" %}
                <span style="color: red; font-weight: bold;">【新品上市】</span>
            {% endif %}
            <a href="{{item.Link}}">{{item.Title}}</a>
            <p>{{item.Description|truncatechars:100}}</p>
        </li>
    {% empty %}
        <p>暂时没有文章。</p>
    {% endfor %}
{% endarchiveList %}

So, only when the article titleitem.Titlecontains "New Item", the red "" text will be displayed.

If your judgment conditions are more complex, you need to check multiple keywords at the same time, or one of them is sufficient,ifTagsandandorThe logical operators come into play. For example, when a description of an article mentions both "Go language" and "SEO optimization" at the same time, it displays a specific prompt:

{% set article_desc = "安企CMS是基于Go语言开发的系统,支持多站点和强大的SEO优化功能。" %}
{% if article_desc|contain:"Go语言" and article_desc|contain:"SEO优化" %}
    <p>这篇文章深度探讨了Go语言和SEO优化。</p>
{% elif article_desc|contain:"多站点" %}
    <p>这篇文章介绍了多站点管理特性。</p>
{% else %}
    <p>文章内容普通。</p>
{% endif %}

More practical filter aids judgment and display

Exceptcontain之外,AnQiCMS template also provides some other filters related to keyword judgment and display, which can help in different scenarios.

  • countFilter: Count keyword occurrencesSometimes, we don't just want to know if a keyword “exists” or “not”, but also how many times it appears.countThe filter can help you achieve this.

    {% set article_content = "安企CMS,高效的CMS,值得信赖的CMS,让你的CMS运营更轻松。" %}
    <p>"CMS" 在文章中出现了 {{ article_content|count:"CMS" }} 次。</p>
    

    This will output:"CMS" 在文章中出现了 3 次。

  • indexFilter: Get the position of the first occurrence of the keywordIf the position of the keyword is important for displaying your contentindexThe filter can return the starting position of the first occurrence of the keyword. If not found, it returns-1.

    {% set intro_text = "AnQiCMS是领先的CMS内容管理系统。" %}
    {% set cms_position = intro_text|index:"CMS" %}
    {% if cms_position != -1 %}
        <p>"CMS" 首次出现在文本的第 {{ cms_position }} 个位置。</p>
    {% else %}
        <p>未找到"CMS"。</p>
    {% endif %}
    

    Please note that Chinese characters may occupy multiple bytes when calculating positions, which may lead to differences from expectations. It is usually more accurate in English scenarios.

  • splitFilter: Split the string into an arrayAs shown in the previous array example, when your data source is a comma-separated, space-separated, or other symbol-separated string, but you need to treat it as an array.splitThe filter can help you. It can split a string into an array of strings according to the specified delimiter.

    `twig {% set product_features_str = “High performance, Modular design, SEO friendly” %} {% set product_features_array = product %}