How to split a string containing multiple keywords in AnQiCMS template by spaces, commas, or a custom delimiter into an array?

Calendar 👁️ 77

In AnQiCMS (AnQiCMS) content management and template development, we often encounter scenarios where we need to process strings containing multiple keywords.For example, an article may have a comma-separated list of keywords, or product attributes are space-separated tags.Make full use of these data and display them flexibly in the template, you need to split these strings into an iterable array.

AnQiCMS uses a template engine syntax similar to Django, providing powerful filter functions to handle such needs. Among them,splitThe filter is the core tool to achieve this goal, supplemented byfieldsThe filter handles special cases, as well asreplaceThe filter is used to deal with more complex delimiter scenarios.

UsesplitThe filter performs precise splitting

splitThe filter is your preferred tool, it can split a string into a string array based on any delimiter you specify. Its usage is very intuitive:

{{ 原始字符串|split:"分隔符" }}

For example, suppose you have a keyword string in an article, separated by commas:

{% set keywordString = "AnQiCMS,网站运营,内容管理,模板开发" %}
{% set keywordsArray = keywordString|split:"," %}

{# 现在 keywordsArray 就是一个包含 ["AnQiCMS", "网站运营", "内容管理", "模板开发"] 的数组 #}

If you want to use a space as a delimiter, the operation is just as simple:

{% set tagString = "SEO 优化 CMS 模板" %}
{% set tagsArray = tagString|split:" " %}

{# tagsArray 将是 ["SEO", "优化", "CMS", "模板"] #}

It should be noted that,splitThe filter will default to including whitespace characters on both sides of the delimiter as part of the array elements. If there are extra spaces around the delimiter in your string and you want to remove these spaces, you can first usetrimThe filter processes each element. However, usually, if you use a single delimiter (such as,), the system will handle it intelligently.

Combining strings with multiple delimiters: replaceFilter

In practical applications, the keyword string may not always be so standardized, and may contain a variety of separators such as commas, spaces, and semicolons.For example: Keyword A, Keyword B Keyword C;Keyword D. In this case, you can first usereplaceThe filter replaces all different delimiters with the same one and then usessplitto split.

Here is an example of processing multiple delimiters:

{% set complexString = "教程,模板 开发;SEO 优化" %}

{# 1. 将所有分号替换为逗号 #}
{% set tempString1 = complexString|replace:";","," %}
{# tempString1 现在是 "教程,模板 开发,SEO 优化" #}

{# 2. 将所有空格替换为逗号(注意:如果您的原始字符串中关键词之间本身就可能带空格,但您想保留,则此处需根据实际情况调整替换逻辑) #}
{% set normalizedString = tempString1|replace:" ",", " %}
{# normalizedString 现在是 "教程, 模板, 开发,,SEO, 优化" (这里会因为连续分隔符产生空元素,后续处理时可忽略) #}

{# 3. 最后,使用逗号拆分 #}
{% set finalKeywordsArray = normalizedString|split:"," %}

{# finalKeywordsArray 可能包含空字符串元素,在遍历时需要注意判断或在替换时避免连续分隔符 #}

{# 更好的做法:先将所有分隔符统一成一个,然后用trim去除多余空白,再split。
   如果分隔符是逗号、分号、空格:
   将分号替换为逗号: "教程,模板 开发,SEO 优化"
   将空格替换为逗号: "教程,模板,开发,,SEO,优化" (这里会留下连续的逗号,split后可能出现空字符串)
   更精细的控制,可以统一到单个分隔符,并在 split 前后对字符串进行 trim,确保每个关键词是干净的。
#}

{% set exampleString = "keyword1, keyword2;keyword3 keyword4" %}

{# 统一替换分号为逗号 #}
{% set step1 = exampleString|replace:";","," %} {# "keyword1, keyword2,keyword3 keyword4" #}

{# 统一替换空格为逗号 #}
{% set step2 = step1|replace:" ",", " %} {# "keyword1,, keyword2,,keyword3,,keyword4" (注意这里会在原有的空格处插入额外的逗号,如果关键词之间本身就有空格,这需要更复杂的正则替换) #}

{# 考虑更通用的场景,将所有非字母数字的字符都视为分隔符,并替换为统一的逗号,然后去除连续逗号 #}
{# 注意:AnQiCMS 模板过滤器目前不支持正则表达式替换,所以需要逐个替换 #}

{% set keywordList = "安企CMS, 网站运营; 内容管理 SEO 优化" %}
{% set temp1 = keywordList|replace:";","," %} {# "安企CMS, 网站运营, 内容管理 SEO 优化" #}
{% set temp2 = temp1|replace:" ",", " %} {# "安企CMS,, 网站运营,, 内容管理,, SEO,, 优化" - 这种方式会产生多余逗号甚至空元素 #}

{# 另一种更干净的思路是:先统一替换为单个不常用的分隔符(如 "|"),然后去除多余的,最后再 split #}
{% set rawKeywords = "关键词1, 关键词2 关键词3; 关键词4" %}
{# 将所有分隔符统一替换为单一分隔符(比如 |),并确保前后没有多余空格 #}
{% set unifiedDelimiter = "|" %}
{% set tempProcessed = rawKeywords|replace:","," "|replace:";"," "|replace:"  "," " %}
{# tempProcessed 现在可能是 "关键词1 关键词2 关键词3 关键词4" #}
{% set tempProcessed = tempProcessed|trim %} {# 去除首尾空格 #}
{% set finalKeywordsArray = tempProcessed|split:" " %}
{# finalKeywordsArray 现在是 ["关键词1", "关键词2", "关键词3", "关键词4"] #}

By cleverly combiningreplaceandsplitCan handle most complex string splitting requirements.

Convenient splitting for spaces:fieldsFilter

If you confirm that the keywords in the string are separated only by single or multiple spaces and you do not need to specify other delimiters, thenfieldsThe filter provides a simpler solution.fieldsThe filter will split a line of text into an array according to spaces (including multiple consecutive spaces) and automatically remove empty elements.

{% set spacedKeywords = "AnQiCMS GoLang  CMS SEO" %}
{% set keywordsByFields = spacedKeywords|fields %}

{# keywordsByFields 将是 ["AnQiCMS", "GoLang", "CMS", "SEO"] #}

It issplit:" "more intelligent, capable of handling consecutive spaces.

Character splitting: ``

Related articles

In AnQiCMS template language, what are the similarities and differences between the `join` filter and other string concatenation methods, and what are their applicable scenarios?

In Anqi CMS template language, combining multiple strings or data fragments into a complete string is a very common requirement in front-end display.There are many ways to achieve this goal, each method has its unique application scenarios and advantages.Today, let's delve into the differences and similarities between the `join` filter and other commonly used string concatenation methods.### `join` filter: A bridge from array to string The `join` filter plays a very clear and efficient role in AnQiCMS template language

2025-11-08

How to customize fields in the AnQiCMS backend where a field stores multiple choices (an array), and clearly display them on the frontend page using the `join` filter?

The AnQi CMS provides great convenience for website operators with its flexible content model and powerful custom features.In daily content management, we often encounter situations where we need to add multiple selection properties to articles or products, such as a product may have multiple colors, different sizes, etc.How to display them in a clear and beautiful way on the front-end page when this information is stored in the background through custom fields in the form of multi-select values has become the problem we need to solve.### Understanding the Multi-Select Custom Fields in AnQi CMS On the AnQi CMS backend

2025-11-08

What will happen when the `join` filter encounters an array with mixed data types (such as strings and numbers)?

In Anqi CMS template development, the `join` filter is a very useful tool that can help us concatenate multiple elements of a list (array) into a complete string.This is particularly convenient when it is necessary to display a series of related data in a unified format, such as displaying multiple tags of articles, multiple characteristics of products, or multiple permissions of users.However, in practice, we sometimes encounter situations where the array to be concatenated contains different types of data, such as strings, numbers, and even boolean values. So

2025-11-08

Can the `join` filter in AnQiCMS template handle array elements containing Chinese characters correctly? If so, what are the precautions?

In AnQiCMS template development, the `join` filter is a very practical tool that can concatenate all elements of an array (or slice) with a specified separator to form a single string.For many users concerned, can the `join` filter correctly handle array elements containing Chinese characters? The answer is affirmative.AnQiCMS is developed based on Go language, Go language supports UTF-8 encoding natively, which means it has a natural advantage in handling multi-language characters. Therefore

2025-11-08

The `make_list` filter and the `split` filter are applicable to which scenarios in converting strings to character arrays in AnQiCMS templates?

In AnQiCMS template development, we often need to process string type data, where converting a string to an array is a common requirement.AnQiCMS powerful template engine provides a variety of filters to assist in completing such tasks, among which the `make_list` and `split` filters are the tools for converting strings to arrays.Although they can both 'turn' strings into arrays, there are essential differences in their application scenarios and conversion logic.Understanding these differences can help us achieve template functionality more efficiently and accurately.##

2025-11-08

How to get the actual length of a string or array in AnQiCMS template (number of characters or elements)?

When managing website content in AnQi CMS, you often encounter situations where you need to get the number of characters in text or determine how many elements are in a list or array.In order to control the page layout, ensure the display length of the title introduction, or dynamically adjust the display logic based on the amount of data, it is crucial to understand how to obtain this "length" information in templates for creating flexible and user-friendly websites.The AnQi CMS template engine provides a concise and powerful way to handle such requirements, with the most core being the `length` filter

2025-11-08

How to determine if the length of a variable in the AnQiCMS template matches the expected value and make a judgment in the conditional statement?

In website content management, flexibly controlling the way content is displayed is crucial for improving user experience and page aesthetics.AnQiCMS (AnQiCMS) provides a powerful template engine, allowing us to easily determine how to display page elements based on the characteristics of the content, such as the length of a variable.When you need to judge whether the length of a variable meets the expected requirements and perform different operations in the template based on this, the template tags and filters of Anqi CMS provide an intuitive and efficient solution.Flexible control of content display: The importance of length judgment Imagine one

2025-11-08

How to calculate the total number of times a specific keyword appears in a line string or an array in the AnQiCMS template?

In AnQiCMS (AnQiCMS) template development, we often need to flexibly handle various content on the page.For example, you may need to analyze the frequency of a specific word in an article, or check how many times an element is mentioned in a list.The AnQi CMS powerful template engine provides a variety of practical filters (Filter) that can help you easily meet these needs.The function used to calculate the total number of times a specific keyword or element appears is exactly the focus of our discussion today.### Core Function: `count`

2025-11-08