In the template development of Anqi CMS,splitFilter is a very practical tool that can help us split strings into arrays according to a 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 exhibit some clever characteristics, understanding these characteristics is crucial for precise control of the template and avoiding potential errors.

splitThe basic function of the filter.

In most cases,splitThe function of the filter is to split a string into an array of strings according to the delimiter you provide. For example, if there is a string connected by commas and spaces."apple, banana, cherry",and hope to split it into["apple", "banana", "cherry"]such an array, you will use it like this:

{% set fruits_string = "apple, banana, cherry" %}
{% set fruits_array = fruits_string|split:", " %}
{# fruits_array 现在是 ["apple", "banana", "cherry"] #}

This direct and clear usage issplitThe most common scenario for a filter. But next, we will discuss the behavior under two special cases.

When the delimiter is an empty string: split by each UTF-8 character.

When we use an empty string ("") assplitthe delimiter of a filter, its behavior becomes very interesting and practical. In this special case,splitThe filter does not look for any specific character sequence as a split point, but instead will split each UTF-8 character independently from the original string and return them as separate elements of 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 (thoughlengthFilter more directly)、reverse the string (by array operations and then concatenation), or process each character in the string individually.

It is worth mentioning that the Anqi CMS also provides a filter namedmake_listwhich serves the same purpose assplitvery similar when the delimiter is empty, used specifically to split strings into arrays by characters. In most scenarios that require character-level splitting, it is usedmake_listIt will be more intuitive and recommended, as it explicitly expresses the intention to split by character.

When the delimiter is not present in the target string: return an array with a single element containing the original string.

Another common but often overlooked situation is when the delimiter you specified does not exist at all in the target string. For example, you try to use a hash mark (#)to split a string that does not contain any hashtags.

In this case,splitThe filter does not return an empty array (for example[]),Instead, it will return an array containing a single element. That single element is the original complete string itself.

Let us 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, this single-element array may cause your code logic to deviate, such as accidentally processing only one data in a loop.

Application and **Practice in Reality

Understanding these two special behaviors can help us accurately control data processing in template development, avoiding unnecessary errors.

  1. Avoid unexpected single-element arrays:When usingsplitAfter the filter, if you need to ensure that the array contains multiple actual split elements, consider using it before splittingcontainThe filter checks whether the delimiter exists in the original string or whether the length of the resulting array after splitting is greater than 1.
    
    {% 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 %}
    
  2. Make it clear the intention of splitting at the character level:If you explicitly need to split strings at the character level, usemake_lista filter insteadsplit, using an empty separator makes the intention clearer and improves code readability.

By masteringsplitThe filter performs well in these special scenarios, allowing you to develop templates and present content confidently and efficiently in the security CMS.


Common Questions (FAQ)

  1. Q:splitHow are empty string elements 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:","will generate["a", "", "b"]This means that when you are processing the result array, you may need to consider additional filtering of empty string elements to avoid processing unnecessary empty values.

  2. Q:make_listFilter is related tosplitWhat is the main difference of the filter when the separator is empty?A:make_listFilter designed specifically to split strings into arrays by each character, its behavior is more explicit, and it always splits by character, not affected by the delimiter parameter.splitThe same effect is shown when the delimiter is empty, but its core design is based on delimiters for splitting. Therefore, it is recommended to use it when character-level splitting is needed.make_listTo improve the readability and clarity of the code.

  3. Q: In usagesplitAfter the filter, how to determine if the returned result is an empty array?A: SincesplitWhen a delimiter is not present, it returns a single-element array containing the original string instead of an actual empty array, so it cannot be simply judged by the length of the array. If you need to confirm whether an actual 'split' has occurred (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 after splitting is greater than 1, or if its unique element is exactly the same as the original string.