How to determine if a string or array contains a specific keyword?

Calendar 👁️ 66

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

Related articles

How to center or align a string to a specified length?

When building website content, we often need to fine-tune the text layout to ensure that the information presented is both beautiful and clear.Whether it is a list, table, product parameters, or other structured display data, maintaining visual alignment and consistency is crucial for improving user experience.Aq CMS, known for its high flexibility and ease of use as a content management system, deeply understands the importance of content presentation and has built a series of powerful template filters to help us easily align and center strings

2025-11-09

How to format a floating-point number to a specified number of decimal places?

How to accurately control the decimal places of floating-point numbers in AnQiCMS?In website content operations, we often encounter situations where we need to display prices, statistical data, measurement results, and other floating-point numbers.However, the original floating-point numbers often have unnecessary decimal places, which not only affects the appearance but may also reduce the readability of the data.AnQiCMS fully considers this user's needs, providing flexible and powerful template filters to help us easily implement the formatting of floating-point numbers, making data display more professional and accurate.### Use `floatformat`

2025-11-09

How to get the length of a string, array, or object?

When managing website content in AnQi CMS, we often need to flexibly adjust the page display according to the number of elements in a string, array, or collection.Whether it is to judge whether the length of the article title needs to be truncated, or to count how many documents are under a certain category, or to display the number of user comments, obtaining the 'length' of this content is the key to dynamic display and logical judgment.The AnQi CMS template engine provides built-in features that are simple and efficient, helping us easily deal with these scenarios.Among them, the `length` filter is a powerful assistant for obtaining the length of strings, arrays, or key-value pairs

2025-11-09

How to display a default value when a variable is empty?

In website operation, we often encounter such situations: a content field may not be filled in for various reasons, such as the author of the article, image description, or specific parameters of a product.If the template outputs a blank directly when displaying this content, it not only affects the aesthetics but may also confuse the user.At this time, how to display a friendly default value when a variable is empty has become a very practical skill.The AnQi CMS template system, with its flexible and powerful features, provides us with various methods to elegantly solve this problem

2025-11-09

How to calculate the number of times a specific keyword appears in a string or array?

In AnQiCMS content management practice, we often need to carry out refined analysis and optimization of website content.In which, calculating the number of times a specific keyword appears in an article, title, or dataset is a basic and important requirement.In order to evaluate SEO keyword density, analyze content quality, or conduct data statistics, AnQiCMS provides convenient and efficient solutions.This article will deeply explore how to utilize the built-in features of AnQiCMS to easily achieve this goal.### Core Tool: `count`

2025-11-09

How to remove specific characters or HTML tags from a string?

In the daily content operation of AnQiCMS, we sometimes encounter situations where we need to finely process the content, such as removing specific characters, extra spaces, or stripping unnecessary HTML tags from the content.These operations are very critical for ensuring the neatness of content display, adapting to different front-end styles, and even for data cleaning.The AnQi CMS provides powerful template filter functions to help us efficiently complete these tasks.###

2025-11-09

How to split a string into an array with a specified separator?

In the daily operation of AnQi CMS, we often encounter the need to process some data stored as strings but actually containing multiple values.For example, an article may store multiple keywords in a comma-separated manner, or a custom content field may need to support multiple options, and these options are ultimately connected into a string with a specific symbol.In this case, we often need to split these strings using the preset delimiter into independent segments so that they can be displayed flexibly on the page or further processed.AnQi CMS based on the efficient architecture of Go language

2025-11-09

How to concatenate array elements into a string with a specified separator?

When operating a website, we often encounter the need to display a set of related data in the form of a list, such as multiple tags for articles, multiple keywords for product features, or multiple image URLs in an image library.At this time, if these scattered data elements can be integrated into a concise string and separated by specific symbols, it will greatly enhance the display effect and readability of the content.The Anqi CMS template engine provides a very practical `join` filter, which can easily meet this requirement.Understanding the `join` filter `join`

2025-11-09