In the template development of AnQi CMS, flexible handling of string data is an essential skill for building dynamic pages.Sometimes, we need to split a longer string, using a specified character or string as a delimiter, into a data list (which is what we commonly call an array); at other times, we may need to treat each character of the string independently.splitandmake_listThey can help us easily achieve these needs.

splitFilter: Split strings by specified delimiter

splitThe main function of the filter, as the name implies, is to 'split'.It can split a string into multiple parts based on the delimiter you provide and store these parts as elements in an array.This is like cutting a long rope at each knot, resulting in multiple short pieces of rope.

Why is it practical?Imagine when you get a field value from the background that is connected by commas, semicolons, or other symbols (for example,splitThe filter can be used.

How to use it? splitthe basic syntax of filters is{{ obj|split:"分隔符" }}.

For example, if you have a stringproducts = "Apple,Banana,Orange", and you can write it like this to split it into an array containing "Apple", "Banana", "Orange":

{% set productList = products|split:"," %}
{% for item in productList %}
    <li>{{ item }}</li>
{% endfor %}

So, the page will display:

  • Apple
  • Banana
  • Orange

Handling special cases:

  • The delimiter does not exist:If the specified delimiter does not exist in the original string,splitThe filter will not throw an error, but will treat the entire string as a single element, returning an array containing only one element. For example,"Hello World"|split:","The result is["Hello World"].
  • The delimiter is an empty string:If you set the separator to an empty string ("")splitThe filter will very specifically split each UTF-8 character in the string into a separate element, generating a character array. This is very useful in certain specific scenarios, but usually, if you want to split by character, make_listIt would be a more intuitive choice.

make_listFilter: Split a string into an array of characters

WithsplitThe filter functions are similar, but with a slightly different focus.make_listFilter.It can split a string directly into an array of individual characters, without relying on any delimiter, but directly treating the string as a sequence of characters.This is like picking out each word, each character separately.

Why is it practical?When you need to perform individual operations on each character of a string, such as displaying characters one by one, checking character types, or performing some special formatting,make_listit becomes particularly convenient.

How to use it? make_listThe syntax of the filter is more concise:{{ obj|make_list }}.

For example, if you have a Chinese stringgreeting = "你好安企"and you want to display each character individually, you can write it like this:

{% set charList = greeting|make_list %}
{% for char in charList %}
    <span>{{ char }}</span>-
{% endfor %}

页面上可能会显示: 你-好-安-企-

As you can see,make_list会将中文字符也视为一个独立的元素进行拆分,非常适合处理多语言文本。

结合使用:实用场景

splitandmake_listThe filter is not independent, it is often combined with other template tags and filters to achieve more complex string processing logic.

For example, you may first usesplitSplit into an array, thenjoinThe filter will concatenate the array elements in a different way:

{% set originalString = "Apple, Banana, Orange" %}
{% set processedList = originalString|split:", " %} {# 拆分成 ["Apple", "Banana", "Orange"] #}
{% set newString = processedList|join:" - " %} {# 拼接成 "Apple - Banana - Orange" #}
<p>原始字符串: {{ originalString }}</p>
<p>处理后字符串: {{ newString }}</p>

This is very convenient when you need to reformat the display of list data.

Another example is usingmake_listwithforLoop, to achieve fine control over strings, such as checking whether each character is a number:

{% set myText = "AnQiCMS2024" %}
{% for char in myText|make_list %}
    {% if char|integer %} {# 尝试将字符转换为整数,非数字会返回0 #}
        <span>数字: {{ char }}</span>
    {% else %}
        <span>字符: {{ char }}</span>
    {% endif %}
{% endfor %}

This will check each onemyTextcharacter in it, and display it differently according to its type.

These two filters may seem simple, but they can play a huge role in the development of Anqi CMS templates, making it easier for you to handle string data. Whether it's splitting and displaying multiple tags on the backend, or processing user input at the character level,splitandmake_listAll can provide efficient and convenient solutions.


Common Questions (FAQ)

Q1:splitandmake_listWhat are the differences when the filter processes Chinese?A1: Yes, there are differences in handling Chinese.splitFilter will split according to the Chinese or English character delimiter you specify.make_listThe filter does not depend on delimiters, it will treat each Chinese character or English character in the string as an independent element and split it into an array of characters. In simple terms,splitIs divided by 'words or phrases'.make_listIs divided by 'characters'.

Q2: How to traverse and display the split array?A2: Whether it issplitOrmake_listThe split results are all arrays (or lists), and you can use the loop tags built into the AnQi CMS template to iterate over and display each element of the array. For example,{% for item in array_variable %}The loop tags to iterate over and display each element of the array.{% for tag in tagsString|split:"," %}<span>{{ tag }}</span>{% endfor %}.

Q3: If you want to concatenate the split array back into a string, which filter should you use?A3: If you need to reconnect the split array elements into a string, you can usejoina filter. This filter is different fromsplitfunction is exactly the opposite. For example,{{ productList|join:" - " }}will join the array["Apple", "Banana", "Orange"]to concatenate into a string"Apple - Banana - Orange".