In the template development of AnQi CMS,splitThe filter is a very practical tool that can help us flexibly handle string data, splitting it into an array according to the specified delimiter.This is very useful in many content display and data processing scenarios.splitWhat will the filter's delimiter parameter do if it is empty, how will it split a Chinese string?

UnderstandingsplitThe principle of the filter.

First, let's briefly review.splitThe basic function of the filter.Its main function is to split a string according to one or more characters provided as "edges", split it into multiple segments, and then organize these segments into an array as elements.splitfilter.

For example, a common usage is like this:

{% set tagsString = "安企CMS,内容管理,网站运营" %}
{% set tagList = tagsString|split:"," %}
{# tagList 将会是 ["安企CMS", "内容管理", "网站运营"] #}

In this example, the comma,is our delimiter.

When the delimiter parameter is empty: a special handling method

Then, when we leave the delimiter parameter blank,splitHow will the filter handle it? In the Anqi CMS template environment, based on the powerful string processing capability of Go language,splitThe filter will not fail due to this. Instead, it will take a special processing method: it will take the input string and split it according to eachUnicode character(which is usually called in Go language)runeSplit, and return each character as an independent array element.

This is particularly important for handling Chinese character strings.Chinese characters are often composed of multiple bytes in computers (for example, under UTF-8 encoding), but the concept of Unicode characters ensures that regardless of how many bytes a character occupies at the lowest level, it is considered an independent 'text unit'.splitWhen the filter delimiter is empty:

  • For Chinese stringEach Chinese character is treated as an independent unit and cut, finally forming an array of single Chinese characters.
  • For English stringEach English letter is also considered a separate unit, as are numbers and symbols.

For example, if you have a Chinese string like '安企CMS':

{% set text = "安企CMS" %}
{% set charArray = text|split:"" %}
{# charArray 将会是 ["安", "企", "C", "M", "S"] #}

You can see that even parts of English like 'CMS' are split into individual characters.This provides great convenience for us to handle text operations that require precision down to individual characters.

splitwithmake_listThe differences of filters

In Anqi CMS templates, in addition tosplitthis behavior of the filter under empty separators, we also have a special filter for splitting strings into arrays of individual characters -make_list.

{% set text = "你好世界" %}
{% set charList = text|make_list %}
{# charList 将会是 ["你", "好", "世", "界"] #}

It can be seen from the above example that when dealing with Chinese character strings and splitting characters are needed,splitthe effect of the filter under the empty separator is almost the same asmake_listthe filter is almost the same.

When should you choose one?

  • make_listFilterIf your clear goal is to split a string into individual characters and this operation is the core logic, then usemake_listIt makes your code intentions clearer and more readable.
  • splitFilter (empty delimiter)If you are dealing with a string that may contain various delimiters and one case requires splitting by characters (i.e., the delimiter parameter is dynamically empty), then continue usingsplitThe filter, by controlling the null value of the delimiter parameter, can maintain the continuity of the code logic.In effect, the results are consistent for strings containing Unicode characters.

Application scenarios in practice

This ability to split strings character by character may play a role in the following scenarios in website operation and content management:

  • Character-level text analysisFor example, count the frequency of a specific character in a text, or reverse the order of the text (although it is not usually applicable to Chinese).
  • Interactive UI elementsIn some special UI designs, it may be necessary to display each character of the text independently or perform animation processing.
  • Content review or keyword blur matching: Although it is not as powerful as professional text processing tools, it can perform some simple character-level verification or processing at the template level.
  • Generate unique short codes or identifiers: By rearranging character arrays, create some random or rule-based strings.

In short, the Anqi CMS'ssplitThe filter can accurately split strings by Unicode characters when the delimiter parameter is empty, which is very useful and intuitive for text processing that contains Chinese characters.Understand this detail, which can make our template development more flexible and efficient.


Frequently Asked Questions (FAQ)

1.splitDoes the filter split multi-byte characters (like Chinese) into single bytes under an empty separator?No. Anqicms alsosplitThe filter (in the Go language environment) splits under whitespace delimiters by Unicode characters (rune), rather than by the original byte stream.This means that a Chinese character, even if it occupies multiple bytes in UTF-8 encoding, is treated as a complete character unit for slicing, and there will not be a half Chinese character.

2. Sincemake_listcan also achieve character splitting, when should we use it?splitWhen to use a space separatormake_listWhat?Both are very similar in function, the main difference lies in the clarity of the code intention. If your purpose is to explicitly split a string into an array of individual characters,make_listIt is a more direct and readable choice. And if you are dealing with a scenario that requires dynamic adjustment of delimiters and needs to split by characters when the delimiter is empty, then usesplitAnd making the delimiter empty can maintain logical coherence. In terms of performance, the difference can be ignored in most common application scenarios.

3. If the string contains emoji or special characters,splitHow will the filter handle empty delimiters?Similar to Chinese characters, emojis and most special symbols are also defined as independent characters (rune) in the Unicode standard. Therefore,splitThe filter will treat these emojis or special symbols as single Unicode characters under empty delimiters, and place them as separate elements in the returned array.