How to use the `split` filter to extract a tag array from the article content in a specific citation format (such as `[tag1][tag2]`)?

Calendar 👁️ 71

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

Related articles

The `split` filter has what typical applications when processing user submitted form data (such as the values of checkboxes)?

The AnQi CMS provides great convenience for managing website content with its flexible content model and powerful template system.In daily operations, we often encounter the need to process form data submitted by users, especially those fields that allow for multiple selections, such as product specifications, article tags, or user interests.These data are usually stored as a string in the background, for example,

2025-11-08

The `split` filter splits an array, if you need to format each element (such as `upper` or `lower`), how to implement chaining?

In Anqi CMS template development, flexibly using various filters (Filters) can greatly facilitate our formatting of content.When we encounter the need to split a string into multiple parts by a specific delimiter, and then to further format each part (that is, each element of the array) for example, to convert them all to uppercase or lowercase, we cannot perform the chaining operation as simply as we do with a single string.The AnQi CMS template engine supports syntax similar to Django, it provides a powerful `split` filter

2025-11-08

How to pass the array elements split by the `split` filter as parameters to other template tags or filters?

The template system of AnQiCMS (AnQiCMS) is favored by content operators for its powerful flexibility and ease of use.Among them, the `split` filter is a very practical feature that can help us split a string into an array (or called a slice in Go language) according to the specified delimiter.However, when we want to pass a specific part of this array as a parameter to other template tags or filters, we may feel a bit confused.Don't worry, this article will discuss in detail several efficient and commonly used methods to solve this problem.--- ###

2025-11-08

Does the `split` filter support more complex delimiter patterns, such as matching specific prefix and suffix characters?

In AnQiCMS template development, the `split` filter is a very useful tool that can help us split strings into arrays according to specified delimiters, which is especially convenient for handling data connected by specific characters.However, when it comes to more complex delimiter patterns, such as when it is necessary to match specific prefixes and suffixes of characters, the capabilities of the `split` filter are worth exploring in depth.

2025-11-08

Does the `split` filter support case-sensitive delimiters for the string it processes?

In Anqi CMS template creation, we often need to process strings in various ways, where the `split` filter is a very practical tool that helps us split a long string into multiple parts according to the specified delimiter.However, a common issue when using this filter is: whether it distinguishes between uppercase and lowercase when handling delimiters?Let's delve deeper into this issue. ### The working principle of the `split` filter Firstly

2025-11-08

If you need to limit the maximum length of the array split by the `split` filter, is there a built-in parameter or method?

During the template development process of AnQi CMS, the `split` filter is a very practical tool that can help us conveniently split strings into arrays according to the specified delimiter, which is particularly important in various scenarios such as tag lists and keyword strings.However, some users may want to directly limit the maximum length of the array generated after using the `split` filter. Then, does the `split` filter built into AnQi CMS provide such parameters or methods?A thorough understanding of the system functions

2025-11-08

Which `split` filter is more suitable for handling irregularly spaced data in user input compared to the `fields` filter?

In the daily content operation of AnQi CMS, we often encounter situations where we need to handle user input data.This data may be a sequence of keywords, an item in a list, or other text that needs to be split in a specific way.Among them, data separated by spaces is particularly common, but users' input habits are often not standardized, with excessive spaces, tabs, and even newline characters mixed in.At this time, the powerful template filter provided by AnQiCMS comes into play.

2025-11-08

The `split` filter splits array elements. If a numerical operation needs to be performed, should the type conversion be done first?

In AnQi CMS template development, flexible data handling is the key to building rich pages.The `split` filter is undoubtedly a powerful tool for processing string data, it can split a string into an array based on a specified delimiter.However, when the elements cut out of the array are essentially numbers and need to perform arithmetic operations such as addition, subtraction, multiplication, and division, a common problem arises: do these elements need to be explicitly typecast?Today, let's delve deep into this issue.

2025-11-08