In the template development of Anqi CMS, we often need to process and transform data.splitA filter is a very practical tool that helps 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:splitThe original string variable will be affected when filtering, will its value be changed?
The answer is:will not.
UnderstandingsplitThe working principle of the filter
splitThe primary function of the filter is to split a string into multiple substrings based on the delimiter you provide, and then return these substrings as a new array (or list). For example, if you have a string storing multiple tags, such assplitThe 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 philosophy of the Anqi CMS template engine is similar to many modern template engines (such as Django, Jinja2, etc.), one of its core principles beingKeep the template content read-only to avoid unexpected modifications to the original data.splitThe filter applies to a string variable without modifying the original string. Instead, itcreates a new array (list) to store the results of the splitAnd the original string variable retains its original value.
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 a piece of paper andPhotocopyGo to cut, after cutting you get several lines of text in photocopies, and the one in your handThe original paper (string) is still in perfect conditionThis 'non-destructive' operation method is the standard practice of the template engine, aimed at ensuring the safety and predictability of the data processing process.
Actual case demonstration
To make this more intuitive, 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 result of this code will be:
原始字符串变量:安企CMS,内容管理,Go语言
使用`split`过滤器后的新数组(第一个元素):安企CMS
再次输出原始字符串变量,它的值是:安企CMS,内容管理,Go语言
We can clearly see from the output that even though we usedsplitFilter, and its result is assigned tonewArrayvariable,originalStringThe value of the variable remains unchanged. This provessplitThe filter isnon-destructiveoperation, which 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 strings without worrying about the original data being accidentally tampered with.splitOr any other filter, **the practice 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 example,replace(Replace the substring in the string),trimThey will return a new value after processing, removing whitespace characters from the beginning and end of a string or specified characters, 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.
Common Questions (FAQ)
splitWhat type of data does the filter return?splitThe filter always returns an array of strings (usually called a list in the template context).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 single element of the array returned by the filter?splitThe filter returns an array, you can access its internal elements like a regular array, using indices (starting from 0). For example,newArrayYessplitthe result after,{{ newArray[0] }}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 usereplacea filter. For example,{{ "Hello World"|replace:"World,AnQiCMS" }}”World” will be replaced by ”AnQiCMS”. Similarly,replaceThe filter will also return a new string without affecting the original variable.