During the template creation process in AnQi CMS, we often encounter scenarios where we need to split strings, such as extracting keywords from a description or parsing label strings into independent words. AnQi CMS providessplitandfieldsThese two practical filters. Although they can both split strings into arrays, they have subtle and critical differences in how they work and their application scenarios when splitting strings by spaces.
splitFilter: The Flexible Separator Expert
splitThe filter, as the name implies, is used to 'split' strings.Its strength lies in its high versatility - you can specify any delimiter you want to split the string.This means, whether it is a comma, semicolon, slash, or any other custom character,splitCan all be split according to your instructions.
For example, if you have a label string separated by commas and spaces.“SEO, 内容营销, 网站优化”, you can use|split:", "Easily split it into a container that includes“SEO”/“内容营销”/“网站优化”array.
When it comes to splitting by spaces,splitIt can also handle it, you need to explicitly tell it to use a single space as the delimiter, for example|split:" "However, there is an important detail to note: if your string contains multiple consecutive spaces,splitThe filter treats each space as a separate delimiter. This means that in the split result, you may get some empty strings. For example,"Hello World"By|split:" "After splitting, you will get["Hello", "", "World"]The empty string is caused by consecutive two spaces.This behavior may be useful in some scenarios where precise control of delimiters is required, but in most cases where extracting words, we usually want to ignore these extra empty strings.
fieldsFilter: Designed to handle spaces
Compared tosplit universality,fieldsThe filter is more focused. Its design intention is to easily extract independent 'fields' or 'words' from a text, and it defaults to splitting strings by spaces, without requiring you to specify a delimiter.
fieldsThe most significant feature of the filter is its 'intelligent' handling of spaces.It automatically treats one or more consecutive spaces as a single delimiter.This means, whether it is a single space, or like“Hello AnQiCMS”Such a string containing multiple consecutive spaces,fieldscan be cleanly and neatly split into“Hello”and“AnQiCMS”without producing any empty strings. Moreover,fieldsIt will automatically remove leading and trailing whitespace characters, ensuring you get a neat, redundant word list.
Key differences and selection guide
UnderstoodsplitandfieldsThe principle, we can better choose filters that meet our own needs.
Summary of main differences:
- Specify delimiter:
splitYou need to specify the delimiter explicitly (it can be any string);fieldsIt is not necessary to specify, it defaults and splits only by spaces (including tab characters, newline characters, and other whitespace characters). - Handling of consecutive separators:When multiple consecutive separators are encountered,
splitMay contain empty strings in the result;fieldsWill intelligently treat consecutive spaces as separators and automatically ignore them, not producing empty strings. - Leading and trailing whitespace:
fieldsIt will automatically remove whitespace characters from the beginning and end of a string, whilesplitit will not.
When to choosesplit:
When you need to split data precisely based on non-space characters such as commas, semicolons, dashes, and pipes,splitIt is your first choice. It provides you with complete control over delimiters, able to handle various complex structured data splitting needs.For example, parsing CSV formatted data, components of URL paths, or lists of IDs in a specific format.
When to choosefields:
When your main goal is to extract words or phrases from a natural language text and you want the filter to intelligently handle multiple consecutive spaces or leading and trailing blank characters in the text,fieldsThe filter will be a more convenient and efficient choice. It can directly return an array consisting of clean words, which is very suitable for keyword extraction, article tagging, or simple text analysis.
Actual code example
To illustrate the difference between the two more intuitively, let's look at several simple code examples:
"twig {% set textString1 = " Hello AnQiCMS World " %} {% set textString2 = "apple, banana, orange" %} {% set textString3 = "one-two-three" %}"}
Use split filter
UsesplitFilter:
Split by a single space (may produce empty strings and retain leading and trailing whitespaces):
{% set splitBySpace = textString1|split: " " %}{% for item in splitBySpace %}
<li>'{{ item }}'</li> {# 注意这里使用单引号来显示字符串的实际内容,包括空字符串 #}
{% endfor %}
Hello
AnQiCMS
World
Split by comma and space (handle non-space delimiters):
{% set splitByCommaSpace = textString2|split:", " %}{% for item in split