The 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.This is in the template,splitThe filter is particularly useful. It helps us split a continuous string of text data into independent segments according to the specified delimiter, so that we can display and process it more finely on the page.

UnderstandingsplitBasic usage of filters

splitThe core function of the filter is to split a string into an array (or more accurately, a slice) according to the delimiter you provide. Imagine that you might have a list of article tags connected by commas, such as"SEO优化,内容营销,网站安全". If you want to display these tags one by one, you needsplitthe help of the filter.

Its basic usage is very intuitive, usually{{ 您的字符串变量 | split:"分隔符" }}.

For example, suppose we have atagsThe variable stores a comma-separated string of tags:

{% set tags = "安企CMS,Go语言,内容管理" %}
{% set tagList = tags|split:"," %}

<p>文章标签:</p>
<ul>
{% for tag in tagList %}
    <li>{{ tag }}</li>
{% endfor %}
</ul>

After this code is executed, each tag will be clearly displayed on the page:

文章标签:
- 安企CMS
- Go语言
- 内容管理

In-depth discussion: When multiple separators appear consecutively in a string:

While usingsplitWhen filtering, a common issue is how the system will handle consecutive delimiters in a string. For example, when you have a string like"a,,b"Such a string, when trying to split with a comma as a delimiter,splitThe filter will very clearly keep the empty content between each delimiter as a separate element.

This means"a,,b"aftersplit:","After processing, you will get an array containing three elements:["a", "", "b"]. A blank string in the middle""This behavior ensures that every 'slot' in the original string is considered, even if that slot is empty.

Let us experience this through a specific example:

{% set exampleString = "apple,,banana,," %}
{% set fruitParts = exampleString|split:"," %}

<p>原始字符串: "{{ exampleString }}"</p>
<p>分割结果(包含空元素):</p>
<ul>
{% for part in fruitParts %}
    <li>元素: "{{ part }}" (长度: {{ part|length }})</li>
{% endfor %}
</ul>

Run this code, and you will see the following output:

原始字符串: "apple,,banana,,"
分割结果(包含空元素):
- 元素: "apple" (长度: 5)
- 元素: "" (长度: 0)
- 元素: "banana" (长度: 6)
- 元素: "" (长度: 0)
- 元素: "" (长度: 0)

As you can see,splitThe filter faithfully records all content between delimiters, including those that are empty.This design is very useful for those who need to control the data structure accurately, or distinguish between 'having a null value' and 'not having that position' in specific scenarios.

If you do not want to process these empty elements, you can filter them out by simple condition judgment while traversing the array:

{% set exampleString = "apple,,banana,," %}
{% set fruitParts = exampleString|split:"," %}

<p>过滤空元素后的分割结果:</p>
<ul>
{% for part in fruitParts %}
    {% if part != "" %}
        <li>元素: "{{ part }}"</li>
    {% endif %}
{% endfor %}
</ul>

In this way, only non-empty elements will be displayed on the page:

过滤空元素后的分割结果:
- 元素: "apple"
- 元素: "banana"

splitThe other cleverness of the filter

Besides handling consecutive delimiters,splitThe filter also has some notable behaviors:

  • The separator does not exist:If the specified delimiter is not found at all in the original string,splitThe filter will not cause an error, but will return the entire original string as a single element, forming an array that contains only one element. For example,"hello world"|split:","The result is["hello world"].
  • Empty separators:When you enter an empty string""pass as a delimiter tosplitWhen used as a filter, it treats each UTF-8 character of the original string as a separate element for splitting. For example,"安企CMS"|split:""you will get["安", "企", "C", "M", "S"]such an array. There is also one in the template for this character-based splitting requirementmake_listfilter, whose behavior is similar tosplitvery similar when using the space separator, and they can complement each other

tosplitwithjoincombined use

splitThe filter splits a string into an array, andjoinThe filter works in the opposite way - it concatenates the elements of an array (slice) into a new string with the delimiter you specify.These filters are often used in pairs, providing flexible data transformation capabilities.

For example, you may first usesplitAfter extracting multiple tags from a string, you then need to concatenate them into a new string, at this pointjoinIt can be put to use:

{% set rawTags = "安企CMS,Go语言,,内容管理" %}
{% set processedTags = rawTags|split:"," %} {# 此时包含空元素 #}

{% set filteredTags = [] %}
{% for tag in processedTags %}
    {% if tag != "" %}
        {% set filteredTags = filteredTags|add:tag %} {# 假设 add 过滤器可以向数组中添加元素 #}
    {% endif %}
{% endfor %}

<p>过滤后的标签列表: {{ filteredTags|join:" | " }}</p>

(Note: According to the AnQiCMS filter document,addThe filter is mainly used for adding numbers or strings, and does not directly support adding elements to an array.This is for demonstration purposes, in fact, in the AnQiCMS template, it may be necessary to use more complex macros or complete it in the background logic.

Application scenarios in practice

splitThe filter has a wide range of applications in the content operation of AnQiCMS:

  • Dynamic tag display:Split the tag string preset for the article or product, and then display it as a beautiful tag cloud through a loop.
  • Multiple value field processing:For fields defined in the background that allow users to enter multiple values (such as product features, service lists), throughsplitthe filter can easily parse these values and display them independently.
  • Data parsing and formatting:When data imported from external sources or stored in the background is a string with a specific delimiter,splitCan help us convert it into an operable data structure, making it easier for further formatting or filtering.

In general,splitThe filter is a powerful assistant for processing string data in Anqi CMS templates.Understand its precise behavior in handling consecutive delimiters, as well as its coordination with other filters, which can make it more agile and flexible in building dynamic content presentations.


Frequently Asked Questions (FAQ)

1. How to avoidsplitEmpty strings appear in the filter results?

You can iterate oversplitUse the filter to return the array when{% if item != "" %}such conditional judgment to skip empty elements. Or, if your string may contain consecutive delimiters before cutting and you want to merge them, you cansplitBefore, throughreplaceThe filter will,,Replace with a single,For exampleyourString|replace:",,,,"|split:","But this needs to be adjusted flexibly according to the actual situation.

2.splitFilters andmake_listWhat are the differences between filters?

splitThe filter is based on the one or more characters you explicitly specify as a 'delimiter' to split strings. For example, 'a'