In managing content in AnQi CMS, we often need to structure specific information within articles for front-end display or further data analysis. The article content may contain some references marked with specific formats, such as tags used to identify related topics, which are written in[tag1][tag2]This form appears. How to effectively extract independent tags from this kind of seemingly continuous string is a practical problem that many operators may encounter.

AnQi CMS provides powerful content processing capabilities with its high-performance architecture based on Go language and flexible Django-like template engine. Among them, the 'filters' in the templates are like a Swiss Army knife, capable of performing various fine operations on variables, and today we will discusssplitFilter, it is the key to solving the above problems.

Understand the problem: Extract tags from a specific format

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

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

The power of the filter:splitWithreplaceCombined use

The template engine of AnQi CMS supports chained calls of filters, 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 to a label array, we needsplitFilter, but more importantly, we also needreplaceFilter to "clean" the string 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" }}where,oldis the string that needs to be replacednewis the string after replacement.

  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 is not present, it will return an array containing the original string.

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

Extract the steps of the tag array

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

Step 1: Connect the tags][Replace with a unified delimiter

Since the labels are][connected, we can first replace this connection symbol with an uncommon one that can also serve as a subsequentsplitdelimiter for operations, such as a comma,.

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

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

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

The brackets at both ends of the string are still there, and they are not part of the tag. They need to be removed. We can use it againreplaceFilter, which[and]replaces it 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语言,内容运营.

Step 3: UsesplitFilter 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:"," %}

By now,tags_arrayit is an array containing all the tags, like["AnQiCMS教程", "模板开发", "Go语言", "内容运营"].

Complete template code example

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

English

{% 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 %}