In website operation, we often encounter such needs: to decide how to display page elements based on whether the content contains a specific word, such as highlighting certain text or displaying a module only when certain conditions are met.The template system of AnQiCMS provides flexible filter functions to help us easily implement these keyword-based judgments.This article will delve into how to efficiently and accurately judge whether a line of text contains a certain keyword in the AnQiCMS template.
UsecontainKeyword judgment by filter
The most direct and recommended method is to usecontainFilter.This filter is specifically used to determine if a string, array (slice), even a key-value pair (map) or a struct contains the specified keyword or key name.TrueorFalse),非常适合与模板的条件判断{% if %}auto tag to use.
Suppose we have a variablepageContent存储着网页的主要内容,我们想知道这段内容是否提及了“安企CMS”这个词。我们可以这样使用containFilter:
{% set pageContent = "欢迎使用安企CMS(AnQiCMS),这是一款强大的内容管理系统。" %}
{% if pageContent|contain:"安企CMS" %}
<p>恭喜!内容中包含关键词“安企CMS”!</p>
{% else %}
<p>抱歉,内容中未找到关键词“安企CMS”。</p>
{% endif %}
In the above example, ifpageContentThe string contains "AnQi CMScontainThe power of the filter lies in its ability not only to judge strings, but also to support multiple data types.
1. Determine if the string contains a keyword:This is the most common usage, for example, to check articles.