In AnQiCMS template development, string processing is a common requirement.Sometimes we need to split a string containing multiple values into individual items so that they can be traversed and displayed on the page or further processed.splitFilter has become a powerful assistant for template developers.It can split a string into an array (or called a slice) according to the specified delimiter, greatly simplifying the logic of complex data display.

However, when usingsplitFiltering may cause confusion for developers who are new to the concept, especially when dealing with empty strings or strings that do not contain the specified delimiter.splitHow will the behavior of the filter be? Understanding these details can help us write more robust and flexible AnQiCMS template code.

splitBasic application of the filter

First, let's take a look back atsplitThe basic usage of the filter. Its main function is to split a string according to the delimiter you provide and then return an array containing all the parts after splitting.

For example, a comma-separated tag string is obtained from the background“SEO,优化,内容营销”Do you want to display each tag as an independent link on the page? UsesplitThe filter can be easily implemented:

{% set tags_string = "SEO,优化,内容营销" %}
{% set tag_list = tags_string|split:"," %}

{# tag_list 现在是一个包含 ["SEO", "优化", "内容营销"] 的数组 #}

{% for tag in tag_list %}
    <a href="/tag/{{ tag }}">{{ tag }}</a>
{% endfor %}

So, you can easily traversetag_listeach element in it and generate corresponding links.

when the string does not contain the specified delimiter.

This issplitA smart design of the filter. If enteredsplitThe string content of the filter does not contain the separator you specified, it will not throw an error or return an empty. Instead, it will return aonly the original string itselfThe array, and the length of this array is 1.

For example, you may have a keyword in an article, but this keyword is only one, without using a comma or other separator to distinguish, like"AnQiCMS模板开发". When you try to usesplitThe filter processes it:

{% set single_keyword_string = "AnQiCMS模板开发" %}
{% set keyword_array = single_keyword_string|split:"," %}

{# 此时,keyword_array 将会是 ["AnQiCMS模板开发"] #}

{% for item in keyword_array %}
    <p>处理后的关键词: {{ item }}</p>
{% endfor %}

In this case,keyword_arrayhas a length of 1, with the only element being the originalsingle_keyword_stringThis feature allows you to avoid writing additional code to check if a string contains a delimiter, and you can directly apply it.splitThe result after is traversed. You can determine if the original string contains only one unseparated item by checking the length of the array.

When the delimiter is an empty string ("")

Another special case is, you will pass aEmpty stringassplitthe separator parameter of the filter. In this case,splitthe filter will exhibit different behavior: it will take everyUTF-8 字符拆分成数组的一个元素。

例如,如果您想将一段中文文本拆分成单个字符进行处理:

{% set chinese_text = "安企CMS" %}
{% set char_array = chinese_text|split:"" %}

{# 此时,char_array 将会是 ["安", "企", "C", "M", "S"] #}

{% for char in char_array %}
    <span>{{ char }}</span>
{% endfor %}

This behavior is very useful in some character processing or gamification display scenarios. It is worth mentioning that AnQiCMS also providesmake_listFilter, its function is also to split strings into an array of characters. Both have similar effects in this scenario, and you can choose to use them according to your personal habits.

When the string to be processed itself is empty ("")

Ifsplitreceived by the filteris emptyfor example"", no matter what delimiter you specify, it will return aempty array.

{% set empty_string = "" %}
{% set result_array = empty_string|split:"," %}

{# 此时,result_array 将会是一个空数组 [] #}

{% if result_array %}
    <p>数组不为空,可以进行遍历。</p>
{% else %}
    <p>数组为空,没有内容可显示。</p>
{% endif %}

This behavior is very practical when making conditional judgments in templates. An empty array will be evaluated as in AnQiCMS templates.{% if %}in the statement.falseThis allows you to conveniently control displaying "No data" and other prompts when there is no content, rather than traversing an empty array causing the page to render an error.

Considerations in practical application

UnderstandingsplitThe filter's behavior in these edge cases can help you build templates with more confidence:

  • Uncertainty of the data source:Data obtained from the background may be inconsistent in format due to the editor's operations.For example, some articles' tags may be a complete string (without separators), and some are comma-separated lists.splitThe fault tolerance of the filter allows you to avoid writing different handling logic for each case.
  • Graceful empty state handling:Combine{% if ... %}You can easily judge the statement,splitThe array after is empty, thus deciding whether to render related content, improving user experience.
  • Fine character operations:When you need to process a string at the level of individual characters, use an empty string as a delimiter.splitFilter ormake_listThe filter can quickly meet your needs.

In short, what is the data type returned by the filter in the AnQiCMS template?splitFilter is a powerful and well-thought-out tool.It can handle the splitting of regular strings while elegantly dealing with special cases such as empty strings, no delimiters, or empty delimiters, making template development more smooth and efficient.


Common Questions and Answers (FAQ)

  1. splitWhat is the data type returned by the filter? splitThe filter always returns an array (or sometimes called a slice), even if the original string does not contain a delimiter or is empty.You can iterate over it like a list, or check its length.

  2. How to judgesplitIs the array after it empty, or does it only contain one element, the original string?You can check the length of the array to determine. For example,{% if array|length == 0 %}indicates that the array is empty;{% if array|length == 1 %}and{% if array[0] == original_string %}means that the array only contains the original string itself and is not separated. More concisely,{% if array %}Can determine if an array is non-empty.

  3. splitFilter is related tomake_listWhat are the differences in function between filters? splitThe filter is based on the one you provided.SeparatorTo split the string. If the specified delimiter does not exist or is empty, it will be processed according to specific rules.make_listThe filter will split the string without any conditions.Every UTF-8 character.Is split into an element of an array. When you need to split by character, both can be used, butsplitMore flexible when splitting by specific symbols.