In AnQiCMS template development,splitA filter is a very practical 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 those that need to match specific prefixes and suffixes of characters, splitThe scope of the filter's capabilities is worthy of our in-depth exploration.

splitBasic usage of the filter.

splitThe main function of the filter is to split a string into an array of strings based on a specified delimiter.Its usage is simple and intuitive, usually used to process data composed of single, fixed characters or concatenated strings.For example, if you have a string containing multiple tags"AnQiCMS,GoLang,CMS"You can easily go throughsplitThe filter splits it into["AnQiCMS", "GoLang", "CMS"]such an array.

Specifically, its basic syntax is{{ obj|split:"分隔符" }}. Among them,objIs the source string you need to process, and"分隔符"Then it is the fixed text you want to use to split the string.

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

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

If your delimiter is a string containing spaces, for example", "(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"] #}

Furthermore,splitThe filter has some special behaviors: If the provided delimiter is not found in the source string, it will return an array containing only the original string itself. And when the delimiter is set to an empty string""then,splitWill smartly split each UTF-8 character of the source string into an element of the array.

For example:

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

In AnQiCMS, besidessplitAnd there is also a very convenient one.make_listA filter that can also split strings into arrays of characters, with functionality similar tosplitSimilar when using an empty string as a delimiter, but emphasizes processing by character list:

{% 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 the AnQiCMS template was initially designed to handle relatively simple and direct string splitting tasks. It supports the delimiter pattern isfixed string matchingThis means that you providesplitThe separator for the filter, which is considered as a complete, literal string for matching. It will search for the separator you provided in the string.Absolutely consistentSubstrings, and split at this boundary.

For example, if you want to separate"prefix-item1-item2-suffix"separated, the delimiter must be"-"Or"prefix-"as a prefix and"-suffix"as a suffix to handle. However,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.

It lies at the core ofliteral matching, not pattern matching. You cannot give it a complex expression for it to 'understand' and find a split point that meets the conditions.

Does it support regular expressions or other complex patterns?

Then,splitDoes the filter support patterns like regular expressions, recognizing 'numbers starting with, ending with a specific character', or matching variable prefixes and suffixes? 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 engines usually tends to maintain their 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 keyword batch replacement) mentioned in the document does indeed support regular expressions, but this belongs to the backend function category, and is related to the template level,splitThe filter operates differently. These functions in the background have already completed 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 methods:

  1. Perform data preprocessing on the backend in Go language:This is the most recommended and powerful method. AnQiCMS is developed based on Go language, which provides a powerful regular expression library(regexpA package can easily achieve complex matching and splitting logic.You can perform complex string splitting operations in the Controller or Service Layer, and then pass the processed data (such as a string array) directly to the front-end template for display.This can take advantage of Go's powerful features while maintaining the concise and efficient template.

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