What are the main differences and application scenarios between the `make_list` filter and the `split` filter when splitting strings into arrays?

Calendar 👁️ 58

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, butsplitThe 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 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_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

Related articles

What are the different splitting behaviors when the delimiter is empty or not present?

In AnQi CMS template development, the `split` filter is a very practical tool that can help us split strings into arrays according to the specified delimiter, which is very convenient when dealing with list data, tag content, or any structured text.However, when the delimiter is an empty string or does not exist in the target string, the behavior of the `split` filter shows some clever characteristics, understanding these characteristics is crucial for precise template control and avoiding potential errors.

2025-11-07

How to split a comma-separated keyword string configured in the AnQiCMS backend into an iterable keyword list?

In AnQiCMS (AnQiCMS) daily content operations, we often need to display related keywords on articles or product detail pages.These keywords not only help search engines understand the content of the page, but also guide users to discover more relevant information.In most cases, we will enter these keywords in a text box on the backend, separated by commas, for example: "website operation, SEO optimization, content marketing.

2025-11-07

How to use the `slice` filter to extract specific parts of parameters or paths from a long URL?

In the world of AnQi CMS templates, flexible data handling is the key to improving website user experience and SEO performance.Sometimes, we need to extract a specific part from a long URL, such as path parameters, product ID, or simply display a part of the URL to keep the page concise.At this time, the `slice` filter is an extremely useful tool that can help us accurately cut any segment of a string or array.

2025-11-07

Does the `slice` filter support negative indices to start cutting from the end?

In AnQi CMS template development, we often need to flexibly truncate and display strings or data lists, only showing a part of them.The `slice` filter is designed for this purpose, allowing us to precisely control the length of the content.And the answer to whether the `slice` filter supports negative indices, which is whether it can start cutting from the end, is affirmative, and this feature greatly enhances our flexibility and convenience when dealing with dynamic content templates.

2025-11-07

How to combine a dynamically generated array (such as a tag list) into a string with a specified delimiter for display in a template?

In website content operation, we often need to display a series of related information in the form of a list to users, such as all tags of an article, multiple characteristics of a product, or a set of image URLs.AnQiCMS's template system usually provides these data in the form of a dynamic array (or sometimes called slices, lists).

2025-11-07

How to use `split` and `join` filters to standardize user tag inputs?

In the daily content operation of AnQi CMS, the tags (Tag) submitted by users often face a common problem: the format is not unified.Some users are accustomed to separating with commas, some with semicolons, and even possibly with Chinese commas or even just spaces.These irregular inputs, if directly displayed on the website front-end, not only affect the aesthetics but may also reduce the usability of tags, for example, causing trouble when generating tag clouds or performing SEO optimization.

2025-11-07

How to truncate the title of the article and automatically add an ellipsis in the AnQiCMS template?

In AnQi CMS template design, in order to ensure the beauty of the page and the uniformity of the layout, we often need to truncate the article title and automatically add an ellipsis after truncation.AnQiCMS provides a simple and efficient template filter to meet this requirement, making content display more flexible.

2025-11-07

What is the essential difference between the `truncatechars` and `truncatewords` filters, and how should one choose?

In AnQi CMS template development, in order to better display content summaries or control page layouts, we often need to truncate text.At this moment, the `truncatechars` and `truncatewords` filters come into play.They can all help us shorten long texts and add an ellipsis at the end, but there is an essential difference in their truncation logic. Understanding these differences is crucial for choosing and using them correctly.

2025-11-07