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

UsesplitFilter performs precise splitting

splitThe filter is your preferred tool, it can split a string into an array of strings 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 spaces as separators, the operation is just as simple:

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

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

It is worth noting that,splitThe filter will also include the whitespace characters on both sides of the delimiter as part of the array elements. If there are extra spaces before and after the delimiter in your string and you want these spaces to be removed, you can first usetrimFilter processes each element. However, usually, if you use a single delimiter (such as,), the system will handle it intelligently.

strings with multiple delimiters: CombinereplaceFilter

In practical applications, the keyword string may not always be so standardized, and may contain multiple delimiters such as commas, spaces, and semicolons.For example: 'Keyword A, Keyword B Keyword C; Keyword D'.replaceThe filter replaces all different delimiters with the same one and then splits.splitSplitting is performed.

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 needs.

Convenient splitting for spaces:fieldsFilter

If you are sure that the keywords in the string are separated by a single or multiple spaces, and you do not need to specify other delimiters,fieldsthe filter provides a simpler solution.fieldsThe filter will split a line of text into an array by 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 and can handle consecutive spaces.

Character level splitting: `