In the template development of AnQi CMS,splitA filter is a very practical tool that can help us flexibly handle string data, splitting it into an array according to a specified delimiter. Whether it is to parse the tag list of an article, handle multi-value settings in configuration files, or perform other scenarios that require splitting strings by specific characters,splitFilters can all show their strength. However, there is a frequently questioned scenario during use: when the source string itself is an empty string,splitThe filter returns an empty array or an array containing an empty string? Understanding this point can help us write more robust and expected template code.

First, let's reviewsplitThe basic usage of the filter. According to the Anqi CMS documentation,splitThe filter accepts two parameters: the string to be processed and the delimiter string.Its core function is to split the source string into an array of strings according to the delimiter.For example, if there is a string"apple,banana,orange"and use","as a separator,splitwill return["apple", "banana", "orange"]such an array.

What will happen when the source string is empty? Here we need to distinguish between two main scenarios: whether the delimiter is empty.

Scenario one: the delimiter is not empty, and the source string is empty.

Suppose we have an empty string""and try to use a non-empty delimiter (for example",") to operate on itsplitIn this case,splitThe filter treats an empty string itself as a 'paragraph' or 'element' because it does not find any separators to further split it. Therefore, it returns an array containing only an empty string, i.e.[""].

For example, if you use it like this in a template:{% set result = ""|split:"," %}at this point,resultIt will be an array of length 1, with its only element being an empty string.When trying to iterate over this array or check its length, unexpected results may occur.For example, if judged directlyresultYou will find that its length is not 0, but 1.

Scenario two: the delimiter is empty, and the source string is also empty

The document explicitly mentions a special case: splitOperation (for example""|split:""), the result will be different.

In this case, since there are no UTF-8 characters that can be split in the source string,splitThe filter will return a truly empty array, i.e.[]. This is an empty array with a length of 0, containing no elements.

In order to better understand, we can also consider a related scenario: when the source string is not empty but the separator is empty. For example,"AnQi"|split:"". At this point,splitThe filter splits each UTF-8 character and returns["A", "n", "Q", "i"]such an array. This is in contrast to the empty source string and empty delimiters, further confirming the logic of 'splitting by each UTF8 character'.

Considerations in practical applications.

UnderstandingsplitThe subtle differences in handling empty strings by the filter are crucial for the robustness of template code. Especially when iterating in a loopsplitWhen encountering an array at the end, if the source string may be empty but you expect an empty loop (i.e., do not execute the loop body), then return[""]instead of[]The scenario may confuse you.

For example, if you want tosplitskip a block and go directly to the array that follows when it is empty{% if my_array %}Or{% if my_array|length > 0 %}then whenmy_string|split:","The result is[""]When, both of these conditions will be true because the array itself is not empty, and its length is 1. To avoid this situation, you may need to further check whether the elements in the array are empty, for example{% if my_array|length > 0 and my_array[0]|length > 0 %}.

In short, the Anqi CMS'ssplitThe filter behaves differently when processing empty strings, depending on whether the delimiter is empty or not: when the delimiter is not empty and the source string is empty, it returns[""]When the delimiter and the source string are both empty, return[]Mastering these details helps us predict and process data more accurately, enabling us to write efficient and error-free template code.


Frequently Asked Questions (FAQ)

  1. Why""|split:","will return[""]instead of[]?This is becausesplitThe filter processes the entire source string as a 'paragraph' or 'element' to try to find delimiters. When the delimiter","is not found in an empty string""when searching,splitConsider this empty string to be a single complete paragraph, therefore, it is returned as the only element of the array, that is[""].

  2. How can I ensuresplitThe subsequent array does not contain an empty string (such as,[""]Is this situation?)?If your business logic does not want[""]If such an array appears, you can first check if the original string is empty, or insplitThen perform a filter again. For example, you can use one firstifstatement to judge the originallengthwhether it is greater than 0, thensplitoperation; or, insplitAfter getting the array, traverse the array again to exclude empty string elements.

  3. make_listFilters andsplitWhat are the differences between filters? make_listThe filter will split the string into an array by each UTF-8 character, for example"安企CMS"|make_listwill return["安", "企", "C", "M", "S"]HoweversplitThe filter splits the string based on the delimiter you specify, and if the delimiter is empty, it degrades to splitting by each UTF-8 character. Therefore,splitProvided more flexible split capabilities based on custom delimiters.