In AnQiCMS template development, we often need to process string type data, among which converting a string to an array is a common requirement. AnQiCMS' powerful template engine provides a variety of filters to assist in completing such tasks, wheremake_listandsplitFilter is a powerful tool for converting strings to arrays.Although they can both 'turn' a string into an array, there are essential differences in the application scenarios and conversion logic between the two.Understanding these differences can help us implement template functions more efficiently and accurately.

splitFilter: String splitting based on delimiter

splitThe filter, as the name suggests, splits a string into an array of substrings based on a delimiter you specify.It is very suitable for processing data items connected by specific characters (such as commas, spaces, bars, etc.).splitthe filter can make a big difference.

Main applicable scenarios:

  • Processing keywords or tags (Tags) list:In AnQiCMS's documentation, products, or category management, there are often 'keywords' or 'Tag tags' fields.Users often use commas or spaces to separate multiple keywords when entering in the background."SEO优化,内容营销,网站运营"In the template, if you need to traverse these keywords individually, generate a link or assign a specific style to each keyword.splita filter is used to **select.

    {# 假设 item.Keywords 是从后台获取的关键词字符串,例如 "AnQiCMS,模板开发,过滤器" #}
    {% set keywordsArray = item.Keywords|split:"," %}
    <div class="article-tags">
        {% for tag in keywordsArray %}
            <a href="/tag/{{ tag|urlencode }}" class="tag-item">{{ tag }}</a>
        {% endfor %}
    </div>
    

    Here,split:","Split the string by comma.["AnQiCMS", "模板开发", "过滤器"]This array is convenient for us to process in a loop.

  • Parse the multiple choice fields in the custom content model:If you have customized the content model in the AnQiCMS backend and it contains a multi-choice (checkbox) type field, the stored values are often also separated strings with specific symbols (such as commas). When you need to display these multi-choice values one by one on the front end, splitThe filter can help you easily implement it.

    {# 假设 archive.Features 是多选字段,例如 "高性能,多站点,SEO友好" #}
    {% set featuresList = archive.Features|split:"," %}
    <ul class="product-features">
        {% for feature in featuresList %}
            <li>✅ {{ feature }}</li>
        {% endfor %}
    </ul>
    
  • Process the list data in the configuration options:Some AnQiCMS global configurations or module configurations may store a series of related data items in a string for simplicity, separated by a specific delimiter. For example, a site may store the list of allowed user roles as"admin|editor|contributor"In the template, when performing permission judgment or displaying the role list,splitthe filter can effectively extract this information.

    {# 假设 system.AllowedRoles 是一个竖线分隔的字符串 #}
    {% set allowedRoles = system.AllowedRoles|split:"|" %}
    <p>该系统允许的角色有:</p>
    <ul>
        {% for role in allowedRoles %}
            <li>{{ role }}</li>
        {% endfor %}
    </ul>
    

make_listFilter: string splitting at the character level

Withsplitdifferent,make_listFilter does not depend on any delimiter, it directly splits a string into a sequence ofindividual charactersThe composed array.This means that every character (including Chinese characters, letters, numbers, symbols, and even spaces) in the string will become an independent element in the array.runetype represents a Unicode character).

Main applicable scenarios:

  • Implement text animation or special visual effects:Imagine a website title, where each character can perform an independent animation effect (such as appearing one by one or changing colors).This is when you need to split the title string into individual characters and then apply styles or animations to each character.

    {# 假设 archive.Title 是文档标题,例如 "安企CMS" #}
    <h1 class="animated-title">
        {% for char in archive.Title|make_list %}
            <span class="char-animation">{{ char }}</span>
        {% endfor %}
    </h1>
    

    Here,make_listto"安企CMS"Converted to["安", "企", "C", "M", "S"]So that each character can be processed independently.

  • Generate a 'word cloud' or unique layout:Some design requirements call for breaking up a segment of text to display in a more creative way, such as generating a 'word cloud' or randomly positioning each character.make_listThis can provide a data foundation for free layout at the character level.

    {# 假设 category.Description 是分类描述 #}
    <div class="description-cloud">
        {% for char in category.Description|make_list %}
            <span class="random-position-char">{{ char }}</span>
        {% endfor %}
    </div>
    
  • Verification or processing of specific character sets:Although AnQiCMS usually has built-in input validation, in some advanced template scenarios, you may need to check each character of the user input or system-generated content on the frontend, for example, to determine if it contains specific non-allowed characters or to replace characters.make_listCan provide character-level access capabilities.

    {# 检查用户名是否包含数字 #}
    {% set hasNumber = false %}
    {% for char in user.Name|make_list %}
        {% if char|integer is not sameas 0 %} {# 尝试将字符转为数字,如果成功且不为0,则认为是数字 #}
            {% set hasNumber = true %}
        {% endif %}
    {% endfor %}
    {% if hasNumber %}
        <p class="warning">用户名中不应包含数字!</p>
    {% endif %}
    

    (Note: This character validation example is rather simple, and in actual applications, it would usually be combined with regular expressions or more comprehensive backend validation.)

When to choose which filter?

Choosemake_listOrsplitThe key is the granularity of the string you want to "split".

  • ChoosesplitFilter:When you need to split a string according to a clearSeparatorsplit into meaningful substringsbusiness logicusesplitIt focuses on the "data blocks" inside the string.
  • Choosemake_listFilter:when you need to split a string intoA single independent characterTo be processed without a specific delimiter concept, please usemake_list. It focuses on the 'constituent elements' of the string.

In AnQiCMS template development, these two filters provide flexible string processing capabilities. Whether it's dealing with structured data lists or implementing creative character-level visual effects, you can find their stage of action.


Common Questions and Answers (FAQ)

Q1:splitFilter can use multiple delimiters to split strings at the same time?A1: Based on AnQiCMS template engine'ssplitFilter design, it currently only supports specifying a string as a delimiter. If you need to split the same string using multiple different delimiters, you may need to do so multiple times.splitCombine orreplaceThe filter first unifies all different delimiters to the same one, and then performs anothersplitOperation. For example, replace all spaces and semicolons in a string with commas, and then use commas forsplit.

Q2:make_listDoes the filter split Chinese character strings correctly during processing?A2: Yes,make_listFilter performs well when processing Chinese string.AnQiCMS is developed based on Go language, which has native good support for Unicode characters. Its character type (rune) can correctly represent and process multi-byte characters including Chinese.make_listwill split each Chinese character in the Chinese string as an independent character element, and will not