In website operation, we often encounter such needs: dynamically adjusting the display method of content based on specific information of web page content.For example, if an article title contains keywords such as 'promotion' or 'event', we may want it to automatically display a prominent tag;Or, in order to check content for sensitive words and ensure user experience, it is necessary to check if certain sensitive words exist in the article and handle them accordingly.
The Anqi CMS is a powerful and flexible content management system that provides intuitive template tags and filters, making dynamic judgments and displays very simple.This article will introduce in detail how to judge whether a string contains a certain keyword in the AnQiCMS template, and implement various content operation strategies based on this.
1. Core Solution: UtilizecontainFilter keyword judgment
The most direct and commonly used method in the AnQiCMS template system is to usecontainA filter that can check if a string (or an array, key-value pair, etc.) contains a specified keyword (substring) and return a boolean value (TrueorFalse)
Basic usage:
containThe use 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 an article titlearticle.Titlecontains the word 'Promotion':
{{"我们的AnQiCMS最新功能"|contain:"AnQiCMS"}} {# 这将输出 True #}
{{"这是一篇普通文章"|contain:"促销"}} {# 这将输出 False #}
Combine conditional judgment{% if %}Display result:
due tocontainThe filter returns a boolean value, which is used for conditional judgment tags in the template{% if %}Combined with, it can achieve the display of dynamic content.
Suppose you want to display a 'Hot' corner mark when the article title contains the words 'Hot':
{% 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 it can be reused in multiple locations of the template or to perform more complex logical judgments. At this time, you can use{% set %}.
{% 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,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 the array:
{% set tags = ["CMS", "Go", "内容管理", "企业站"] %} {% if tags|contain:"内容管理" %} <p>文章包含“内容管理”标签。</p> {% endif %} - Check the key name (for a struct or map):
{# 假设 article.Extra 是一个包含自定义字段的 map 或 struct #} {% if article.Extra|contain:"author" %} <p>此文章有作者信息。</p> {% endif %}
Part two: More keyword processing filters
exceptcontain,AnQiCMS also provides other practical filters that can help you process keywords more finely related to the features.
countFilter: Count the number of times keywords appearIf you not only want to know if a keyword exists, but also 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 counting mentions of specific content in multiple places.
indexFilter: Get the position of the first occurrence of the keywordindexThe 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 when handling strings containing Chinese characters,
indexThe filter may count a Chinese character as multiple bytes when calculating positions, which depends on its underlying implementation, so the returned index may not be the character number position you intuitively understand.replaceFilter: Replace keywords in stringsWhen you need to modify keywords in strings,replacethe filter comes into play. 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