What is the type of the string array after splitting the `fields` filter in AnQiCMS template?

Calendar 👁️ 73

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)

  1. 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["你", "好", "世", "界"].
  2. How can I judgefieldsIs 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 %}
    
  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. 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.

Related articles

How to split a long string into an array of strings in AnQiCMS template?

During AnQiCMS template development, we often encounter situations where we need to flexibly handle data, one common requirement being to split a long string containing multiple pieces of information into an independent string array using a specific delimiter (such as a space), so as to facilitate loop display or further operations.AnQiCMS's template engine provides a concise and efficient filter (Filter) function, which can easily achieve this goal.### Understand `split`

2025-11-08

How to use the `autoescape` tag to control the automatic escaping of HTML content in AnQiCMS templates?

It is crucial to understand and effectively control the escaping behavior of HTML content during AnQiCMS template development.This not only concerns the correct display of page content, but also directly affects the security of the website, especially in preventing cross-site scripting (XSS) attacks.AnQiCMS's template system provides a flexible mechanism to manage this, with the `autoescape` tag playing a core role.

2025-11-08

escape` filter and `e` filter in AnQiCMS template are they functionally the same?

During the development of AnQiCMS templates, the security of data output is a focus we need to pay attention to.Frequently encounter issues about `escape` filter and `e` filter, many users are curious about whether there are differences in their functions.

2025-11-08

How does the AnQiCMS template escape special characters in HTML or JavaScript code to prevent XSS attacks?

When building a website with AnQiCMS, we often need to fill dynamic content into the page template, which includes text that may come from user input.However, if not handled properly, the content entered by these users may be exploited by malicious attackers to plant malicious scripts, thereby triggering cross-site scripting (XSS) attacks.XSS attacks can steal user data, tamper with page content, even hijack user sessions, causing serious harm to websites and users.AnQiCMS as a content management system that focuses on security

2025-11-08

How to get the string in AnQiCMS template?

In AnQiCMS template development, text and strings are the foundation for building website content.Flexible and efficient string retrieval and manipulation are skills that template developers must master, whether it is to display article titles, website names, or process user input.AnQiCMS's powerful template engine is based on Go language, providing rich tags and filters, making string acquisition and processing intuitive and practical.### Core Mechanism: Understanding the String Retrieval Method of AnQiCMS Template The most direct way to retrieve strings in AnQiCMS templates is through variable references

2025-11-08

How to display the title and content of articles in AnQiCMS templates?

Manage website content in AnQiCMS, the ultimate goal is to be able to present this content to visitors in an elegant and efficient manner.For the most common content type of articles, how to accurately display the title and body in front-end templates is a basic skill that every website operator needs to master.AnQiCMS powerful template function, providing us with a flexible and intuitive implementation method.### Core: Get to know the `archiveDetail` tag AnQiCMS's template system is designed to be very user-friendly

2025-11-08

How to implement personalized display of front-end content in AnQiCMS?

In today's era of content overload on the internet, how to make a website stand out and provide a unique and valuable experience to visitors has become crucial.The personalized display of content is an important means to enhance user stickiness, conversion rate, and brand image.AnQiCMS as an efficient and customizable content management system provides many features to help us easily achieve this goal.The flexible construction of content models: the foundation of personalized display The first step in personalized display is to make our content inherently personalized.The flexible content model feature of AnQiCMS

2025-11-08

What template file extensions and storage locations does AnQiCMS support?

AnQiCMS as a flexible and efficient content management system provides powerful template customization capabilities.It is crucial to make full use of this advantage of AnQiCMS and understand the suffix and storage location of its template files.This can not only help you design websites more efficiently, but also locate and solve problems faster when encountering them.First, AnQiCMS template files use the `.html` suffix.This means that all template files you create or edit should be saved in HTML format. These`

2025-11-08