In AnQiCMS template development, string processing is a common requirement.Sometimes we need to split a string containing multiple values into separate items so that they can be traversed on the page for display or further processing. At this point,splitThe filter has become a powerful assistant for template developers. It can split a string into an array (or called a slice) based on the specified delimiter, greatly simplifying the logic of displaying complex data.

However, when usingsplitWhen filtering, certain scenarios may confuse developers who are new to the field, 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 reviewsplitThe 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 the split.

For example, a comma-separated tag string is obtained from the background“SEO,优化,内容营销”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 %}

This way, you can easily traversetag_listeach element and generate the corresponding link for them.

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 specified delimiter, it will not throw an error or return an empty result. Instead, it will return aonly contains the original string itselfThe array, and the length of this array is 1.

For example, you might have a keyword for an article, but this keyword is only one, not separated by commas or other separators, such as"AnQiCMS模板开发". When you try to usesplitFilter it when

{% 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_arrayIt has a length of 1, where the only element is the originalsingle_keyword_stringThis feature allows you to avoid writing additional code to judge whether a string contains delimiters, and you can directly processsplitTraverse the result. You can check the length of the array to determine if the original string only contains one item that is not separated.

When the delimiter is an empty string ("")

In a special case, you will pass aempty stringassplitdelimiter parameter to the filter. In this case,splitthe filter will behave differently: it will take every singleUTF-8 characterSplit into an array element.

For example, if you want to split a Chinese text into individual characters for processing:

{% 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_listA filter that also splits strings into arrays. Both have similar effects in this scenario, you can choose to use them according to your personal preference.

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

IfsplitThe filter receivesThe input string itself is emptyFor example""No matter what delimiter you specify, it will return a empty 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 useful when performing conditional judgments in the template. An empty array in the AnQiCMS template{% if %}is evaluated asfalseThis allows you to control the display of prompts such as 'No data available' when there is no content, rather than traversing an empty array causing the page to render incorrectly.

Considerations in practical applications.

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

  • Uncertainty of the data source:Data retrieved from the background may be inconsistent due to the editor's operations.For example, some article tags may be a complete string (without separators), and some are comma-separated lists.splitThe filter's fault tolerance allows you to avoid writing different handling logic for each case.
  • Elegant empty state handling:Combine{% if ... %}statement, you can easily judge.splitWhether the array is empty after, thus deciding whether to render relevant content to improve user experience.
  • Precise character operations:When you need to process a string at a character level, use an empty string as a separator to separatesplitOr filter.make_listfilter, which can quickly meet your needs.

In short, in the AnQiCMS templatesplitThe filter is a powerful and thoughtful tool. It handles regular string splitting while also elegantly dealing with special cases such as empty strings, no delimiters, or empty delimiters, making template development smoother and more efficient.


Frequently Asked Questions (FAQ)

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

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

  3. splitthe filter meetsmake_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 process according to specific rules. Andmake_listThe filter will unconditionally split the string'sEach UTF-8 characterInto an array element. Both can be used when you need to split by character, butsplitMore flexible when splitting by specific symbols is needed.