In the template development of AnQiCMS,splitA filter is a very useful tool that helps us split strings into arrays according to specified delimiters. This is particularly convenient for processing data connected by specific characters. However, when it comes to more complex delimiter patterns, such as matching specific prefixes and suffixes, splitThe ability range of the filter is worth delving into.

splitBasic usage of the filter

splitThe main function of the filter is to split a string into an array of strings according to the specified delimiter.It is used in a simple and intuitive manner, usually for handling data composed of single, fixed characters or concatenated strings."AnQiCMS,GoLang,CMS"You can easily do this throughsplitwhere the filter splits it into["AnQiCMS", "GoLang", "CMS"]Such an array.

Specifically, its basic syntax is{{ obj|split:"分隔符" }}.objIt is the source string you need to process, while"分隔符"is the fixed text you want to use to split the string.

For example, suppose you have a variabletagStringThe value of"文章,产品,分类"You can split it into an array like this:

{% set tagString = "文章,产品,分类" %}
{% set tagArray = tagString|split:"," %}
{# tagArray 的值为 ["文章", "产品", "分类"] #}

If your delimiter is a string containing spaces, such as", "(Comma followed by a space),splitThe filter can also match exactly:

{% set dataString = "Hello, 99, 3.140000, good" %}
{% set dataArray = dataString|split:", " %}
{# dataArray 的值为 ["Hello", "99", "3.140000", "good"] #}

In addition,splitThe filter has some special behaviors: If the provided delimiter is not found in the source string, it returns an array containing the original string itself. And when the delimiter is set to an empty string""whensplitIt will be very intelligent to split each UTF-8 character of the source string into an element of an array.

For example:

{% set charString = "你好" %}
{% set charArray = charString|split:"" %}
{# charArray 的值为 ["你", "好"] #}

In AnQiCMS, besides that,split, there is also a very convenientmake_listFilter, which can also split strings into arrays by characters, with functionality similar tosplitWhen using an empty string as a delimiter, it is similar to, but emphasizes processing by character list more:

{% set text = "AnQiCMS" %}
{% set charList = text|make_list %}
{# charList 的值为 ["A", "n", "Q", "i", "C", "M", "S"] #}

splitSupport for delimiter patterns in the filter

splitThe filter in AnQiCMS template was initially designed to handle relatively simple and direct string splitting tasks. It supports the delimiter patternsFixed string matching。This means, the delimiter you providesplitis treated as a complete, literal string by the system for matching. It will look for the delimiter you provide in the stringcompletely consistentEnglish substring, and use this as a delimiter.

For example, if you want to split"prefix-item1-item2-suffix"to be split, the delimiter must be"-", or"prefix-"as a prefix and"-suffix"as a suffix to handle.splitThe filter itself does not have the ability to recognize patterns that start with a specific character and end with another specific character, or match any prefix but end with a specific suffix.

Its core lies inliteral matching, rather than pattern matching. You cannot give it a complex expression for it to 'understand' and find the split point that meets the conditions.

Does it support regular expressions or other complex patterns?

So,splitDoes the filter support identifying patterns such as 'starts with any number and ends with a specific character', or matching variable prefixes and suffixes, like regular expressions? The answer is,AnQiCMS template engine built-insplitFilter, currently does not directly support regular expressions or more complex, non-fixed string pattern delimiters.

The design philosophy of template engine usually tends to maintain its lightness and focus, mainly used for data display logic, while keeping complex string processing and business logic in the backend code.Integrating a complex regular expression parser at the template level may increase the complexity of template parsing, performance overhead, and even introduce potential security risks.

It is worth mentioning that the AnQiCMS backend content management tool (such as batch replacement of keywords) mentioned in the document indeed supports regular expressions, but this belongs to the backend functional scope, and is related to the template level.splitThe working mechanism of the filter is different. These features are already completed with data processing before the data is stored in the database or published.

If your data needs to be split through complex patterns (such as regular expressions), AnQiCMS users have the following recommended practices:

  1. Perform data preprocessing in the Go backend:This is the most recommended and the most powerful method. AnQiCMS is developed based on Go language, which provides a powerful regular expression library (regexppackage), can easily implement any complex matching and splitting logic.You can complete the complex string splitting operation in the controller (Controller) or service layer (Service Layer) and then directly pass the processed data (such as an array of strings) to the front-end template for display.This can take advantage of the powerful features of Go while maintaining the simplicity and efficiency of the template.

  2. **Using front-end JavaScript to process: