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