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 in information.Whether it is for optimizing search engine (SEO), implementing intelligent content recommendations, or performing simple data verification to quickly judge whether a string or array contains a specific keyword, it is a very practical and efficient skill.In the flexible and powerful template system of Anqi CMS, this task becomes easy.Using 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 text, AnQi CMS provides several intuitive ways to help us.

Firstly,containThe filter is your preferred tool to determine if a string contains a specific keyword. It will directly return a boolean value (TrueorFalse

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

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

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

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

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

Sometimes, we not only need to judge if it exists, but also know how many times a certain keyword appears in a line of text. At this point,countIt is especially convenient to have a filter. It calculates the total number of times a specified keyword appears in a line of string.

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

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

Keyword judgment in an array

The content model and tag system of AnQi CMS often store data in an array format, such as the Tag tags of documents, custom multi-select fields, etc.In these cases, we also need to determine whether the array contains a specific element.

Fortunately,containThe filter is also applicable to array types, it can help you quickly determine if 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 was originally a string but you want to split it into an array of words for judgment, for example, a field stores multiple keywords separated by commas.splitFilter by delimiter ormake_listFilter by character can help you first convert the string to an array, and then applycontainto check.

{# 假设 `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, corresponding to theifWhen used in combination, logical tags can play a powerful role, enabling your website to achieve more intelligent and dynamic display effects.

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 for 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 usesettags tocontain