In AnQi CMS, efficiently identify key information in content: string and array keyword judgment skills

As content operators, we often need to handle a large amount of text data, ranging from article titles, content descriptions to custom fields, rich and diverse information.In order to optimize search engines (SEO), implement content intelligent recommendation, or perform simple data verification, quickly judge whether a string or array contains a specific keyword is a very practical and efficient skill.In AnQi CMS's flexible and powerful template system, this task becomes easy.With built-in filters, we can easily achieve this goal without writing complex backend code, making your website content management more intuitive.

Keyword search in strings

When we need to check if a word exists in a piece of text, Anqi CMS provides several intuitive ways to help us.

first, containThe filter is your preferred tool for determining if a string contains specific keywords. It will directly return a boolean value (TrueorFalse), clearly tells you the result. This is very convenient in many scenarios where simple 'yes' or 'no' judgments are needed, such as determining whether an article title contains 'AnQi CMS' or whether a description mentions a brand word.

{# 假设有一个字符串变量 `articleTitle` 为 "欢迎使用安企CMS(AnQiCMS)" #}
{{ articleTitle|contain:"CMS" }}
{# 显示结果:True #}

{# 如果要判断是否包含 "建站",结果将是: #}
{{ articleTitle|contain:"建站" }}
{# 显示结果:False #}

If you need to know the first occurrence position of the keyword in addition to checking for its existence, thenindexThe filter comes into play. It returns the starting index of the keyword in the string (starting from 0), or -1 if the string does not contain the keyword.This is especially useful in scenarios where further processing or analysis of keyword positions is needed.

{# 同样使用 `articleTitle` 为 "欢迎使用安企CMS(AnQiCMS)" #}
{{ articleTitle|index:"CMS" }}
{# 显示结果:18 (这里的计算基于UTF-8字符长度,中文按3个字符计算,英文按1个计算,因此实际显示的字符位置可能与视觉上的位置略有差异)#}

{# 如果查找不存在的关键词 #}
{{ articleTitle|index:"系统" }}
{# 显示结果:-1 #}

Sometimes, we not only need to judge whether it exists, but also know how many times a certain keyword appears in a line of text. At this time,countThe filter is particularly convenient. It calculates the total number of times a specified keyword appears in a line of text.

{# 假设 `articleContent` 包含 "安企CMS是一个功能强大的CMS系统,安企CMS..." #}
{{ articleContent|count:"CMS" }}
{# 显示结果:2 #}

{# 如果关键词不存在,则出现次数为0 #}
{{ articleContent|count:"Go语言" }}
{# 显示结果:0 #}

Keyword judgment in an array.

The Anqi CMS content model and tag system often stores data in array form, such as the Tag tags of documents, custom multiple-choice fields, etc.In these cases, we also need to determine whether the array contains a specific element.

Fortunately,containThe filter can also be applied to array types, it can help you quickly determine whether an array contains a specific value.This is very useful for scenarios such as dynamically displaying content based on user-selected tags, or checking whether a product belongs to a certain feature category.

{# 假设 `productTags` 是一个数组,例如 ["企业建站", "SEO优化", "多站点管理"] #}
{% set productTags = '["企业建站", "SEO优化", "多站点管理"]'|list %}
{{ productTags|contain:"SEO优化" }}
{# 显示结果:True #}

{# 判断数组中是否存在 "营销推广" #}
{{ productTags|contain:"营销推广" }}
{# 显示结果:False #}

If your data is originally a string but you want to split it into an array by words before judgment, for example, a field stores multiple keywords separated by commas,splitFilter (by specified delimiter) ormake_listThe filter (by character) can help you first convert the string to an array, and then applycontainCheck.

{# 假设 `keywordsString` 为 "Go语言,CMS,企业站" #}
{% set keywordsString = "Go语言,CMS,企业站" %}
{% set keywordsArray = keywordsString|split:"," %}
{{ keywordsArray|contain:"CMS" }}
{# 显示结果:True #}

Apply the judgment result to the template logic

These filters return boolean values, with the Anqi CMS template inifWhen combined, logical tags can bring powerful functions, making your website more intelligent and dynamic in display.

For example, you may want to display a special icon or style when the article title contains a certain word, or add an eye-catching "recommended" tag to specific content.

{# 假设当前文档的标题是 "安企CMS:高效企业建站解决方案" #}
{% set articleTitle = archive.Title %}
{% set isHotTopic = articleTitle|contain:"安企CMS" %}

{% if isHotTopic %}
    <span class="badge badge-primary">推荐阅读</span>
{% else %}
    <span class="badge badge-secondary">最新发布</span>
{% endif %}
{# 如果 articleTitle 包含 "安企CMS",则显示“推荐阅读”,否则显示“最新发布” #}

To make the template code clearer and easier to maintain, you can usesetLabels willcontain