In the powerful functions of AnQi CMS, the flexibility of content display has always been our focus.Sometimes, you may need to input a long string in a field on the backend, such as multiple keywords of an article, multiple parameters of a product specification, or a list of skills of a team member.These information are usually connected by specific separators (such as commas, semicolons, or vertical bars).However, when you want to display this information separately on the front-end page, even apply different styles to each part, the problem arises: how can you conveniently split this long string into individual items?
At this time, the Anqi CMS template engine providessplitFilter becomes your powerful assistant.It can cleverly split a string into an iterable array (or list) according to the delimiter you specify, thus greatly enhancing the precision and flexibility of content display.
Core Feature: The Secret Weapon for Splitting Strings into Arrays
splitThe basic function of the filter is very intuitive: it takes a string and a delimiter as parameters, and then, based on this delimiter, it 'slices' the original string to generate an array containing all the separate parts.
The usage is very concise:
{{ 你的字符串变量 | split:"分隔符" }}
For example, if you have a string{{ article.Keywords }}with the value"Go语言, 安企CMS, 网站运营"You want to split it into:Go语言/安企CMSand网站运营These three independent words can be used like this:
{% set keywordList = article.Keywords | split:", " %}
After execution,keywordListit will become a container that includes["Go语言", "安企CMS", "网站运营"]The array. Next, you can handle it like any other array, performing operations and displaying each element.
Application scenarios: make the content more flexible
splitThe filter is widely used in content operation and template design, let's take a look at some common application scenarios.
Scenario one: Dynamic display of article keywords or tags
When editing articles in the Anqi CMS backend, you may be accustomed to entering multiple keywords in the 'Document Keywords' field, separated by commas. On the frontend page, if you want to display these keywords as separate, clickable tags, splitThe filter can be used.
Assuming your article detail page has onearchiveobject, whichKeywordsthe field content is"安企CMS, 内容管理, SEO优化". You can handle it in the template like this:
<div class="article-tags">
{% set tags = archive.Keywords | split:", " %}
{% for tag in tags %}
<a href="/tags/{{ tag }}" class="tag-item">{{ tag }}</a>
{% endfor %}
</div>
Through these lines of code, the originally long string of keywords has been split into independent tags, each with its own link and style, greatly enhancing the user experience.
Scene two: Handling list-style custom field data
Imagine you have defined a product model in the backend, with a custom field namedfeaturesTo store product features, the content may be"防水|防尘|抗摔". You may want to display these features in a list format on the product detail page.
<ul class="product-features">
{% set featureList = product.features | split:"|" %}
{% for feature in featureList %}
<li>{{ feature }}</li>
{% endfor %}
</ul>
This way, product features can be presented to users clearly and logically, rather than a long paragraph that is difficult to read.
In-depth Understanding: Special Cases and Sister Filters
When usingsplitWhen filtering, understanding some special cases and related sister filters can help you more flexibly deal with different content processing needs.
- The delimiter does not exist:If you specify
分隔符Does not exist in the original string.splitthe filter will not return an error but instead will return an array with a single element containing the original string itself. For example,"安企CMS" | split:","You will get["安企CMS"]. - The delimiter is an empty string:If you set the delimiter to an empty string
"",splitThe filter will be very intelligent in splitting each UTF-8 character of the string into an element of the array.This means that both English letters and Chinese characters will be recognized individually."你好" | split:""You will get["你", "好"].
For character-level splitting, Anqi CMS also provides a convenient sibling filter:make_listIts function is tosplitThe effect of using a space delimiter is similar, both are to split the string into an array of individual characters. When you know for sure that you want to split by individual characters,make_listit is a more intuitive choice.
{# 使用 split 过滤器并指定空分隔符 #}
{% set charArray1 = "安企CMS" | split:"" %}
{# 使用 make_list 过滤器 #}
{% set charArray2 = "安企CMS" | make_list %}
{# charArray1 和 charArray2 都会得到 ["安", "企", "C", "M", "S"] #}
Use Case: Hands-on Practice
Below are some specific code examples to help you better understand and applysplitFilter.
{# 示例1:基本拆分与遍历 #}
{% set rawString = "Apple,Banana,Orange,Grape" %}
{% set fruits = rawString | split:"," %}
<p>水果列表:</p>
<ul>
{% for fruit in fruits %}
<li>{{ fruit | trim }}</li> {# 使用 trim 过滤器去除可能存在的空格 #}
{% endfor %}
</ul>
{# 示例2:自定义字段内容的拆分与展示 #}
{# 假设 archive.Specs 为 "颜色:红色;尺寸:L;材质:棉" #}
{% set specsString = "颜色:红色;尺寸:L;材质:棉" %}
<p>产品规格:</p>
<ul>
{% set specPairs = specsString | split:";" %}
{% for pair in specPairs %}
{% set detail = pair | split:":" %}
<li><strong>{{ detail[0] }}:</strong> {{ detail[1] }}</li>
{% endfor %}
</ul>
{# 示例3:使用空分隔符进行字符拆分 #}
{% set chineseChars = "安企CMS" %}
<p>按字符拆分:</p>
{% for char in chineseChars | split:"" %}
<span>{{ char }}</span>
{% endfor %}
{# 示例4:使用 make_list 过滤器进行字符拆分(与示例3效果类似,更简洁) #}
<p>使用 make_list 拆分:</p>
{% for char in chineseChars | make_list %}
<span>{{ char }}</span>
{% endfor %}
{# 示例5:当分隔符不存在时 #}
{% set singleString = "Hello AnQiCMS" %}
{% set parts = singleString | split:"," %}
<p>无分隔符拆分结果(数组长度为1):{{ parts | stringformat:"%#v" }}</p>
Summary: Unlock New Content Management Potential
splitThe filter is a seemingly simple but extremely powerful tool in the Anqi CMS template engine.It allows you to easily deconstruct those string data stored in a specific format into an array that can be independently operated.splitFilters can help you manage and present your website content in a more flexible and efficient way.Make good use of this technique, and you will be able to better utilize the advantages of Anqi CMS in terms of content customization and extensibility, providing users with a higher quality and more user-friendly browsing experience.
Common Questions (FAQ)
1.splitfilters andjoinWhat are the differences between filters?
splitfilters andjoinFilters are a pair of complementary features.splitThe function of this is to 'split' a long string into an array (list) using a specified delimiter; andjoinThe function is the opposite, it connects all elements of an array (list) into a single string using a specified delimiter. In simple terms,splitmeans 'split',joinIs it 'He'?
2. UsesplitWhat type is the variable obtained after filtering? Can it be traversed directly?
UsesplitAfter filtering, the variable you obtain is an array (or in Go language,sliceType). In the template engine of AnQi CMS, you can directly use{% for ... in ... %}loop tags to iterate over each element in this array, as demonstrated in the example in the article.
3. If my string does not contain the specified delimiter,splitwhat will the filter return?
If your string does not contain the delimiter you specified,splitThe filter will not throw an error. It will return an array containing only the original string itself, with the length of the array being 1. For example,