When managing content in AnQi CMS, we often need to structure specific information in the article for front-end display or further data analysis. The article content may contain some references marked in a specific format, such as tags used to identify related topics, which are followed by[tag1][tag2]This form appears. How to effectively extract such seemingly continuous strings into independent tag arrays is a practical problem that many operators may encounter.

AnQi CMS with its high-performance architecture based on Go language and flexible Django-like template engine provides us with powerful content processing capabilities. Among them, the 'filters' in the template are like a Swiss Army knife, which can perform various fine operations on variables, and today we are going to discuss thesplitThe filter is the key to solving the above problems.

Understanding the problem: Extracting tags from a specific format.

Assuming our article content or some custom field contains a description of article tags, the format is very irregular, like this: [AnQiCMS教程][模板开发][Go语言][内容运营]Our goal is to convert this string into an array, like["AnQiCMS教程", "模板开发", "Go语言", "内容运营"]So that we can display these tags in a loop on the front-end or make further logical judgments based on these tags.

directlysplitThe filter may encounter some challenges because there is no unified, simple delimiter (such as a comma or space) to directly separate each tag. Each tag is enclosed in square brackets[]Wrap, and there are no additional separators between the tags. This requires us to preprocess the original string first.

The power of filters:splitwithreplaceCombination of applications

The AnQi CMS template engine supports chained filter calls, which means we can chain multiple filters together to perform a series of transformations. In order to[tag1][tag2]This format needs to be converted into a label array, we needsplitFilter, but more importantly, we also needreplaceFilter to "clean" strings first.

  1. replaceFilter: Replace specific characters replaceThe filter can help us replace a specific keyword in a string with another keyword. Its basic usage is{{ obj|replace:"old,new" }}of whicholdis the string to be replaced,newis the replaced string.

  2. splitFilter: Split into an array by delimiter splitThe filter can split a string into an array by a specified delimiter. Its basic usage is{{ obj|split:"分隔符" }}If the delimiter does not exist, it will return an array containing the original string.

By cleverly combining these two filters, we can solve our problem.

Step to extract the label array

Let's take the article detail page as an example, assuming we have a custom fieldarchive.CustomTagswhich stores[AnQiCMS教程][模板开发][Go语言][内容运营]such strings.

First step: connect the labels.][Replace with a unified separator

Since the tags are][connected, we can first replace this separator with an uncommon one that can be used as a subsequentsplitdelimiter, such as a comma,.

{% set temp_string_1 = archive.CustomTags|replace:"][","," %}

at this point,temp_string_1The value will become[AnQiCMS教程,模板开发,Go语言,内容运营].

Step two: Remove the brackets at both ends of the string[and]

Now there are brackets at both ends of the string, which are not part of the tag, and they need to be removed. We can use it again.replaceThe filter, to[and]Replace with an empty string.

{% set temp_string_2 = temp_string_1|replace:"[,", "" %}
{% set cleaned_tags_string = temp_string_2|replace:"],", "" %}

After this step,cleaned_tags_stringthe value becomesAnQiCMS教程,模板开发,Go语言,内容运营.

Third step: usesplitThe filter splits the string into an array

Now we have a comma-separated pure tag string that can be easily usedsplitto convert it into an array with a filter.

{% set tags_array = cleaned_tags_string|split:"," %}

Until now,tags_arrayis an array that includes all tags, such as["AnQiCMS教程", "模板开发", "Go语言", "内容运营"].

a complete template code example

Integrate the above steps into the AnQi CMS template, you can do it like this:

"twig {# Assume archive.CustomTags stores "[AnQiCMS Tutorial][Template Development][Go Language][Content Operation]" #}

{% if archive.CustomTags %}

{# 步骤一:将标签间的 "][" 替换为 "," #}
{% set step1_string = archive.CustomTags|replace:"][","," %}

{# 步骤二:移除字符串开头的 "[" #}
{% set step2_string = step1_string|replace:"[,", "" %}

{# 步骤三:移除字符串结尾的 "]" #}
{% set cleaned_tags_string = step2_string|replace:"],", "" %}

{# 步骤四:使用 "," 分隔符将清洗后的字符串切割成数组 #}
{% set tags_array = cleaned_tags_string|split:"," %}

{# 循环遍历并显示提取出的标签 #}
<div class="article-tags">
    <strong>相关标签:</strong>
    {% for tag_name in tags_array %}
        {# 这里可以根据实际需求,将标签渲染为链接,例如链接到该标签的文章列表页 #}
        <a href="/tag/{{ tag_name|urlencode }}" class="tag-item">{{ tag_name }}</a>
    {% empty %}
        <span>暂无相关标签。</span>
    {% endfor %}