In the daily content operation of Anqi CMS, we often encounter the need to process data separated by specific symbols (such as commas).This data may come from custom fields, imported CSV files, or information organized into a single line to keep the content concise.When we need to split this data that looks like a "whole piece" into independent items that can be displayed or processed individually, the template engine built into AnQi CMS provides a very practical tool——splitfilter.
splitThe filter can help us easily split a string into an iterable array (usually referred to as a slice or list in programming) by a specified delimiter. This means that the data originally piled together can besplitAfter processing, it can be displayed as independent entries in a loop on the web page, greatly enhancing the flexibility and display effect of the content.
splitBasic usage of the filter
splitThe filter is very intuitive to use. Its basic syntax is{{ 字符串变量|split:"分隔符" }}Among them, the 'string variable' is the original string you want to split, and the 'delimiter' is the character or string you use to tell the system where to split.
Imagine you have a custom field in an article where you record a series of keywords, such as 'AnQi CMS, content management, website optimization, template development'. You want to display these keywords one by one as small tags. First, we need to get this string:
{% set keyword_string = "安企CMS,内容管理,网站优化,模板开发" %}
Then, we can usesplitFilter to split it into an array:
{% set keyword_array = keyword_string|split:"," %}
at this point,keyword_arrayIt already contains an array with the elements 'AnQi CMS', 'Content Management', 'Website Optimization', 'Template Development'. To display them on the page, we usually combineforLoop labels to iterate over this array:
<div class="tags-container">
{% for tag in keyword_array %}
<span class="tag-item">{{ tag }}</span>
{% endfor %}
</div>
By such a combination, the original line of text becomes multiple independent labels, each of which can be rendered separately, making the page content more dynamic and friendly.
Some tips for practical application
In the actual operation, you may encounter some details issues, here are some tips to help you better utilizesplitFilter:
Handling spaces before and after delimiters:In many cases, comma-separated strings have a space after the comma, for example
"苹果, 香蕉, 橘子". If used directly|split:",", then the split elements will contain extra spaces (such as" 香蕉")。This is when we cansplituse it aftertrimA filter to remove whitespace from each element.{% set fruits_string = "苹果, 香蕉, 橘子" %} {% set fruit_list = fruits_string|split:"," %} <ul> {% for fruit in fruit_list %} <li>{{ fruit|trim }}</li> {# 使用 trim 过滤器去除空格 #} {% endfor %} </ul>trimThe filter will remove all leading and trailing spaces, tabs, and new lines from the string, ensuring that each element is clean.String that does not contain delimiters:If your string does not contain the separator you specified,
splitHow will the filter handle it? It's simple, it will return an array containing only the original string itself, with a length of 1.This is very useful when dealing with data that may or may not have delimiters, without the need for additional logical judgment.{% set single_item_string = "唯一的项" %} {% set item_array = single_item_string|split:"," %} {% comment %} item_array 现在是 ["唯一的项"] {% endcomment %} {% if item_array|length > 0 %} <p>{{ item_array[0] }}</p> {% endif %}Empty separators:When you use an empty string as a separator (
|split:"")splitThe filter will split the string into an array based on each UTF-8 character.This means, it will treat each Chinese character, letter, or symbol as an independent element.If you just want to split by characters, Anqi CMS also providesmake_listFilter, with a simpler usage and the same effect:{{ "你好世界"|make_list }}.joinThe inverse operation of the filter:It is worth mentioning,splitThe filter has a 'partner', that isjoinfilter.joinThe filter does the opposite thing, it can concatenate an array into a string using a specified delimiter.If you need to convert the format between content display and data storage, these two filters will be your good assistants.
By flexible applicationsplitThe filter, we can convert comma-separated data in AnQi CMS into structured arrays, thereby achieving richer and more dynamic content display. Whether it is product parameters, article tags, or any other list form of data,splitCan make your template logic clearer, and make the page display more attractive.
Frequently Asked Questions (FAQ)
Q1: If my string does not have a delimiter,splithow will the filter handle it?A1: If the string does not contain the specified delimiter,splitThe filter will return an array containing the original string itself, with a length of 1. For example,"AnQiCMS"|split:","The result is["AnQiCMS"].
Q2: Why do the elements I split out have extra spaces?A2: This is usually because the original string has spaces after the delimiter (such as a comma), like"选项一, 选项二". The solution is to specify the delimiter in thesplitAfter the filter is processed, use each element againtrimFilter, for example{{ item|trim }}to remove extraneous whitespace.
Q3: Can I usesplitthe result of the filter directly for conditional judgment?A3: Of course, you can.splitThe filter returns an array, you can usefora loop to iterate over it, or you can useifstatements to combinelengtha filter to check if the array is empty or the number of elements it contains, for example{% if my_array|length > 0 %}.