In the template world of AnQi CMS, we often need to process content in various ways, sometimes by paragraph, sometimes by sentence, and sometimes, we need to process text more meticulously, down to every character. When conventional string processing methods cannot meet this refined need, make_listThe filter can really shine here, as it can split a common string into an array of individual characters, providing unprecedented flexibility for template designers.

Understandingmake_listThe working principle of the filter

In simple terms,make_listThe filter receives a string as input, then treats each character (including letters, numbers, punctuation, even Chinese characters and spaces) as an independent element, and organizes these elements in order into an array. This process is similar to how Go language internally handles strings as[]runeType, ensures correct recognition and splitting of multi-byte characters (such as Chinese) without any garbled or truncated issues.

Its basic usage is very intuitive, simply pass the string to be processed through the pipe character|Pass tomake_listthe filter:

{# 假设我们有一个字符串变量 `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 handled correctly.

make_listWithsplitEach has its focus.

In AnQiCMS's filters, we also have another one.splitFilter, it can also split strings into arrays. Then,make_listandsplitwhat is the difference between them?

splitThe filter splits strings based on the 'delimiter' you specify. For example, if you want to split a string list by comma,splitit will be very convenient:

{% set tagsString = "安企CMS,模板,Go语言,CMS" %}
{% set tagsArray = tagsString|split(',') %}
{# tagsArray 结果为 ['安企CMS', '模板', 'Go语言', 'CMS'] #}

whilemake_listThen it does not rely on any delimiter at all; it simply treats each character in the string as an independent element. If your string does not have an explicit delimiter, or if the unit you need to process is a single character,make_listauto is the correct choice.

In short, when you need to split a string into 'words' or 'phrases' based on a 'pattern' (such as spaces, commas, etc.), choosesplitWhen you need to break a string down to the most basic 'character' units,make_listis your best choice.

make_listreal application scenarios: unlock new ways to handle text processing

Splitting a string into a character array brings many practical fine-grained processing possibilities for the template:

  1. Displaying characters individually or dynamic effects:Imagine, would you like each character of the title to appear independently, or add different styles to each character to achieve an animation effect?make_listThat 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.

  2. Customize character spacing or formatting:If you want to insert a specific separator between each character or to style some special characters separately,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 utilizeforloop.LastDetermine whether it is the last character to avoid adding extra spaces at the end.

  3. Simple text obfuscation or display:In scenarios where text display 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 AnQiCMS templates.It gives template designers the ability to finely control text content by splitting strings into an array of individual characters.make_listCan help you unlock more possibilities in AnQiCMS template development, making your website content display more flexible and personalized.


Common Questions (FAQ)

  1. make_listFilter can correctly handle strings containing Chinese characters?Yes,make_listFilter is based on the Go language[]runeMechanism implemented, which means it can correctly identify and handle multibyte characters, including Chinese characters.Each Chinese character will be treated as an independent element in the array, and there will be no truncation or garbled characters.

  2. make_listandsplitWhat is the main difference between filters? make_listIt is to split a string into each of its individual characters without specifying a delimiter; andsplitThe filter is based on one or more delimiters (such as commas, spaces, specific words, etc.) that you explicitly specify 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.

  3. Used in the templatemake_listWill splitting very long strings have an impact on website performance?For the general application scenarios of AnQiCMS templates, such as processing titles, short sentences, or medium-length article summaries,make_listand the subsequent,forThe impact of looping on server performance is negligible.AnQiCMS based on Go language, its template rendering efficiency is very high, and there is usually a cache mechanism.<span>Tags), 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.Suggest testing in combination with specific requirements and string length, and consider optimizing front-end rendering.