In the template development of Anqi CMS, flexible handling of strings is an indispensable part of content presentation.splitThe filter is a powerful tool that can split strings of a specific format into an array according to a specified delimiter, which is particularly useful in scenarios such as article tags, multi-value fields, etc. However, when usingsplitHow to effectively debug and confirm the cutting results after the filter, ensuring that the data is presented as expected, is a concern for many users.

This article will delve into debugging output in the AnQiCMS templatesplitSeveral recommended methods for filtering and cutting results, helping you understand the data stream more clearly and improve the efficiency and accuracy of template development.

KnowsplitThe working principle of the filter

splitThe filter is a built-in feature provided by the Anqi CMS template engine, its core function is to take a string (obj) and split it according to the delimiter you specify (delimiterConvert it into a string array (in Go language):[]stringA slice).

Its basic syntax is very intuitive:{{ 字符串变量 | split:"分隔符" }}.

For example, if you have a string"SEO,内容营销,网站运营"And if you want to split it into three separate tags, you can use it like this:split:{% set tags = "SEO,内容营销,网站运营"|split:"," %}【en】At this point,tagsthe variable will store an array containing"SEO","内容营销","网站运营"these three elements.

It should be noted that if the specified delimiter does not exist in the string,splitThe filter will return an array containing only the original string itself. If the delimiter is an empty string""If so, it will split the string into an array of each UTF-8 character. In addition, if you need to split the string into an array by individual characters (for example, handling each Chinese character in a Chinese string),make_listFilter is also a good choice.

Recommended debugging output method

In actual development, knowing onlysplitIt's not enough to use it yet, the key is how to view what it actually cuts out, especially when the result does not meet expectations. Here are several methods of efficient debugging and output.splitmethods for cutting results:

1. Directly output the array variable

The most direct and simplest verification method is to output the array variable that has beensplitprocessed directly to the webpage.

{% set keywordString = "AnQiCMS,Go语言开发,企业建站" %}
{% set tagsArray = keywordString|split:"," %}
<p>切割结果(直接输出):{{ tagsArray }}</p>

Run this code, you might see something similar[AnQiCMS Go语言开发 企业建站]Such an output.This method quickly confirms whether the variable has been successfully converted to an array and displays its elements.It is very suitable for quick verification, but it may not be enough for complex array structures or when more detailed type information is needed.

2. UtilizedumpFilter to view detailed structure

When you want to understand in depthsplitThe array returned by the filterTypeandDetailed structurewhendumpThe filter is your weapon.dumpThe filter can print the underlying structure, type, and value of any variable in a developer-friendly format.

{% set keywordString = "AnQiCMS,Go语言开发,企业建站" %}
{% set tagsArray = keywordString|split:"," %}
<p>切割结果(dump过滤器):{{ tagsArray|dump }}</p>

UsedumpAfter the filter, you will see something like[]string{"AnQiCMS", "Go语言开发", "企业建站"}The output clearly indicates.tagsArrayis a string slice.[]string), and lists each string element inside, which is one of the ways to understand the accurate type and content of the data.

3. CombinejoinFilter outputs in a custom format

splitThe "inverse operation" isjoinFilter that can concatenate array elements into a string. In debuggingsplitWhen the result is obtained, we can cleverly utilizejointo use the split arraydifferent from the original delimiterto reconnect the characters, for a more intuitive and readable verification method.

For example, if the original string is separated by commas, you can use a pipe|tojoin:

{% set keywordString = "AnQiCMS,Go语言开发,企业建站" %}
{% set tagsArray = keywordString|split:"," %}
<p>切割结果(join过滤器):{{ tagsArray|join:" | " }}</p>

output may beAnQiCMS | Go语言开发 | 企业建站.This method makes the array content clear at a glance, especially when the original string delimiter may cause ambiguity (for example, the delimiter itself may also appear in the data). Using a unique connector can effectively avoid confusion.

4. Byforlooping through one by one

In actual templates, we usually need to iteratesplitover the array after, processing each element (such as generating links, list items). Therefore, using directlyforLooping to output each element one by one is also a very practical debugging method. It not only verifies whether the cutting is correct, but also confirms the performance of each element in the loop.

{% set keywordString = "AnQiCMS,Go语言开发,企业建站" %}
{% set tagsArray = keywordString|split:"," %}
<div class="debug-output">
    <p>切割结果(for循环逐一输出):</p>
    <ul>
        {% for tag in tagsArray %}
            <li>第 {{ forloop.Counter }} 个标签: "{{ tag|trim }}"</li>
        {% else %}
            <li>数组为空或无内容。</li>
        {% endfor %}
    </ul>
</div>

Here we also added extra|trima filter to remove any leading or trailing whitespace characters, and to utilizeforloop.Counterto display the element's index, which is very convenient during debugging.{% else %}Blocks are used to handle the case where the array is empty. This method simulates the data processing flow in actual applications and can most truly reflect the problem.

Practice Case: Handling article tag strings

Assume you have an article in an A security CMS, its keywords (archive.Keywords) are separated by commas, and you want to display them as clickable tags on the page.

{# 假设 archive.Keywords 的值为 "安企CMS,GoLang,模板开发,SEO优化" #}

{% set keywordString = archive.Keywords %}

{% if keywordString %}
    {% set tagsArray = keywordString|split:"," %} {# 使用 split 过滤器将字符串切割成数组 #}

    <div class="article-tags">
        <strong>文章标签:</strong>
        {% for tag in tagsArray %}
            {# 遍历数组中的每个标签 #}
            {# 使用 trim 过滤器去除标签可能存在的首尾空格,然后 urlencode 确保链接安全 #}
            <a href="/tag/{{ tag|trim|urlencode }}" class="tag-item">
                {{ tag|trim }}
            </a>
        {% else %}
            {# 如果 tagsArray 为空,则显示此内容 #}
            <span>暂无标签</span>
        {% endfor %}
    </div>

    {# 调试输出,确认切割结果 #}
    <div style="background-color: #f0f0f0; padding: 10px; margin-top: 20px; border-left: 3px solid #007bff;">
        <p><strong>调试信息:</strong></p>
        <p>原始关键词字符串:`{{ keywordString }}`</p>
        <p>split后数组内容(直接输出):`{{ tagsArray }}`</p>
        <p>split后数组结构(dump过滤器):`{{ tagsArray|dump }}`</p>
        <p>split后数组重连接(join过滤器):`{{ tagsArray|join:" -- " }}`</p>
    </div>

{% else %}
    <div class="article-tags">
        <span>暂无标签</span>
    </div>
{% endif %}

In this example, we not only demonstrate how to utilizesplitThe filter processes data and renders it on the front end, and also includes multiple debugging output methods.When the tags on the page are displayed incorrectly, you can refer to the debugging information to quickly locate whether the original string has a problem, the delimiter is not chosen properly, or the elements after slicing are processed incorrectly.

Summary

splitThe filter is a powerful tool in the Anqi CMS template for handling string data.Grasp the principle of operation, and combine it with various debugging output methods, which can help you develop templates more efficiently and accurately.dumpinformation, but also its intuitive naturejoinrestructuring