English CMS as a powerful and flexible content management system, provides intuitive template tags and filters, making dynamic judgment and display very simple.This article will introduce in detail how to judge whether a string contains a certain keyword in AnQiCMS template, and based on this, to implement a variety of content operation strategies.


1. Core Solution: UsecontainFilter judgment keywords

The most direct and commonly used method in the AnQiCMS template system is to usecontainFilter. This filter can check if a string (or array, key-value pair, etc.) contains the specified keyword (substring), and return a boolean value (TrueorFalse).

Basic usage:

containThe usage of the filter is very intuitive, it accepts a keyword as a parameter and applies it to the target string.

{{ 目标字符串|contain:"关键词" }}

For example, to determine if a title of an articlearticle.Titlecontains the word 'Promotion':

{{"我们的AnQiCMS最新功能"|contain:"AnQiCMS"}} {# 这将输出 True #}
{{"这是一篇普通文章"|contain:"促销"}} {# 这将输出 False #}

Combine condition judgment:{% if %}Show results:

Due tocontainThe filter returns a boolean value, which corresponds to the condition judgment tag in the template{% if %}Combined use, it can achieve dynamic content display.

If you want to display a "Hot" corner mark when the title of the article contains the words "Hot", do as follows:

{% if article.Title|contain:"热门" %}
    <span class="badge badge-hot">热门</span>
{% endif %}
<h2 class="article-title">{{ article.Title }}</h2>

Store the judgment result in a variable{% set %}:

In some cases, you may need to store the judgment result in a temporary variable so that you can reuse this result in multiple locations of the template or perform more complex logical judgments.{% set %}Label.

{% set isSpecialOffer = article.Description|contain:"限时优惠" %}

{% if isSpecialOffer %}
    <p class="promo-message">🎉 限时优惠中,抓住机会!</p>
{% endif %}

<div class="product-description">
    {{ article.Description }}
</div>

containFilter support for arrays and key-value pairs:

It is worth mentioning that,containThe filter is not limited to strings. It can also be used to check if a value exists in an array, or if a key exists in a key-value pair (map/struct).

  • Check array:
    
    {% set tags = ["CMS", "Go", "内容管理", "企业站"] %}
    {% if tags|contain:"内容管理" %}
        <p>文章包含“内容管理”标签。</p>
    {% endif %}
    
  • Check key names (for struct or map):
    
    {# 假设 article.Extra 是一个包含自定义字段的 map 或 struct #}
    {% if article.Extra|contain:"author" %}
        <p>此文章有作者信息。</p>
    {% endif %}
    

Part two: More keyword processing filters

ExceptcontainAnQiCMS also provides other practical filters that can help you handle the related functions with keywords more finely.

  1. countFilter: Count the number of times the keyword appearsIf you not only want to know if a keyword exists, but also want to know how many times it appears,countThe filter will be very useful.

    {% set keywordCount = archive.Content|count:"安企CMS" %}
    {% if keywordCount > 0 %}
        <p>文章内容中“安企CMS”一词出现了 {{ keywordCount }} 次。</p>
    {% endif %}
    

    this is very convenient for analyzing keyword density, or for statistical purposes when a specific content is mentioned in multiple places.

  2. indexFilter: Get the position of the first occurrence of the keyword indexThe filter returns the starting position (index) of the first occurrence of the keyword in the string. If the keyword does not exist, it returns -1.

    {% set firstOccurrence = article.Title|index:"活动" %}
    {% if firstOccurrence != -1 %}
        <p>“活动”一词在标题中首次出现在第 {{ firstOccurrence }} 个位置。</p>
    {% else %}
        <p>标题中不包含“活动”一词。</p>
    {% endif %}
    

    Please note that when handling strings containing Chinese characters,indexThe filter may count a Chinese character as multiple bytes when calculating positions, depending on its underlying implementation, so the returned index may not be the character position you intuitively understand.

  3. replaceFilter: Replace keywords in stringsWhen you need to modify keywords in strings,replacethe filter comes in handy. It can replace old keywords with new content.

    {% set originalTitle = "安企CMS:高效内容管理" %}
    {% set newTitle = originalTitle|replace:"安企CMS,AnQiCMS" %}
    <p>原标题:{{ originalTitle }}</p>
    <p>新标题:{{ newTitle }}</p>
    

    This feature is very useful in scenarios such as sensitive word filtering, keyword highlighting, and brand name unification. For example, to highlight a specific word, you can replace it with a version containing HTML tags:

    `twig