In website content management, we often need to handle various data, among which string processing is particularly common.Many times, the strings obtained from databases or user input contain multiple pieces of information, and we need to split them into independent data items for display or further processing.splitandmake_listThese filters can help us convert strings to arrays, but each has its unique working method and application scenario.Understand the differences, which can help us organize and present content more accurately and efficiently.
splitFilter: Split strings by 'rule'
Imagine, your article tags, product attributes, or other data are connected strings using commas, semicolons, or even specific words, such as 'SEO optimization, content marketing, Go language development'.If you want to display these tags one by one or perform a count, you need to split this string into individual tag items.splitThe filter is your powerful assistant.
splitThe core of it is that it needs a clear 'delimiter'. This delimiter is like a pair of scissors, telling the system where to 'cut' the string. For example, {{ "SEO优化,内容营销,Go语言开发"|split:"," }}This code, separated by commas, will split the string.["SEO优化", "内容营销", "Go语言开发"]Such an array.
Its usage is very intuitive:{{ 你的字符串变量 | split:"你的分隔符" }}You can choose any character or string as a delimiter, such as space, pipe, or a combination like "##".
It should be noted that if your string does not contain the specified delimiter,splitThe filter does not throw an error; instead, it returns an array that contains only the original string itself.This is very useful when you are dealing with single-item data that may not have separators."")splitit will very intelligently split each UTF-8 character in the string. But this is notmake_listThe behavior when processing Chinese is different, and the details will be explained below.
Practical Example:Suppose we have a keyword string of an articlekeywords_str = "安企CMS,建站,SEO,Go语言"and we want to display these keywords separately.
{% set keywords_str = "安企CMS,建站,SEO,Go语言" %}
{% set keyword_list = keywords_str|split:"," %}
<ul>
{% for kw in keyword_list %}
<li>{{ kw|trim }}</li> {# 使用trim过滤器去除可能存在的空格 #}
{% endfor %}
</ul>
This code will first separate the string with commas, then iterate over the generated array, and display each keyword as a list item.
make_listFilter: the art of breaking down 'characters'.
WithsplitThe dependency separator is different.make_listThe way the filter works is more direct and pure: it treats each individual character in the string as an element and splits it into an array.It does not care about any delimiters, as if it were picking out each character, each letter, and each symbol from the string separately.
This filter is very suitable for those scenarios where you need to perform character-level operations on strings.For example, do you want to count how many characters (including Chinese characters) are in a string, or do you need to display each character of the string individually to achieve some special effect (such as typewriter effect, text reversal, etc.)?
make_listThe usage is more concise:{{ 你的字符串变量 | make_list }}.It automatically recognizes and handles various characters in strings, whether they are English letters, numbers, or complex Unicode characters (such as Chinese).In the template environment of Anqi CMS, it handles multibyte characters well, ensuring that each Chinese character is treated as a separate element.
Practical Example:Suppose we have a title `title_str = "AnQi CMS, Efficient Content Management"