In AnQiCMS template development, we often need to process text content, such as splitting a string of keywords, tags, or descriptions into independent entries for list display or further operations. At this point,fieldsThe filter has become a very practical tool. It can efficiently complete the splitting of strings, but many users may be curious about, afterfieldsAfter the filter, what data type will these split strings appear in?
Get to know in-depthfieldsThe mechanism of the filter is crucial for us to make effective use of the AnQiCMS template features.
fieldsFilter: the secret weapon of string splitting.
As the name implies,fieldsThe filter is mainly used to split a piece of text content into independent strings according to spaces (including ordinary spaces, tab characters, newline characters, and other whitespace characters).It is like an intelligent text analyzer that can automatically identify words or fragments in the 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 them directly in a template. ByfieldsAfter the filter is processed, each keyword can be treated independently, making it convenient for us to perform style rendering or loop output.
The types of split data revealed:[]string{}
Then,fieldsAfter the filter process, what form will these split strings exist in? The answer is: an array of Go language[]string{}type.
This means, when you usefieldsWhen filtering, you get a list of strings containing multiple strings, each of which is an independent 'word' or 'fragment' from the original text.This data type corresponds exactly to the Go language's string slice (slice), which allows you to traverse, index access, count, and perform various operations on the split strings in the template just like you would with a regular array.
Understanding this is crucial because it determines how you further operate on these data in the template, such as usingforLoop through and display each term one by one, or combine it with other filters for more complex processing.
Practical application: How to use it in a template[]string{}
Since we knowfieldsThe filter outputs is[]string{}Array, so in the AnQiCMS template, we can use it flexibly like this:
1. Basic traversal and display
The most common usage is to iterate over the split array, displaying each item one by one. This is very useful for displaying article tags, product features, and other scenarios.
{% 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 slice a specific range of segments, which is especially convenient when you need to highlight 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 with other filters for data processing
[]string{}The characteristics of arrays also enable them to seamlessly collaborate with other filters provided by the AnQiCMS template engine, realizing more powerful data processing functions.
{% 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 have different applicable scenarios, but the output type is the same asfieldsThere are similarities as well as key differences:
fieldsFilter:Designed for the keyWhitespaceSplitting strings is designed, the output type is[]string{}It is most suitable for splitting a natural language text into independent words or phrases.splitFilter:If you need to divide according toa custom delimiterSplitting strings using, rather than just whitespace characters, thensplitThe filter would be a better choice. For example, you will split strings using commas"Apple,Banana,Orange"Split. Its output type is also[]string{}.make_listFilter:If your goal is to split a string completely intoa single character(Even Chinese characters will be split apart),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 you need to process each character independently.
In short,fieldsSimple and direct, focusing on splitting by whitespace, it is a powerful tool for handling 'words.'
Summary
AnQiCMS template infieldsThe filter is a simple yet powerful tool that can split the content of a string into one[]string{}A type of array. Understanding and mastering 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 combining with other filters, it can make our website content more dynamic and expressive.
Frequently Asked Questions (FAQ)
fieldsCan the filter handle Chinese? How will it split Chinese sentences?Yes,fieldsThe filter can handle Chinese. It will also split strings based on whitespace characters (such as spaces). For example,"你好 世界"be split into["你好", "世界"]If there are no blank characters between Chinese characters, such as"你好世界"Then the entire string“你好世界”will be considered as a single element and will not be split into["你", "好", "世", "界"].How can I judge
fieldsIs the split array empty?You can useifstatements to combinelengthA filter to determine if the length of the array is 0, or inforUsing a loopemptyA keyword 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 %}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. Instead, you should usesplitThe filter, which allows you to specify any custom delimiter to split strings. For example,"Apple,Banana,Orange"|split:","Can be split by commas, and it will return the same[]string{}type.