In AnQi CMS template development, it is often encountered that a string needs to be split into multiple parts for further processing or dynamic display. To meet this need, the AnQiCMS template engine providesmake_listandsplitThese are very useful filters. Although both can convert strings to arrays, there are obvious differences in their core functions, splitting logic, and applicable scenarios in actual use.Understanding these differences can help us process content more efficiently and accurately.
splitFilter: Split by specified delimiter
splitThe filter is the most commonly used tool when processing strings with clear delimiters.Imagine, your article tags may be saved in the background as a string like 'website operation, SEO optimization, content marketing', or each word in a sentence needs to be processed separately. At this point,splitThe filter can be put to use. It will split the string into an array (or slice) based on the separator you specify.
For example, if you have a string containing 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 an array with a single element containing the original string. Additionally, if an empty string is passed as a delimiter tosplitfor examplesplit:""),it will be very similar tomake_listSplitting strings into each UTF-8 character. But this is usually notsplitthe main design purpose.
Application scenarios: splitThe filter is best suited to process strings that are structured and have content separated by specific symbols (such as commas, semicolons, bars, spaces, etc.).It performs well in parsing user input, handling multi-value fields saved in the background, and extracting specific information from text.
make_listFilter: Split character by character
withsplitdifferent,make_listThe filter is a more 'atomic' tool. It does not care about any delimiters and directly splits the string you provide into an array character by character.This means that it will treat each character as an independent element, regardless of whether there are spaces, punctuation, or even multibyte characters (such as Chinese).
For example, if you want to display each character of a slogan individually:
{% set slogan = "安企CMS,让网站更安全" %}
{% set charList = slogan | make_list %}
{# 此时charList将是 ["安", "企", "C", "M", "S", ",", "让", "网", "站", "更", "安", "全"] #}
You can iterate through this character array to achieve some special design effects, such as adding animations or unique styles to each character:
{% for char in charList %}
<span class="animated-char">{{ char }}</span>
{% endfor %}
Especially worth mentioning is that for Chinese and other multi-byte characters,make_listCan accurately split each Chinese character into an independent element without any garbled or split errors, which is very practical in the development of Chinese content website templates.
Application 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 separately,...make_listThese are the most direct and reliable choices.
Guide to Core Differences and Selection
Although both of these filters can convert strings to arrays, their core logic and application scenarios are quite different:
- The splitting criteria are different:
splitDepends on the explicitly specified delimiter for logical slicing andmake_listIt does not depend on delimiters, it treats each character of the string as an independent element for physical separation. - The output granularity is different:
splitAn array is formed by substrings (usually words or phrases), which are 'meaningful units' divided by delimiters in the original string.make_listAn array consisting of individual characters is produced, where each element is the smallest unit character in a string. - Handling multi-byte characters:Both handle multi-byte characters well, but
splitThe processing result depends on the separator (for example, a comma-separated Chinese string), andmake_listit will treat each Chinese character as an independent array element.
When choosing which filter to use, the key is in what granularity you want the string to be split, and whether there is a clear, identifiable marker within the string that can be used to separate it:
- If your string is structured, with clear delimiters (such as commas, spaces, bars, etc.), and you want to get an array of 'content blocks' divided by these boundaries, then use
splitfilter. - 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, then
make_listThe filter is your only choice.
Understand and use flexibly.make_listandsplitThese filters will greatly enhance your ability to process and display dynamic content in Anqi CMS, making your website content more expressive and interactive.
Frequently Asked Questions (FAQ)
1. If my string does not containsplitwhat will happen if the delimiter specified by the filter?Answer:splitThe filter will not error, it will return an array containing a single element with the original string. For example, if the string is"安企CMS", and you usesplit:",", the result would 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 be split into corresponding character units. In most cases, an emoji is recognized as one or more independent character elements.
3. Which other template filters or tags can be combined with these two filters to achieve more complex functions?Answer: They can be combined with various filters and tags. The most common is to combine withforthe loop tag to iterate over the split array. You can also pass the split array through againjoinThe filter is concatenated with a new delimiter. In addition,lengthThe filter can calculate the number of elements in the split array,slice