During the development of AnQiCMS templates, we often encounter situations where we need to flexibly process data. One common requirement is to split a long string containing multiple pieces of information into an array of independent strings based on a specific delimiter (such as a space), so that it can be displayed in a loop or further operated.The AnQiCMS template engine provides a concise and efficient filter (Filter) feature that can easily achieve this goal.
UnderstandingsplitFilter
AnQiCMS template system uses a template engine syntax similar to Django, which includes a variety of practical filters to help us process data. For the need of string splitting,splitThe filter is our helpful assistant.
splitThe core function of the filter is to split a string based on a specified delimiter into a string slice (in the context of templates, we can understand it as a string array). Its basic syntax is very intuitive:{{ 变量 | split:"分隔符" }}.
When you usesplitWhen filtering, you need to provide two key pieces of information:
- The string variable to be processed: This is the long string you want to split.
- SeparatorAn indicator character or string used to specify where a string should be split. For example, if your string is '标签1 标签2 标签3', then the delimiter is a space (
" ").
After the filter is applied, an array containing the split substrings will be returned. If the original string does not contain the specified delimiter,splitThe filter will return a single-element array containing only the original string itself. In addition, if an empty string is used as the delimiter"")splitThe filter will split each character of the original string into an element of the array.
Actual operation: Split a long string into an array by spaces.
Assuming you have a document fieldarchive.KeywordsIts content is a string of keywords separated by spaces, for example"AnQiCMS 模板开发 字符串处理 Go语言"Now, we want to display these keywords as independent tags one by one on the page.
We can achieve this through the following template code:
{% set keywordString = "AnQiCMS 模板开发 字符串处理 Go语言" %}
{# 使用 split 过滤器将字符串按空格拆分成数组 #}
{% set keywordArray = keywordString|split:" " %}
<div>
<h4>关键词列表:</h4>
<ul>
{# 打印数组的原始结构,方便调试理解 #}
<p>数组原始结构:{{ keywordArray|stringformat:"%#v"|safe }}</p>
{# 遍历拆分后的数组,逐一展示关键词 #}
{% for keyword in keywordArray %}
<li>{{ keyword }}</li>
{% endfor %}
</ul>
</div>
In this code block:
- We first use
{% set keywordString = "..." %}A long string variable was simulatedkeywordString. In actual use, this is usually from yourarchive.Keywordsor other data fields. - The next key step is
{% set keywordArray = keywordString|split:" " %}Here,keywordStringVariables through the pipeline symbol|Pass tosplitFilter, and specify the delimiter as" "(a space).splitAfter the filter is executed, it will return an array of strings and assign it tokeywordArraya variable. - To help you better understand
keywordArrayThe internal structure, we used{{ keywordArray|stringformat:"%#v"|safe }}.stringformat:"%#v"The filter will print the variable in detail with Go language structure,safeThe filter ensures that the printed string (if it contains special characters) is not automatically escaped. - Finally, we use
{% for keyword in keywordArray %}to iteratekeywordArrayEach element (that is, each keyword) should be enclosed within<li>tags for display.
After executing this template code, you will see an output similar to the following on the page:
关键词列表:
数组原始结构:[]string{"AnQiCMS", "模板开发", "字符串处理", "Go语言"}
<ul>
<li>AnQiCMS</li>
<li>模板开发</li>
<li>字符串处理</li>
<li>Go语言</li>
</ul>
In this way, you can flexibly split a long string into independent array elements and dynamically display them in the AnQiCMS template.
Withmake_listComparison of filters
In AnQiCMS, there is also anothermake_listfilter that can also convert strings to arrays. However,make_listworks differently fromsplitfilter:make_listis sorted byCharacterSplit the string instead of using the specified delimiter.
For example:
{% set myString = "你好 世界" %}
{% set charArray = myString|make_list %}
{% set spaceSplitArray = myString|split:" " %}
<p>按字符拆分:{{ charArray|join:"," }}</p>
<p>按空格拆分:{{ spaceSplitArray|join:"," }}</p>
The output will be:
按字符拆分:你,好, ,世,界
按空格拆分:你好,世界
It's not hard to find that,make_listSpaces are also considered as characters for splitting,split:" "Therefore, it precisely retains 'Hello' and 'World' as words separated by spaces. Therefore, when you need to split words by specific delimiters (such as spaces, commas, etc.),splitthe filter is a more appropriate choice.
PasssplitFilter, you can flexibly handle various string data and bring more dynamic content display to your AnQiCMS website.
Common Questions (FAQ)
How to reassemble the split string array into a long string?You can use
joinThe filter reassembles the array elements into a string. For example,keywordArrayis a split array, if you want to join it with a comma and a space, you can do it like this:{% set keywordArray = "AnQiCMS 模板开发 字符串处理"|split:" " %} <p>重新拼接后的字符串:{{ keywordArray|join:", " }}</p> {# 输出: 重新拼接后的字符串:AnQiCMS, 模板开发, 字符串处理 #}How can I split a long string if it is not separated by spaces but by other symbols (such as commas)?
splitThe filter is very flexible, you just need to replace the delimiter parameter with the actual symbol you are using. For example, if the string is"关键词A,关键词B,关键词C", then the delimiter should be a comma (,):{% set commaSeparatedString = "关键词A,关键词B,关键词C" %} {% set newArray = commaSeparatedString|split:"," %} <p>按逗号拆分:{{ newArray|join:" / " }}</p> {# 输出: 按逗号拆分:关键词A / 关键词B / 关键词C #}Splitting, I only want to get a specific element from the array, not traverse all, is that okay?Of course. AnQiCMS template supports array index access, the index starts from
0Start. For example, to getkeywordArraythe first element (index of0), you can do this:{% set keywordArray = "AnQiCMS 模板开发 字符串处理"|split:" " %} <p>第一个关键词:{{ keywordArray[0] }}</p> {# 输出: 第一个关键词:AnQiCMS #} {% if keywordArray|length > 1 %} <p>第二个关键词:{{ keywordArray[1] }}</p> {% endif %}It is best to check the length of the array first when accessing a specific index to avoid index out of bounds errors.