In the template development of AnQi CMS,splitThe filter is a very practical tool that can help us split strings into arrays according to the specified delimiter, which is very convenient when dealing with list data, tag content, or any structured text. However, when the delimiter is an empty string or does not exist in the target string,splitThe behavior of the filter will display some clever characteristics, understanding these characteristics is crucial for precise control of the template and avoiding potential errors.
splitThe basic function of the filter
Generally speaking,splitThe filter's function is to split a string into an array of strings according to the separator you provide. For example, if there is a string connected by commas and spaces."apple, banana, cherry"split into["apple", "banana", "cherry"]such an array, you would use it like this:
{% set fruits_string = "apple, banana, cherry" %}
{% set fruits_array = fruits_string|split:", " %}
{# fruits_array 现在是 ["apple", "banana", "cherry"] #}
This is a direct and clear usage issplitThe most common scenario of the filter. But next, we will discuss the behavior in two special cases.
When the delimiter is an empty string: split by each UTF-8 character.
When we put an empty string in parenthesis"") assplitas the delimiter for the filter, its behavior becomes very interesting and practical. In this special case,splitThe filter does not look for any specific character sequence to act as a split point, but rather splits each UTF-8 character in the original string independently, and returns them as separate elements in an array.
For example, if you have a Chinese string"安企CMS"Try splitting it with an empty string:
{% set text_string = "安企CMS" %}
{% set char_array = text_string|split:"" %}
{# char_array 现在是 ["安", "企", "C", "M", "S"] #}
{# 如果您将其用 "-" 连接起来显示,会是 "安-企-C-M-S" #}
{{ char_array|join:"-" }}
This behavior is very useful for scenarios that require character-level operations on strings, such as counting the number of characters in a string (thoughlengthThe filter is more direct), reverse the string (by array operations and then concatenation), or handle each character of the string individually.
It is worth mentioning that Anqi CMS also provides a filter namedmake_listwhich serves the same purpose assplitWhen the delimiter is empty, it is very similar, and it is used specifically to split a string into an array of characters. It is used in most scenarios that require character-level splitting, such asmake_listIt will be more intuitive and recommended because it explicitly expresses the intention to split by characters.
When the delimiter is not present in the target string: return an array with a single element containing the original string.
A common but often overlooked situation is when the specified delimiter does not exist at all in the target string. For example, you try to use a hash mark (#Split a string that does not contain a hashtag.
In this case,splitThe filter will not return an empty array (for example[]),而是会返回一个只包含一个元素的数组。这个唯一的元素就是原始的完整字符串本身。
Let's illustrate this with an example:
{% set sentence_string = "欢迎使用安企CMS" %}
{% set no_delimiter_array = sentence_string|split:"#" %}
{# no_delimiter_array 现在是 ["欢迎使用安企CMS"] #}
{# 它的长度是 1,而不是 0 #}
{{ no_delimiter_array|stringformat:"%#v" }}
This behavior is crucial for subsequent processing logic. If you expect to always get multiple elements when iterating over the split array without making an early judgment, then this single-element array may cause a deviation in your code logic, such as processing the data only once unexpectedly in a loop.
Application and practice with **
Understanding these two special behaviors can help us control data processing more accurately in template development, avoiding unnecessary errors.
- Avoid unexpected single-element arrays:While using
splitAfter the filter, if you need to ensure that the array contains multiple actual split elements, consider using it before splittingcontainThe filter checks if the delimiter exists in the original string or if the length of the resulting array is greater than 1 after splitting.{% set my_string = "item1,item2" %} {% set my_delimiter = "," %} {% if my_string|contain:my_delimiter %} {% set result_array = my_string|split:my_delimiter %} {# 正常处理包含多个元素的数组 #} {% else %} {% set result_array = [my_string] %} {# 或者根据需求处理为 [] #} {# 处理分隔符不存在的情况 #} {% endif %} - Clarify the intention of character-level splitting:If you explicitly need to split a string at the character level, use it directly
make_listthe filter will besplitmore explicit in intention with the empty separator, improving code readability.
by masteringsplitThe filter's performance in these special scenarios allows you to be more confident and efficient in template development and content presentation in Anqi CMS.
Frequently Asked Questions (FAQ)
Q:
splitHow will the empty string elements be handled in the array returned by the filter?A: If the original string contains consecutive delimiters, or delimiters appear at the beginning or end of the string,splitThe filter will generate empty string elements according to the specific situation. For example,"a,,b"|split:","it will generate["a", "", "b"]This means you need to consider filtering out empty string elements when processing the result array to avoid unnecessary empty values.Q:
make_listthe filter meetssplitWhat is the main difference of the filter when it is at an empty delimiter?A:make_listThe filter is designed specifically to split strings into arrays of characters, its behavior is more explicit, and it always splits according to characters, without being affected by the delimiter parameter. AndsplitWhen the delimiter is empty, it has the same effect, but its core design is based on delimiters for splitting. Therefore, it is recommended to use it for character-level splitting when neededmake_listTo improve code readability and clarity of intent.Q: When using
splitAfter the filter, how to judge whether the returned result is an empty array?A: BecausesplitWhen the delimiter does not exist, it returns an array containing the original string as a single element, rather than a true empty array, so you cannot simply determine whether the length of the array is zero. If you need to confirm that there has been an actual split (i.e., whether the original string contains at least one delimiter), you can combinecontainThe filter first checks if the delimiter exists in the original string, or if the length of the resulting array is greater than 1, or if its unique element is exactly the same as the original string.