When using AnQi CMS for content management and template development, we often need to handle 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 think thatsplitCan it also handle special characters such as tabs (Tab) and newline characters (Newline) as delimiters?splitWhat is the effective delimiter of the filter? Today, we will delve into this topic in depth.
splitThe working principle of the filter in the security CMS
The template engine of Anqi CMS supports syntax similar to Django, which includessplitThe filter is designed to be quite flexible.The core function is to split a string into an array of strings according to the delimiter you specify (slice), which is very useful when dealing with structured data.splitcan help you easily extract them to further process or display in the template.
In the document, we seesplitThe basic usage of the filter is{{ obj|split:"分隔符" }}. Among them"分隔符"is a string, which means you can pass any legal string literal as a delimiter, not just a single character.
Unlock custom delimiter: Tab and New Line application
The good news is,splitThe filter fully supports using tabs and new lines as delimiters. The key is, when you aresplitThe filter provides a delimiter parameter, where you can pass the escaped forms of these special characters.
Use the tab (Tab)\t) as the delimiter
Imagine you have a product list imported from an external system, where each product's attributes are separated by tabs, such as: "Product Name\tPrice\tStock". In this case, you cansplitUse directly in the filter"\t"Parse these data as delimiters.
{# 假设 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 a string by tabs into an array containing three elements, and you can access each part by index.
using the newline (Newline)\nor\r\n) as the delimiter
Similarly, if you have a multi-line text input box, the user may have entered multiple entries separated by lines, or you may have read multi-line data from a file.You can use the newline character as a delimiter to extract each line content as an independent array element.\n(LF, Line Feed) is the main line feed character. Windows systems commonly use it.\r\n(CRLF, Carriage Return Line Feed). To handle these differences more robustly, it is usually used"\n"As a delimiter, it is sufficient, as it will handle\r\nof\nPart.
{# 假设 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 separate lines and|trimThe filter removes any extra whitespace that may exist on each line, ensuring the output is neat.
Further flexibility: the empty separator withmake_list
The document also mentions an interesting feature: ifsplitThe filter receives an empty string""as a delimiter, it will be very special and split on each UTF-8 character. This is similar tomake_listthe filter's effect.make_listis also an array of individual characters split from a string. For example:
`twig {# Use empty delimiter to split by character #} {% set charsBySplit = "Hello World"|split