In the process of template creation in Anqi CMS, we often encounter scenarios where string splitting is required, such as extracting keywords from a description or parsing a tag string into individual words. Anqi CMS providessplitandfieldsThese two practical filters.Although they can all split strings into arrays, there are subtle yet crucial differences in their working methods and applicable scenarios when splitting strings by spaces.

splitFilter: Flexible Separator Expert

splitFilter, 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 strings.splitCan complete the splitting task according to your instructions.

For example, if you have a label string separated by commas and spaces“SEO, 内容营销, 网站优化”You can use|split:", "Come and easily split it into a containing“SEO”/“内容营销”/“网站优化”using a comma as a delimiter.

When it comes to splitting by spaces,splitIt can also handle it, you need to explicitly tell it to use a single space as a 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"Pass|split:" "After splitting,["Hello", "", "World"]The English translation of 'auto' is 'English'.This behavior may be useful in some scenarios that require precise control of delimiters, but in most scenarios where we extract words, we usually want to ignore these extra empty strings.

fieldsFilter: Designed to handle spaces

Compared tosplitUniversality,fieldsThe filter becomes more focused.Its original design was to facilitate the extraction of independent 'fields' or 'words' from a piece of text, and it defaults to splitting strings by spaces without any additional delimiter required.

fieldsThe most significant feature of the filter is its 'intelligent' handling of spaces.It will automatically treat one or more consecutive spaces as a single delimiter.“Hello AnQiCMS”This includes strings with multiple consecutive spaces,fieldsand can split them cleanly and neatly into“Hello”and“AnQiCMS”, without producing any empty strings. Moreover,fieldsThe string will automatically remove leading and trailing whitespace characters to ensure you get a neat, redundant-free word list.

Key differences and selection guide

UnderstoodsplitandfieldsThe working principle, we can better choose the filter that suits our own needs.

Summary of main differences:

  • Separator specification: splitYou need to explicitly specify the separator (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 delimiters:When encountering multiple consecutive delimiters,splitMay contain empty strings in the results;fieldsThen it will intelligently treat consecutive spaces as separators and automatically ignore them, without producing empty strings.
  • Leading and trailing whitespace characters: fieldsThe string will automatically remove leading and trailing whitespace characters,splitwhile it will not.

When to choosesplit:

When you need to split data precisely based on non-space characters such as commas, semicolons, dashes, and pipe symbols,splitIt is your first choice.It provides you with complete control over delimiters, capable of handling various complex structured data splitting needs.For example, parsing CSV 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/trailing whitespace characters in the text, fieldsThe filter will be a more convenient and efficient choice.It can directly return an array composed of clean words, which is very suitable for keyword extraction, article tagging or simple text analysis.

Actual code example

To more intuitively demonstrate the differences between them, let's look at several simple code examples:

auto

{# Use split filter #}

UsesplitFilter:

Split by a single space (may produce empty strings and retain leading/trailing whitespaces):

{% set splitBySpace = textString1|split:” “ %}

    {% for item in splitBySpace %}
    <li>'{{ item }}'</li> {# 注意这里使用单引号来显示字符串的实际内容,包括空字符串 #}
    {% endfor %}
    

{# English,‘Hello’,#}

Split by comma and space (handle non-space delimiters):

{% set splitByCommaSpace = textString2|split:", " %}

    {% for item in split