In AnQiCMS template development, we often need to process text content, such as splitting a string of keywords, tags, or descriptions into individual entries for list display or further operations.fieldsFilter has become a very practical tool. It can efficiently complete the splitting of strings, but many users may be curious about,fieldsAfter the filter process, what kind of data type will these split strings present?

Deeply UnderstandfieldsThe operation principle of the filter is crucial for us to make more effective use of the AnQiCMS template functions.

fieldsFilter: the secret weapon for string splitting.

As the name suggests,fieldsThe filter is mainly used to split a piece of text content into separate strings according to spaces (including common spaces, tab characters, newline characters, and other whitespace characters).It is like an intelligent text analyzer that can automatically identify "words" or "segments" in text and extract them.

For example, if you have a string containing multiple keywords, such as"SEO 优化 内容营销 网站运营 AnQiCMS", it may not be aesthetically pleasing or practical to display directly in the template. ByfieldsThe filter process allows each keyword to be treated independently, making it convenient for us to perform style rendering or loop output.

Unveiling the data types after splitting:[]string{}

Then,fields过滤器处理后,这些被拆分出来的字符串会以什么形式存在呢?答案是:一个 Go 语言的 English.[]string{}类型数组。

这意味着,当你使用 English.fieldsFiltering yields a list of strings, each representing a single 'word' or 'fragment' from the original text.This data type corresponds exactly to the string slice (slice) in the Go language, it allows you to traverse, index access, count, and other operations on the split strings as if you were operating on a normal array in the template.

It is crucial to understand this, as it determines how you further operate on these data in the template, such as usingforLoop through and display each entry, or combine with other filters for more complex processing.

Practical Application: How to use in templates[]string{}

Since we know thatfieldsThe filter outputs are[]string{}Array, so in AnQiCMS templates, we can use it flexibly like this:

1. Basic Traversal and Display

The most common usage is to iterate over the split array and display each item one by one. This is very useful for scenarios such as displaying article tags, product features, etc.

{% set keywordsString = "SEO优化 内容营销 流量增长 AnQiCMS" %}
{% set keywordList = keywordsString|fields %}

<div class="keyword-tags">
    {% for keyword in keywordList %}
        <span class="tag-item">{{ keyword }}</span>
    {% empty %}
        <span class="no-tags">暂无关键词</span>
    {% endfor %}
</div>

In this example,keywordListIt is just a[]string{}An array of types,forLoops can easily access each string element in it.

2. Retrieve a specific element or segment

Since it is an array, you can easily access the first or last element, even cut out a specific range of segments, which is especially convenient when it comes to highlighting certain information.

{% set features = "多站点管理 灵活内容模型 强大SEO工具 高性能架构 安全可靠"|fields %}

<p>第一个亮点:<strong>{{ features|first }}</strong></p>
<p>最后一个优势:<strong>{{ features|last }}</strong></p>
<p>核心特性 (2-4项):{{ features|slice:"1:4"|join(", ") }}</p> {# 注意:slice 索引从 0 开始 #}

3. Combine other filters for data processing

[]string{}The array features also allow it to seamlessly collaborate with other filters provided by the AnQiCMS template engine, achieving more powerful data processing capabilities.

{% set slogan = "AnQiCMS 让 网站 运营 更 高效" %}
{% set sloganWords = slogan|fields %}

<p>口号包含 <strong>{{ sloganWords|length }}</strong> 个词。</p>
<p>重新用破折号连接口号:{{ sloganWords|join(" - ") }}</p>
<p>口号中是否包含“高效”:{% if sloganWords|contain:"高效" %}是{% else %}否{% endif %}</p>

fields/splitWithmake_listdifferences and similarities

AnQiCMS template engine also provides other string splitting filters, such assplitandmake_listThey each have different applicable scenarios, but the output type is the same asfieldsThere are similarities and also key differences:

  • fieldsFilter:Specifically designed forWhitespace charactersSplitting strings, the output type is[]string{}. It is most suitable for splitting a natural language text into individual words or phrases.
  • splitFilter:If you need to split a string based ona custom delimiter(instead of just whitespace characters), thensplitthe filter would be a better choice. For example, a string separated by commas"Apple,Banana,Orange"Split. Its output type is also[]string{}.
  • make_listFilter:If your goal is to completely split a string intoindividual characters(even Chinese characters will be split),make_listThe filter will convert the string to[]runeType (represented as an array of single-character strings in the template). This is very useful when each character needs to be processed independently.

In short,fieldsSimple and direct, focusing on splitting by whitespace, a powerful tool for handling 'words'.

Summary

In the AnQiCMS templatefieldsThe filter is a simple yet powerful tool that can split string content by whitespace into a[]string{}The array of types.Understand and master this output type can help us handle and display text data more flexibly and efficiently in templates. Whether it is for list rendering, content extraction, or combination with other filters, it can make our website content more dynamic and expressive.


Common Questions (FAQ)

  1. fieldsFilter can handle Chinese? How will it split Chinese sentences?Yes,fieldsFilter can handle Chinese. It will also split strings based on whitespace characters (such as spaces). For example,"你好 世界"Would be split into["你好", "世界"]. If there are no blank characters between Chinese characters, like"你好世界", then the entire string“你好世界”would be considered as a single element and would not be split into["你", "好", "世", "界"].

  2. How can I judgefieldsThe split array is empty?You can useifSentence combinationlengthFilter to determine if the length of the array is 0, orforuse in a loop.emptyKeywords to handle the case where the array is empty.

    {% set tags = ""|fields %}
    {% if tags|length > 0 %}
        <p>数组不为空,共有 {{ tags|length }} 个元素。</p>
    {% else %}
        <p>数组为空。</p>
    {% endif %}
    
    
    {# 或者在 for 循环中 #}
    {% for item in tags %}
        {# ... 处理元素 ... #}
    {% empty %}
        <p>没有元素可供显示。</p>
    {% endfor %}
    
  3. If my string delimiter is not a space, but a comma or semicolon,fieldsCan the filter still be used?If your string delimiter is not a space,fieldsthe filter is not applicable. In this case, you should usesplitFilter, which allows you to specify any custom delimiter to split strings. For example,"Apple,Banana,Orange"|split:","will split by comma as well and return[]string{}类型数组。