In Anqi CMS template development, flexible string handling is an indispensable part of content presentation.splitFilter is indeed such a powerful tool, it 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 and multi-value fields. However, when usingsplitAfter the filter, how can we effectively debug and confirm the cutting results, ensuring that the data is presented as expected, which is a concern for many users.

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

Get to knowsplitThe principle and working of the filter.

splitThe filter is an integrated feature provided by the Anqi CMS template engine, its core function is to transform a string (obj) according to the delimiter you specify (delimiterConvert it to a string array (in Go language)[]stringslice).

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

For example, if you have a string"SEO,内容营销,网站运营"And if you want to split it into three independent tags, you can usesplit:{% set tags = "SEO,内容营销,网站运营"|split:"," %}. At this point,tagsThe variable will store a containing"SEO","内容营销","网站运营"This array of 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""It will split the string into an array of characters according to each UTF-8 character. In addition, if you need to split the string into an array of individual characters (such as handling Chinese character strings with each Chinese character),make_listThe filter is also a good choice.

Recommended debugging output method

In actual development, just knowingsplitHow to use it is not enough, the key is how to view what it actually cut out, especially when the result does not meet expectations. Here are several efficient debugging and output methods.splitMethods for cutting results:

1. Directly output the array variable

The most direct and simplest verification method is to pass throughsplitthe processed array variable directly to the page.

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

Run this code and you may see something similar[AnQiCMS Go语言开发 企业建站]This output. This method can quickly confirm whether the variable has been successfully converted to an array and display 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. UsedumpThe filter examines the detailed structure

When you want to understand moresplitThe array returned by the filter'sTypeanddetailed structurethen,dumpThe 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 indicatestagsArrayIt is a string slice ([]string), and lists each string element within it, which is one way to understand the accurate type and content of the data.

3. CombinejoinFilter output in a custom format

splitThe 'inverse operation' isjoinFilter, it can concatenate array elements into a string with a specified delimiter. In debuggingsplitWhen the result is obtained, we can cleverly utilizejointo concatenate the split array with adifferent from the original delimitercharacter to reconnect it in a more intuitive and readable manner for verification.

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

{% 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. PassforIterate output one by one in a loop

In the actual template, we usually need to iterate throughsplitAfter the array, process each element (such as generating links, list items). Therefore, use it directly.forLoop to output each element one by one is also a very practical debugging method. It can not only verify whether the segmentation is correct, but also confirm 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 and utilizesforloop.Counterto display the index of the element, which is very convenient during debugging.{% else %}This block is used to handle the case of an empty array. This method simulates the actual processing process of data, which can truly reflect the problem.

Practical Case: Handling article tags string

Assuming you have an article on an Aiqi CMS, the keywordsarchive.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 it also includes various debugging output methods.When the label on the page is displayed incorrectly, you can refer to the debugging information to quickly locate whether the original string is problematic, the separator is not chosen correctly, or there is an error in the processing of the elements after cutting.

Summary

splitThe filter is a powerful tool for handling string data in the AnQi CMS template.Master the principles and combine with various debugging output methods, which can help you develop templates more efficiently and accurately.Whether it is a simple direct output or an in-depth onedumpinformation, but also in a直观 wayjoinrestructuring