AutoCMS has always been favored by users for its flexibility and powerful features in content display and management.When dealing with dynamic content, we often encounter the need to split strings and extract information.splitThe filter is particularly practical.It can help us split a continuous sequence of text data into independent segments according to the delimiter we specify, so that we can display and process it more finely on the page.
UnderstandingsplitBasic usage of the filter
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 may have a list of article tags connected by commas, like"SEO优化,内容营销,网站安全"If you want to display these tags one by one, you needsplitfilter help.
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 exploration: When multiple separators occur consecutively in a string
When usingsplitA common issue when filtering is how the system will handle consecutive delimiters within a string. For example, when you have a string like"a,,b"such a string, and when trying to split it using commas as delimiters,splitthe filter will very clearly retain each empty content between delimiters as an independent element.
This means"a,,b"Aftersplit:","After processing, you will get an array containing three elements:["a", "", "b"]. An empty string in the middle""This will be considered a valid, but empty element. This behavior ensures that every 'slot' in the original string is considered, even if that slot is empty.
Let us feel it 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 accurately records all content between delimiters, including the empty parts.This design is very useful for those who need to precisely control data structures, or distinguish between 'null value' and 'no such 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 clever aspects of the filter
In addition to handling consecutive delimiters,splitThe filter also has some notable behaviors:
- The delimiter does not exist:If the delimiter you specify is not found at all in the original string,
splitThe filter will not report an error, but will return the entire original string as a single element, forming an array containing only one element. For example,"hello world"|split:","The result is["hello world"]. - Empty delimiter:When you have an empty string
""Passing as a delimiter,splitWhen used as a filter, it will treat each UTF-8 character in the original string as a separate element for splitting. For example,"安企CMS"|split:""You will get["安", "企", "C", "M", "S"]such an array. For the need to split by characters, there is also a template for this.make_listfilter, whose behavior issplitvery similar to using an empty separator, and they can complement each other.
tosplitWithjoincombined use
splitThe filter splits a string into an array, whilejoinThe filter does the opposite work—it concatenates the elements of an array (slice) into a new string using the delimiter you specify.These two filters are often used together, providing flexible data conversion capabilities.
For example, you may first usesplitmultiple tags are extracted from a string, processed, and then need to be concatenated into a new string. At this pointjoinit comes in handy:
{% 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,)addFilter is mainly used for adding numbers or strings, and does not directly support adding elements to an array.Here is for demonstration of logic, actually in AnQiCMS template, it may require more complex macros or background logic to construct a new array directly.
Actual application scenarios
splitFilters are widely used in the content operation of AnQiCMS:
- Dynamic tag display:Split the predefined tag string of the article or product, and then display it as a beautiful tag cloud through a loop.
- Multiple value field processing:For fields defined by the backend that allow users to input multiple values (such as product features, service lists),
splitthe filter can easily parse these values and display them independently. - Data parsing and formatting:When the data imported from external sources or from background storage is a string with a specific delimiter,
splitIt can 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 the Anqi CMS template.Understand its precise behavior in handling continuous delimiters, as well as its配合使用 with other filters, which will allow you to be more agile when building dynamic and flexible content displays.
Common Questions (FAQ)
How to avoidsplitAre there empty strings in the filter results?
You can traversesplitThe filter returns an array, use{% 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 cansplitPreviously, 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 one or more characters you explicitly specify as a 'delimiter' to split strings. For example, 'a'