In the daily content operation of Anqi CMS, we often encounter scenarios where we need to handle string data, such as keyword lists of articles, content tags, or some multi-value custom fields stored in a specific format.These data often exist in the form of a single string, but when displayed on the front-end, we hope they can be presented in a more flexible and structured manner, such as independent label blocks, clickable links, or need to count the number of elements within them.
At this time, the Anqi CMS template has built-insplitThe filter has become a very practical tool. It can help us easily split a long string into an array or list of multiple segments according to the specified delimiter, thereby laying the foundation for subsequent statistics, traversal, or further processing.
splitFilter: Turns string into zero
Imagine you have a series of keywords connected by commas, like "AnQi CMS, content management, Go language". If notsplitThe filter, you may find it difficult to handle each keyword individually in the template. AndsplitThe filter acts like a pair of scissors, able to precisely cut the string of keywords according to the "cut point" (delimiter), into a list consisting of three independent elements such as "AnQi CMS", "Content Management", and "Go Language".
Its basic usage is very intuitive: you just need to pass the string through a pipe|pass tosplitfilter, and specify your delimiter after the filter with a colon. For example:
{% set keyword_string = "安企CMS,内容管理,Go语言" %}
{% set keyword_list = keyword_string|split:"," %}
{# 此时,keyword_list 会是一个包含 ["安企CMS", "内容管理", "Go语言"] 的数组 #}
It is worth noting that the delimiter you choose must match the delimiter in the actual string. If your keywords are separated by 'comma space', such as 'AnQi CMS, Content Management, Go language', then the delimiter should also be',This is not a single quotation mark,If the string does not contain the specified separatorsplitThe filter will treat the entire string as a single element and return an array containing only this one element.
Combineforloop andlengthThe filter performs statistics and traversal
Once a string has beensplitInto an array, we can use the powerful Anqi CMS template toforloop andlengthThe filter to display and statistically analyze the data flexibly.
Traverse and display the keyword list
Assume your article detail page has onearchive.KeywordsField, its value is "SEO optimization, website promotion, content marketing". You want to display these keywords in the form of independent tags at the bottom of the page. You can do this:
{% set article_keywords = archive.Keywords %} {# 假设值为 "SEO优化,网站推广,内容营销" #}
{% if article_keywords %}
<div class="keywords-list">
<strong>文章关键词:</strong>
{% set keyword_array = article_keywords|split:"," %}
{% for keyword in keyword_array %}
{# 注意:这里我们使用 |trim 过滤器来移除每个关键词可能存在的首尾空格,确保显示整洁 #}
<span class="keyword-tag">{{ keyword|trim }}</span>
{% endfor %}
</div>
{% endif %}
This, each keyword will be extracted and displayed separately in a label with.keyword-tagstyled one.<span>which is both beautiful and convenient for users to click or recognize.
Count the number of keywords
If you want to know how many keywords an article has,splitFilter combinationlengththe filter can easily achieve it.lengthThe filter is used to get the length or quantity of strings, arrays, or objects.
{% set article_keywords = archive.Keywords %} {# 假设值为 "SEO优化,网站推广,内容营销" #}
{% if article_keywords %}
{% set keyword_array = article_keywords|split:"," %}
<p>这篇文章包含了 <strong>{{ keyword_array|length }}</strong> 个关键词。</p>
{% endif %}
Through these two steps, you can intuitively display the total number of keywords in the article.
In-depth mining and processing
splitIts use is not limited to this. If your custom fields store multi-value data that needs special processing, such as "Color: red, green, blue" or "Size: S, M, L, XL", you can use them first.splitSplit them into an array and then combineifWith conditional judgment or nestedforLoop to implement more complex display logic.
For example, a custom field of a product modelproduct_optionsPossibly contains "Material: Cotton and Linen; Color: Red, Blue, Green; Size: M, L". This is needed twicesplit: Split options once by semicolon, and then split the values of each option by comma.
{% set product_options_string = product.CustomOptions %} {# 假设值为 "材质:棉麻;颜色:红,蓝,绿;尺寸:M,L" #}
{% if product_options_string %}
<ul class="product-options">
{% set options_array = product_options_string|split:";" %}
{% for option_pair in options_array %}
{% set pair_parts = option_pair|split:":" %}
{% if pair_parts|length == 2 %}
<li>
<strong>{{ pair_parts[0]|trim }}:</strong>
{% set values_string = pair_parts[1]|trim %}
{% set values_array = values_string|split:"," %}
{% for value in values_array %}
<span class="option-value">{{ value|trim }}</span>
{% endfor %}
</li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
This example shows how to use it multiple times.splitandforLoop, parse complex formatted string data and present it in a structured way on the front end.
Summary
splitThe filter is a powerful and flexible data processing tool in Anqi CMS template. It can organize seemingly chaotic string data into an orderly array, cooperate withforLoop for traversal display, as welllengthThe filter is used for quantity statistics, greatly enhancing the dynamic processing and display of the template. Proficient in usingsplitWill make your content management more efficient, and website content more dazzling.
Frequently Asked Questions (FAQ)
1.splitFilters andmake_listWhat are the differences between filters?
splitThe filter is based on the "delimiter" you specify to split strings. For example,"A,B,C"|split:","You will get["A", "B", "C"]Howevermake_listThe filter splits each character of a string into an element of an array, considering each letter and each Chinese character as one. For example,"你好"|make_listYou will get["你", "好"]In most cases, if you need to separate data by specific symbols (such as commas or semicolons), you should usesplit; if you need to handle individual charactersmake_listit is more convenient.
2.splitWhat are the precautions when the filter is processing a string containing spaces?
If your string contains elements separated by delimiters and spaces, such as "Apple, Banana, Orange", then insplitthere are two common handling methods:
- Exact matching of delimiters:If you write
"苹果, 香蕉, 橘子"|split:", "(