As an experienced website operations expert, I know that flexible template application is the key to improving website efficiency and user experience.AnQiCMS (AnQiCMS) with its efficient architecture based on the Go language and support for Django template engine syntax, provides us with powerful content customization capabilities.Today, let's delve deeply into how to elegantly check if the content in the AnQiCMS template contains specific keywords, which is what you mentioned, namely the word "{% if "关键词" in 变量 %}The implementation of this judgment method.


The content of the Anqi CMS template includes judgment: master.containThe wonder of filters

In the actual operation of website, we often encounter such needs: according to whether the content of the article, title, or custom field contains a specific keyword, to decide the display method on the page, recommended content, or even trigger specific interaction logic. For example, we may want:

  • If the article title includes the words 'Promotion', then add an eye-catching 'Hot Sale' label.
  • If the product description mentions 'Time-limited Offer', then display a countdown module.
  • Recommend related in-depth readings based on whether a certain industry term is included in the keyword list of the article.

The core of these scenarios revolves around a question: How to accurately and efficiently judge whether a variable (whether it is a string, array, or object) contains the keywords we are concerned about in the AnQiCMS template?

The AnQi CMS template system is similar to Django in syntax, but it extends the variable processing capabilities by providing a set of rich and practical filters. For checking content inclusion relationships, we cannot directly use Python-styleinAn operator, but it should be skillfully utilized built-in AnQiCMScontainFilter.

UnderstandingcontainThe core function of the filter

containThe filter is a Swiss Army knife specifically designed in the AnQiCMS template engine for checking content inclusion relationships.Its function is straightforward and powerful: to determine whether a "keyword" is contained within a line of string, an element of an array (slice), or as a key name existing in a key-value pair (map) or a struct.This filter will return a boolean value (TrueorFalseThis is what we need for conditional judgment.

Its syntax is very concise, usually like this:

{{ 变量 | contain:"关键词" }}

Or, when we need to{% if %}It will perfectly integrate into the conditional judgment:

{% if 变量 | contain:"关键词" %}
    <!-- 如果变量包含关键词,这里的内容就会显示 -->
{% endif %}

Next, let's take a look at several common application scenarioscontainHow the filter shines in the AnQiCMS template.

Practical exercise:containMultiple applications of filters

Scenario one: Check if the article title or content contains specific words

Suppose we are developing an article detail page, hoping to use the article title (archive.TitleDoes it contain 'AnQiCMS' to display a special prompt message.

{% archiveDetail archiveContent with name="Title" %}
    <h1 class="article-title">{{ archiveContent }}</h1>
    {% if archiveContent|contain:"AnQiCMS" %}
        <p class="highlight-info">✨ 这篇文章特别推荐,因为它提到了强大的 AnQiCMS!</p>
    {% endif %}
{% endarchiveDetail %}

Similarly, if we want to check the main content of the article (archive.ContentWhether it contains a certain word. Since the article content is usually rich text, it may contain HTML tags, in order to ensurecontainThe filter processes plain text or the text we hope for, and can be correctly parsed when outputting HTML, we may need to combine other filters such asstriptagsRemove HTML, or use it directly inarchive.Contentand use it directlysafeFilter:

{% archiveDetail article with name="Content" %}
    <div class="article-content">
        {{ article|safe }} {# 输出文章内容,并确保HTML被解析 #}
        {% if article|striptags|contain:"GoLang" %}
            <p class="tech-note">🔍 文章内容中包含 GoLang 相关技术讨论。</p>
        {% endif %}
    </div>
{% endarchiveDetail %}

Here, article|striptagsFirst remove the HTML tags, then perform keyword extractionGoLangThe judgment, so as to avoid the keywords being separated by HTML tags and unable to be matched.

Scenario two: Determine the attributes of the article based on the article keywords (Keywords)

Our AnQi CMS allows us to set keywords for articles (archive.Keywords), and these keywords are usually stored as a comma-separated string. We can utilizecontainTo determine whether an article belongs to a specific topic.

{% archiveDetail articleKeywords with name="Keywords" %}
    {% if articleKeywords|contain:"SEO优化" %}
        <div class="seo-badge">🚀 SEO优化必读!</div>
    {% elif articleKeywords|contain:"内容营销" %}
        <div class="marketing-badge">💡 内容营销策略精选!</div>
    {% endif %}
{% endarchiveDetail %}

In this example,articleKeywordsIt is a string,containThe filter will directly search for "SEO optimization" or "content marketing" in this string.

If a field returns an array (for example, we usesplita filter to convert comma-separated strings into an array),containthis also applies. For example, if we have a virtualcategory_tagsArray:

`twig {% set category_tags =