Can the `split` filter use tab or newline characters as delimiters, in addition to commas and spaces?

Calendar 👁️ 68

When using Anq CMS for content management and template development, we often need to process strings and split them into smaller data segments according to specific rules.splitThe filter is undoubtedly an important tool to achieve this goal. However, many friends may habitually thinksplitCan only handle common separators such as commas and spaces. Then, can tab (Tab) and newline (Newline) characters also be used assplitWhat is the effective delimiter of the filter? Today, let's delve into this topic in depth.

splitThe working principle of the filter in Anqi CMS

The Anqi CMS template engine supports syntax similar to Django, among whichsplitThe filter is designed to be quite flexible. Its core function is to split a string into an array of strings based on the delimiter you specify (slice), which is very useful when processing structured data.For example, you have a text field containing multiple keywords, which are separated by specific symbols,splitit can help you easily extract them, so that they can be further processed or displayed in the template.

In the document, we seesplitThe basic usage of the filter is{{ obj|split:"分隔符" }}."分隔符"It is a string, which means you can pass any valid string literal as a delimiter, not just a single character.

Unlock custom delimiter: the application of tab and newline characters

The good news is,splitThe filter fully supports using tab and newline characters as delimiters. The key is, when you aresplitWhen the filter provides a delimiter parameter, you can pass the escaped forms of these special characters.

Use the tab (Tab\t) as a delimiter

Imagine you have a product list imported from an external system, where the attributes of each item are separated by tab characters, for example: "Product Name Price Stock". In this case, you cansplitUse directly in the filter"\t"Parse these data as a delimiter.

{# 假设 productData 是从后台获取的字符串,内容类似 "笔记本电脑\t9999.00\t100" #}
{% set productData = "笔记本电脑\t9999.00\t100" %}
{% set productDetails = productData|split:"\t" %}

<p>商品名称: {{ productDetails[0] }}</p>
<p>价格: {{ productDetails[1] }}</p>
<p>库存: {{ productDetails[2] }}</p>

This code will split the string by tab into an array containing three elements, and then you can access each part by index.

Using newline (Newline\nor\r\n) as a delimiter

Likewise, if you have a multi-line text input box, the user may have entered multiple entries separated by lines, or you may have read multiple lines of data from a file.You can use the newline character as a delimiter to extract each line of content as an independent array element.In most Unix/Linux systems and Go language environments,\n(LF, Line Feed) is the main line feed character. It is commonly used in Windows systems.\r\n(CRLF, Carriage Return Line Feed). In order to handle these differences more robustly, it is usually used"\n"It is sufficient as a separator because it handles it\r\nof\nSection.

{# 假设 multiLineText 是从后台获取的多行文本,内容类似 "第一行内容\n第二行内容\r\n第三行内容" #}
{% set multiLineText = "第一行内容\n第二行内容\r\n第三行内容" %}
{% set lines = multiLineText|split:"\n" %}

<ul>
{% for line in lines %}
    {# 使用 |trim 过滤器去除每行可能存在的额外空白字符(如 \r),并检查是否为空行 #}
    {% if line|trim|length > 0 %}
        <li>{{ line|trim }}</li>
    {% endif %}
{% endfor %}
</ul>

This code will split multi-line text into individual lines and through|trimThe filter removes any extra whitespace that may be present on each line, ensuring the output is neat.

Further flexibility: with the empty separator andmake_list

The document also mentions an interesting feature: ifsplitThe filter receives an empty string""As a delimiter, it will split very specially on each UTF-8 character. This is similar tomake_listThe filter has a similar effect,make_listIt is also an array of individual characters of a string. For example:

"twig {# Use whitespace delimiter to split characters #} {% set charsBySplit = "Hello World"|split

Related articles

Do `split` filters have special considerations when processing data for different sites in the AnQiCMS multi-site environment?

Are there special considerations when the `split` filter processes data for different sites in the AnQiCMS multi-site environment?Under the multi-site management capability of AnQiCMS, we often need to display content and handle data between different sites.The `split` filter is a basic and powerful string processing tool in the template engine and is naturally used frequently.

2025-11-08

Does the `split` filter affect the performance of template rendering when cutting large strings or complex data?

In website operations and template development, we often make use of the various powerful and flexible template filters provided by AnQiCMS to process data, among which the `split` filter is popular for its ability to easily split strings into arrays.However, when dealing with large strings or complex data structures, some users may wonder whether the `split` filter will affect the performance of template rendering.To deeply understand this problem, we first need to talk about the core technology stack of AnQiCMS.

2025-11-08

How to combine the `split` filter with the `if` logical judgment tag to perform different operations based on the cutting results?

In website content management, we often encounter a situation where a field stores a string of data separated by a specific symbol, and we need to perform different operations based on the results after the data is split.For example, an article tag field may store “SEO, operation, content marketing”, or a product attribute field may store “Color: red, size: L”.The AnQi CMS template engine provides powerful `split` filters and `if` logical judgment tags, which can be used together to achieve this requirement in a very flexible manner.

2025-11-08

`split` filter when splitting a numeric string, such as `"1_2_3_4"`, will the array elements remain as numeric type or string type?

When using AnQi CMS for website content management and template development, flexibly using built-in filters is the key to improving efficiency.Among them, the `split` filter is highly valued for its practicality in handling string splitting.Many users wonder when dealing with strings like `"1_2_3_4"` containing numbers, what type the array elements will be after splitting with the `split` filter.

2025-11-08

How to get the first or last element of an array split by the `split` filter?

When managing content in AnQi CMS, we often encounter the need to process some text information stored in a specific format.For example, the keywords of an article are usually stored in a field in the form of a comma-separated string, such as 'AnQi CMS, content operation, template making'.When we want to highlight the most important keyword on the website front end, such as an article list or detail page, or to obtain the last keyword as a hint, we need to split these strings and extract the specific elements.

2025-11-08

How would the `split` filter handle consecutive delimiters in a string, for example `"a,,b"` using a comma as the delimiter?

AnQi CMS has always been favored by users for its flexibility and powerful functions in content display and management.When dealing with dynamic content, we often encounter the need to split strings and extract information.At this time, the `split` filter in the template is particularly practical.It can help us split a continuous string of text data into independent segments according to the specified delimiter, so that it can be displayed and processed more finely on the page.

2025-11-08

Does the `split` filter support more complex delimiter patterns, such as matching specific prefix and suffix characters?

In AnQiCMS template development, the `split` filter is a very useful tool that can help us split strings into arrays according to specified delimiters, which is especially convenient for handling data connected by specific characters.However, when it comes to more complex delimiter patterns, such as when it is necessary to match specific prefixes and suffixes of characters, the capabilities of the `split` filter are worth exploring in depth.

2025-11-08

How to pass the array elements split by the `split` filter as parameters to other template tags or filters?

The template system of AnQiCMS (AnQiCMS) is favored by content operators for its powerful flexibility and ease of use.Among them, the `split` filter is a very practical feature that can help us split a string into an array (or called a slice in Go language) according to the specified delimiter.However, when we want to pass a specific part of this array as a parameter to other template tags or filters, we may feel a bit confused.Don't worry, this article will discuss in detail several efficient and commonly used methods to solve this problem.--- ###

2025-11-08