This is where, the AnQi CMS template built-in,splitFilter becomes a very practical tool.It can help us easily split a longer string into an array or list containing multiple segments, based on the delimiter you specify, thus laying the groundwork for subsequent statistics, traversal, or further processing.

splitFilter: Turn string into zero

Imagine that you have a string of keywords connected by commas, like “AnQi CMS, content management, Go language”. If you have notsplitFilter, it may be difficult for you to handle each keyword separately in the template.splitThe filter is like a pair of scissors, which can precisely cut the string of keywords according to the "cutting point

Its basic usage is very intuitive: you just need to pass the string to be processed through the pipe|Pass tosplitfilter, and specify your delimiter after the filter using 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 exactly. If your keywords are separated by a "comma followed by a space,'auto' rather than the standalone one,If the string does not contain the specified separator,splitthe filter will treat the entire string as a single element and return an array containing this single element.

Combineforloop andlengthFilter for statistics and traversal

Once a string issplitinto an array, we can use the powerfulforloop andlengthfilter to flexibly display and count the data.

Traverse and display the keyword list

Assuming your article detail page has aarchive.KeywordsField, its value is "SEO optimization, website promotion, content marketing". You can display these keywords as independent tags at the bottom of the page.

{% 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 %}

So, each keyword will be extracted and displayed separately in a box, which is both aesthetically pleasing and easy for users to click or recognize..keyword-tagsuch style<span>Labels, both beautiful and convenient for users to click or identify.

Count the number of keywords

If you want to know how many keywords an article has,.splitfilter combined withlengthThe filter can easily achieve this.lengthThe filter is used to get the length or quantity of strings, arrays, or objects, etc.

{% 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 directly display the total number of keywords in the article.

In-depth mining and processing

splitThe purpose is far more than this. If your custom field stores multivalued data that requires specific processing, such assplitSplit them into an array, then use it withifConditional judgments, or nestedforLoops to implement more complex display logic.

For example, a custom field of a product modelproduct_options可能存储了“material: cotton and linen; color: red, blue, green; size: M, L”。这里需要两次splitEnglish: Once split the options by semicolon, and then split each option's value 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 do it by multiple timessplitandforLoop, 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 the Anqi CMS template. It can organize seemingly chaotic string data into an orderly array,配合forPerforming loop traversal display, as well aslengthThe filter for quantity statistics greatly enhances the dynamic processing and display ability of the template. Proficient use ofsplitEnglish will make your content management more efficient, and website content display more精彩。


Common Questions and Answers (FAQ)

1.splitfilters andmake_listWhat are the differences between filters?

splitThe filter is used to split strings based on the "delimiter" you specify. For example,"A,B,C"|split:","You will get["A", "B", "C"].make_listThe filter splits each character of a string into an element of an array, whether it's a letter or a Chinese character. For example,"你好"|make_listYou will get["你", "好"]。In most cases, if you need to separate data by specific symbols (such as commas, semicolons), you should usesplit; if you need to handle individual characters,make_listit is more convenient.

2.splitWhat are the precautions when filtering strings containing spaces?

If your string elements are separated by a delimiter and also contain spaces, such as “apple, banana, orange”, then insplitthere are two common processing methods:

  • Exact match of the delimiter:If you write"苹果, 香蕉, 橘子"|split:", "(