The `make_list` filter and the `split` filter are applicable to which scenarios in converting strings to character arrays in AnQiCMS templates?

Calendar 👁️ 61

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

splitFilter: Splitting strings based on delimiters

splitThe filter, as the name implies, splits a string into an array of substrings based on a specified delimiter.It is very suitable for processing data items connected by specific characters (such as commas, spaces, bars, etc.)In the daily operation of AnQiCMS, many background settings and content entry methods tend to use this structured string storage, at this timesplitFilters can really shine here.

Main applicable scenarios:

  • Processing keywords or tags (Tags) list:In AnQiCMS documents, products, or category management, there are often 'keywords' or 'Tag tags' fields.When a user enters information in the background, they will usually separate multiple keywords with commas or spaces.For example, the keywords of a blog post may be saved as"SEO优化,内容营销,网站运营"In the template, if you need to iterate over these keywords separately, generate a link or assign a specific style to each keywordsplitThe filter is **selection.

    {# 假设 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 to form["AnQiCMS", "模板开发", "过滤器"]such an array for easy iteration processing.

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

    {# 假设 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 items:Some AnQiCMS global configuration or module configuration, for brevity, a series of related data items may be stored in a single string, separated by a specific delimiter. For example, a site may store the list of allowed user roles as"admin|editor|contributor". When performing permission judgments or displaying role lists in templates,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: character-level string splitting

withsplitdifferent,make_listThe filter does not depend on any delimiter, it directly splits a string into a bya single characterThis forms an 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.This is very useful when performing fine-grained operations on strings, especially considering the good support for Unicode characters in the underlying Go language of AnQiCMS (in Go, therunetype 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).At this time, 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"to["安", "企", "C", "M", "S"]So that each character can be processed independently.

  • Generate a "word cloud" or unique layout:Some design requirements involve dispersing a segment of text to display it in a more creative manner, such as generating a 'word cloud' or randomly positioning each character.make_listCan provide a data foundation for this character-level free layout.

    {# 假设 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 user input or system-generated content on the front end, for example, to determine if it contains specific non-permitted characters or to perform character replacement.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 is usually combined with regular expressions or more完善的后端校验.)

When to choose which filter?

Selectmake_listOrsplitThe key is the granularity you want to "split" the string.

  • SelectsplitFilter:When you need to split a string according to a clearseparatorSplit into asub-string with business logic significance, please usesplit. It focuses on the "data blocks" inside the string.
  • Selectmake_listFilter:When you need to split a string intoA single independent characterTo process 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 is to handle structured data lists or to achieve creative character-level visual effects, they can find a stage to play their role.


Frequently Asked Questions (FAQ)

Q1:splitCan the filter use multiple delimiters to split a string at the same time?A1: Based on the 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 timessplitCombine orreplaceThe filter unifies all different delimiters into one before performing another onesplitOperation. For example, replace all spaces and semicolons in the string with commas, and then use commas to separatesplit.

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

Related articles

How to split a string containing multiple keywords in AnQiCMS template by spaces, commas, or a custom delimiter into an array?

In AnQiCMS (AnQiCMS) content management and template development, we often encounter scenarios where we need to process strings containing multiple keywords.For example, an article may have a comma-separated list of keywords, or product attributes are space-separated tags.Make full use of these data and display them flexibly in the template, you need to split these strings into traversable arrays precisely.AnQiCMS uses a template engine syntax similar to Django, providing powerful filter functions to handle such needs.Among, `split`

2025-11-08

In AnQiCMS template language, what are the similarities and differences between the `join` filter and other string concatenation methods, and what are their applicable scenarios?

In Anqi CMS template language, combining multiple strings or data fragments into a complete string is a very common requirement in front-end display.There are many ways to achieve this goal, each method has its unique application scenarios and advantages.Today, let's delve into the differences and similarities between the `join` filter and other commonly used string concatenation methods.### `join` filter: A bridge from array to string The `join` filter plays a very clear and efficient role in AnQiCMS template language

2025-11-08

How to customize fields in the AnQiCMS backend where a field stores multiple choices (an array), and clearly display them on the frontend page using the `join` filter?

The AnQi CMS provides great convenience for website operators with its flexible content model and powerful custom features.In daily content management, we often encounter situations where we need to add multiple selection properties to articles or products, such as a product may have multiple colors, different sizes, etc.How to display them in a clear and beautiful way on the front-end page when this information is stored in the background through custom fields in the form of multi-select values has become the problem we need to solve.### Understanding the Multi-Select Custom Fields in AnQi CMS On the AnQi CMS backend

2025-11-08

What will happen when the `join` filter encounters an array with mixed data types (such as strings and numbers)?

In Anqi CMS template development, the `join` filter is a very useful tool that can help us concatenate multiple elements of a list (array) into a complete string.This is particularly convenient when it is necessary to display a series of related data in a unified format, such as displaying multiple tags of articles, multiple characteristics of products, or multiple permissions of users.However, in practice, we sometimes encounter situations where the array to be concatenated contains different types of data, such as strings, numbers, and even boolean values. So

2025-11-08

How to get the actual length of a string or array in AnQiCMS template (number of characters or elements)?

When managing website content in AnQi CMS, you often encounter situations where you need to get the number of characters in text or determine how many elements are in a list or array.In order to control the page layout, ensure the display length of the title introduction, or dynamically adjust the display logic based on the amount of data, it is crucial to understand how to obtain this "length" information in templates for creating flexible and user-friendly websites.The AnQi CMS template engine provides a concise and powerful way to handle such requirements, with the most core being the `length` filter

2025-11-08

How to determine if the length of a variable in the AnQiCMS template matches the expected value and make a judgment in the conditional statement?

In website content management, flexibly controlling the way content is displayed is crucial for improving user experience and page aesthetics.AnQiCMS (AnQiCMS) provides a powerful template engine, allowing us to easily determine how to display page elements based on the characteristics of the content, such as the length of a variable.When you need to judge whether the length of a variable meets the expected requirements and perform different operations in the template based on this, the template tags and filters of Anqi CMS provide an intuitive and efficient solution.Flexible control of content display: The importance of length judgment Imagine one

2025-11-08

How to calculate the total number of times a specific keyword appears in a line string or an array in the AnQiCMS template?

In AnQiCMS (AnQiCMS) template development, we often need to flexibly handle various content on the page.For example, you may need to analyze the frequency of a specific word in an article, or check how many times an element is mentioned in a list.The AnQi CMS powerful template engine provides a variety of practical filters (Filter) that can help you easily meet these needs.The function used to calculate the total number of times a specific keyword or element appears is exactly the focus of our discussion today.### Core Function: `count`

2025-11-08

How to batch remove leading, trailing spaces or specific characters from AnQiCMS template strings for data cleaning and formatting?

When using AnQiCMS for website content management, we often encounter situations where we need to fine-tune the text output in templates.Whether it is data obtained from a database or content entered in an editor, it may contain extraneous spaces, line breaks, or even specific characters that are not intended to be displayed.In order to ensure the tidiness, consistency of website content, and to enhance user experience and search engine friendliness, it is particularly important to clean and format the data.AnQiCMS provides a flexible and powerful template engine, its syntax is similar to Django

2025-11-08