In the world of Anqi CMS templates, we often need to process content in various ways, sometimes by paragraphs, sometimes by sentences, and sometimes, we need to refine the text in more detail, down to every character. When conventional string processing methods fail to meet this refined requirement, make_listThe filter can really shine, it can break a common string into an array of individual characters, providing unprecedented flexibility for template designers.
Understandingmake_listThe principle of the filter.
In simple terms,make_listThe filter takes a string as input, then treats each character (including letters, numbers, punctuation marks, even Chinese characters and spaces) as an independent element, and arranges these elements in order into an array. This process is similar to how Go language internally handles strings as[]runeEnsure proper identification and splitting of multi-byte characters (such as Chinese) without any garbled or truncated text.
Its basic usage is very intuitive, just pass the string to be processed through the pipe symbol|pass tomake_listfilter:
{# 假设我们有一个字符串变量 `myString` 值为 "安企CMS真棒!" #}
{% set myString = "安企CMS真棒!" %}
{# 使用 make_list 过滤器将其转换为字符数组 #}
{% set charArray = myString|make_list %}
{# 此时,charArray 就是一个包含了 ['安', '企', 'C', 'M', 'S', '真', '棒', '!'] 的数组 #}
{# 我们可以通过 join 过滤器验证其内容,例如: #}
{{ charArray|join:', ' }}
{# 输出: 安, 企, C, M, S, 真, 棒, ! #}
As you can see,make_listSuccessfully split the string into a set of individual characters, even Chinese characters are processed correctly.
make_listwithsplitEach has its own focus
In the AnQiCMS filter, we also have anothersplitA filter that can also split strings into an array. So,make_listandsplitWhat is the difference between them?
splitThe filter splits the string based on the separator you specify. For example, if you want to split a string list by commasplitit will be very convenient:
{% set tagsString = "安企CMS,模板,Go语言,CMS" %}
{% set tagsArray = tagsString|split(',') %}
{# tagsArray 结果为 ['安企CMS', '模板', 'Go语言', 'CMS'] #}
Andmake_listIt does not depend on any separator; it simply treats each character in the string as an independent element. If your string does not have an explicit separator, or if the unit you need to process is a single character, thenmake_listIt is the correct choice.
In short, when you need to split a string into 'words' or 'phrases' based on a certain 'pattern' (such as spaces, commas, etc.), choosesplitWhen you need to break a string down into the most basic 'character' units,make_listit is your best choice.
make_listactual application scenarios: unlock new styles of text processing
The string is split into an array of characters, bringing many practical fine-grained processing possibilities to the template:
Displaying character by character or dynamic effects:Imagine, you want each character of the title to appear independently, or add different styles to each character to achieve an animation effect,
make_listIt is the starting point to achieve these effects.<h1 class="fancy-title"> {% set title = "欢迎使用安企CMS" %} {% for char in title|make_list %} <span class="char-animation">{{ char }}</span> {% endfor %} </h1>With CSS or JavaScript, you can make each character have a unique visual representation.
Customize character spacing or formatting:If you want to insert a specific separator between each character, or to style certain special characters individually,
make_listit also provides convenience.{% set productCode = "AQ-CMS-2023" %} {% for char in productCode|make_list %} {{ char }}{% if not forloop.Last %} {% endif %} {% endfor %} {# 输出: A Q - C M S - 2 0 2 3 #}Here we utilize
forloop.LastDetermine if it is the last character to avoid adding extra spaces at the end.Simple text obfuscation or display:In scenarios where text needs to be weakened or obscured, such as displaying email addresses, you can replace or hide some characters.
{% set email = "[email protected]" %} {% for char in email|make_list %} {% if forloop.Counter0 > 4 and forloop.Counter0 < email|length - 4 %} {# 隐藏中间部分字符 #} * {% else %} {{ char }} {% endif %} {% endfor %} {# 输出: admi*****le.com #}
Summary
make_listThe filter is a small but powerful tool in the AnQiCMS template.It assigns the ability to template designers to finely control text content by splitting strings into an array of individual characters.In order to achieve dynamic visual effects, or to perform character-level text processing, mastery ofmake_listCan help you unlock more possibilities in AnQiCMS template development, making your website content display more flexible and personalized.
Frequently Asked Questions (FAQ)
make_listCan the filter correctly handle strings containing Chinese characters?Yes,make_listThe filter is based on Go language[]runeMechanically implemented, this means it can correctly identify and handle multi-byte characters, including Chinese characters.Each Chinese character is treated as an independent element in the array, there will be no truncation or garbled characters.make_listandsplitWhat is the main difference between filters?make_listIt is to split a string into each individual character that makes it up, without specifying a delimiter; andsplitThe filter is based on the one or more delimiters you explicitly specify (such as commas, spaces, specific words, etc.) to split a string into an array of multiple substrings (words or phrases). In short,make_listIs split by characters,splitIs split by substrings or patterns.Using the template
make_listWill splitting a very long string and iterating over it in a loop affect website performance?For the general application scenarios of AnQiCMS templates, such as handling titles, short sentences, or medium-length article abstracts,make_listand the subsequentforThe impact of loops on server performance is negligible. AnQiCMS is based on Go language, its template rendering efficiency is very high, and it usually has a caching mechanism.However, if the string being processed is extremely long (for example, the entire article content is rendered word by word to tens of thousands of<span>In the tag), this may increase the burden on the front-end browser rendering, but the impact on the back-end server template engine is usually not significant.It is recommended to test in practice, combining specific requirements and string length, and considering front-end rendering optimization.