In Anqi CMS template development, we often need to process and transform data. Among them,splitA filter is a very practical tool that can help us split a long string into multiple parts according to a specified delimiter and present them as an array (list). However, many developers who are new to the field may have a question: when we usesplitWhen using a filter, will the original string variable be affected, and will its value be changed?
The answer is:won't.
UnderstandingsplitThe principle of the filter.
splitThe main function of the filter is to split a string into multiple substrings based on the delimiter you provide, and return these substrings as a new array (or list). For example, if you have a string storing multiple tags, such as “technology, operation, content, SEO”, and you want to handle each tag independently,splitThe filter can efficiently complete this task.
The basic usage is to apply a filter to a variable and specify a delimiter, as shown below:
{{ "关键词一,关键词二,关键词三"|split:"," }}
This code will return an array containing the elements "keyword one", "keyword two", and "keyword three".
Why is the original string not modified?
The design concept of AnQi CMS template engine is similar to many modern template engines (such as Django, Jinja2, etc.), one of its core principles is thatMaintain the read-only nature of the template content to avoid unintended modifications to the original dataTherefore, whensplitThe filter is applied to a string variable, it will not modify the string in place. Instead, it willCreate a new array (list) to store the cut resultsWhile the original string variable remains unchanged.
It's like you have text on a piece of paper, and you want to cut it into several segments.splitThe filter is like you took the piece of paper.copyAfter cutting, you got several pieces of text in the copy, while the one in your handThe original paper (string) remains intactThis 'non-destructive' operation method is the standard practice of the template engine, aiming to ensure the safety and predictability of the data processing process.
Actual case demonstration
To illustrate this more intuitively, let's look at a simple template code example:
{% set originalString = "安企CMS,内容管理,Go语言" %}
<p>原始字符串变量:{{ originalString }}</p>
{% set newArray = originalString|split:"," %}
<p>使用`split`过滤器后的新数组(第一个元素):{{ newArray[0] }}</p>
<p>再次输出原始字符串变量,它的值是:{{ originalString }}</p>
The output of this code will be:
原始字符串变量:安企CMS,内容管理,Go语言
使用`split`过滤器后的新数组(第一个元素):安企CMS
再次输出原始字符串变量,它的值是:安企CMS,内容管理,Go语言
From the output, we can clearly see that even though we usedsplitThe filter, and its result was assigned tonewArraythe variable,originalStringThe value of the variable remains unchanged. This provessplitThe filter isNon-destructiveoperation, it does not modify the original data.
**Practice and Application Scenarios
This non-destructive design brings obvious benefits: it makes template code safer and more predictable.You can safely process the string without worrying that the original data will be accidentally tampered with.Therefore, when usingsplitWhen or any filter is applied, **practical isAlways assign the result of the filter to a new variablefor subsequent operations and display.
Most of the filters in AnQi CMS follow this principle, for examplereplace(Replace the substring in the string),trimRemove whitespace from a string or specified characters, they will all return a new value without affecting the original variable.This greatly simplifies the logic of the template, reduces potential errors, and makes your website content operation more efficient and stable.
Frequently Asked Questions (FAQ)
splitWhat type of data does the filter return?splitThe filter always returns an array of strings (in the template context, usually called a list).Even if the original string does not contain the specified delimiter, it will return an array containing the original string as the only element.How to access
splitWhat is the individual element returned by the filter?splitThe filter returns an array, you can access its internal elements as you would with a regular array, using indices (starting from 0). For example, ifnewArrayIssplitthe result is,{{ newArray[0] }}I will get the first element of the array.If I want to modify a part of the string but do not want to use
splitAre there any other filters?If you just want to replace a substring in a string, you can usereplaceFilter. For example,{{ "Hello World"|replace:"World,AnQiCMS" }}will replace “World” with “AnQiCMS”. Similarly,replaceThe filter will also return a new string without affecting the original variable.