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.
- Select
splitFilter: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. - Select
make_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