In AnQi CMS template development, it is often necessary to split a string content into multiple parts for further processing or dynamic display. To meet this requirement, AnQiCMS template engine providesmake_listandsplitThese are very useful filters.Although they can both convert strings to arrays, there are significant differences in their core functions, splitting logic, and application scenarios in practical use.Understanding these differences can help us handle content more efficiently and accurately.

splitFilter: Split by specified delimiter

splitFilter is the most commonly used tool when we process strings with clear delimiters.Imagine, your article tags may be saved as a string likesplitThe filter comes into play. It will split the string into an array (or a slice) based on the delimiter you specify.

For example, if you have a string with comma-separated values:

{% set tagsString = "安企CMS,SEO优化,模板制作" %}
{% set tagsArray = tagsString | split:"," %}
{# 此时tagsArray将是 ["安企CMS", "SEO优化", "模板制作"] #}

You can easily iterate over this array, generating a separate link for each tag:

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

If the delimiter is a space,splitAlso works well, for example, by splitting a sentence into words:

{% set sentence = "安企CMS 提供高效灵活的内容管理" %}
{% set wordsArray = sentence | split:" " %}
{# 此时wordsArray将是 ["安企CMS", "提供", "高效灵活的", "内容管理"] #}

It is worth noting that when the specified delimiter does not exist in the string,splitThe filter will not throw an error, but will return a single-element array containing the original string. In addition, if an empty string is passed as the delimiter tosplit[for example]split:""),it will be very similar tomake_listThis will split the string by each UTF-8 character. But this is usually notsplitthe main design purpose.

Applicable scenarios: splitThe filter is most suitable for processing strings that are structured and logically separated by specific symbols (such as commas, semicolons, pipes, spaces, etc.).It performs well in parsing user input, handling the saving of multi-valued fields in the background, and extracting specific information from text.

make_listFilter: split by character

Withsplitdifferent,make_listFilter is a more 'atomized' tool.It does not care about any delimiters but directly splits the string you provide into an array character by character.This means that whether there are spaces, punctuation, or even multi-byte characters (such as Chinese) between the characters, it will treat each character as an independent element.

For example, if you want to display each character of a slogan independently:

{% set slogan = "安企CMS,让网站更安全" %}
{% set charList = slogan | make_list %}
{# 此时charList将是 ["安", "企", "C", "M", "S", ",", "让", "网", "站", "更", "安", "全"] #}

You can iterate over this character array to implement some special design effects, such as adding animation or unique styles to each character:

{% for char in charList %}
    <span class="animated-char">{{ char }}</span>
{% endfor %}

It is particularly worth mentioning that for Chinese characters such as multi-byte characters,make_listCan accurately split each Chinese character as an independent element without any garbled characters or splitting errors, which is very practical in the development of templates for Chinese content websites.

Applicable scenarios: make_listThe filter is more suitable for scenarios that require character-level processing. For example, if you want to count the frequency of different characters in a string, or assign unique display effects to each character, or even simply display each letter/character individually, make_listThese are the most direct and reliable choices.

Core Differences and Selection Guide

Although both of these filters can convert strings to arrays, their core logic and applicable scenarios are completely different:

  • Split based on different criteria: splitDependent on the delimiter you explicitly specify for logical "slicing".make_listIt does not rely on delimiters at all; it physically "splits" each character of the string as an independent element.
  • Different granularity of output: splitThe array consists of substrings (usually words or phrases) that are 'meaningful' units divided by delimiters in the original string.make_listThe array produced consists of single-character strings, with each element being the smallest unit character in a string.
  • Handling multi-byte characters:Both handle multi-byte characters well, butsplit的处理结果取决于分隔符(例如,用逗号分隔的中文字符串),而make_list则会把每个中文字符都视为一个独立的数组元素。

When choosing which filter to use, the key is what granularity you want the string to be split into, and whether there is a clear, identifiable marker within the string that can be used for splitting:

  • If your string is structured, with clear delimiters between content that need to be considered as boundaries (such as commas, spaces, pipes, etc.), and you wish to get an array of "content blocks" divided by these boundaries, then usesplitFilter.
  • If your string is irregular, or you don't care about any internal structure, and just want to treat each independent character of the string as an element of an array, thenmake_listFilter is your best choice.

Understand and apply flexibly.make_listandsplitThese filters will greatly enhance your ability to handle and display dynamic content in the AnQi CMS, making your website content more expressive and interactive.


Common Questions (FAQ)

1. If my string does not containsplitthe delimiter specified by the filter, what will happen?Answer:splitThe filter will not throw an error, it will return a single-element array containing the original string. For example, if the string is"安企CMS", and you usesplit:",", the result will be["安企CMS"].

2.make_listCan the filter handle emojis (Emoji)?Answer: Yes,make_listThe filter can correctly handle UTF-8 encoded emojis. Each emoji is usually composed of one or more UTF-8 characters.make_listIt will split it into the corresponding character units. In most cases, an emoji will be recognized as one or more independent character elements.

3. These two filters can be combined with which other template filters or tags to achieve more complex functions?答:They can be combined with a variety of filters and tags. The most common is to use withforthe loop tag to iterate over the split array. You can also pass the split array throughjoinThe filter is concatenated with a new delimiter. Additionally,lengthThe filter can calculate the number of elements in the split array,slice