During the template development process of AnQi CMS, flexibly handling and displaying data is the key to enhancing website functionality and user experience.Sometimes, the content we get from the background may be a single line string containing multiple pieces of information, such as a list of keywords, product tags, or a set of feature descriptions.If you need to process or display this information as separate elements, for example, in the form of a list, we need a method to split this single string into multiple independent items.fieldsThe filter can be used.
What isfieldsFilter?
fieldsThe core function of the filter is that it can intelligently split a string containing spaces into an array of strings (commonly represented as an array in Go language)[]string{}This means that as long as your data items are separated by one or more spaces,
this is the translation task for you.fieldsThe filter can help you parse them into individual elements, making it convenient to loop and display them in templates.
Why is it neededfieldsFilter?
In practical content operations, we may encounter the following scenarios,fieldsThe filter can effectively solve these problems:
- Keyword list display:The website background stores multiple keywords for articles in a certain field, separated by spaces.The front-end needs to render these keywords as separate tags, each of which can be clicked or have different styles.
- List of product features:
- User input label processing:Allow users to enter custom tags in articles or product descriptions, and these tags are separated by spaces.In order to avoid directly displaying the original string, it is necessary to split them and present them in a standardized manner.
ByfieldsFilter, we can transform these seemingly unordered strings into structured data sets, thus enabling flexible style control and interactive design on the front end.
fieldsHow to use the filter
UsefieldsThe filter is very intuitive. Its basic usage is to pass a string variable or a direct string literal through a pipe operator|pass tofieldsThe filter. However, due tofieldsThe filter returns an array, we usually use{% set %}tags to store the result in a temporary variable for later iteration.
Assuming you have a string variablemyStringIts value is"安企CMS GoLang 内容管理 高效 灵活".
First, you need to pass this string throughfieldsfilter processing, and assign the result to a new variable, such astagList:
{% set myString = "安企CMS GoLang 内容管理 高效 灵活" %}
{% set tagList = myString|fields %}
Now,tagListwhich becomes a container that includes["安企CMS", "GoLang", "内容管理", "高效", "灵活"]This is a string array. You can verify its structure throughstringformata filter:
{{ tagList|stringformat:"%#v" }}
{# 显示结果可能是:[]string{"安企CMS", "GoLang", "内容管理", "高效", "灵活"} #}
Next, we can usefora loop tag to traversetagListRender each element and display it on the page.
<div class="tags-container">
{% for tag in tagList %}
<span class="tag-item">{{ tag }}</span>
{% endfor %}
</div>
This code will generate the following HTML structure (the actual style depends on your CSS):
<div class="tags-container">
<span class="tag-item">安企CMS</span>
<span class="tag-item">GoLang</span>
<span class="tag-item">内容管理</span>
<span class="tag-item">高效</span>
<span class="tag-item">灵活</span>
</div>
Using such a combination, we easily convert a string into a list element that can be independently operated and displayed.
Example of practical application: Dynamic keyword tags
Imagine a scenario, you need to display the key words of the article on the article detail page. There is a field in the background article informationarchive.Keywords, its value might be"网站运营 SEO 流量分析 内容营销".
In the template, you can use it like thisfieldsa filter to dynamically generate key word tags:
{% if archive.Keywords %}
{% set keywordsArray = archive.Keywords|fields %}
<div class="article-keywords">
<strong>关键词:</strong>
{% for keyword in keywordsArray %}
<a href="/search?q={{ keyword|urlencode }}" class="keyword-tag">{{ keyword }}</a>
{% endfor %}
</div>
{% endif %}
This code first checksarchive.Keywordsif there is a value, then it splits it intokeywordsArray.Then, traverse this array to create clickable links for each keyword that point to the search results page of the website, and perform URL encoding on the keywords to ensure the correctness of the links.
Summary
fieldsThe filter is a simple yet very powerful tool in the Anqi CMS template engine, making it extremely efficient and convenient to process space-separated string data.By converting these strings into an iterable array, we can achieve more refined content display and interaction functions in the front-end template, thereby significantly enhancing the flexibility and user experience of the website.fieldsusing filters, which will bring more possibilities to the content operation of your Anqin CMS website.
Frequently Asked Questions (FAQ)
Question:
fieldsCan the filter also split strings using other symbols (such as commas, semicolons) instead of spaces?Answer:fieldsThe filter is specifically designed to split strings according tospacesSplit. If your string is connected with other delimiters (such as comma,or semicolon;), you should usesplitfilter.splitThe filter allows you to specify any delimiter, for example{{ myString|split:"," }}You can split a comma-separated string into an array.Question:
fieldsWhat is the data type of the data obtained after splitting the filter, and what operations can I perform on it?Answer:fieldsThe filter splits to get a string array type ([]string{})。This means you can use{% for %}The loop tag can be used to iterate over each element of the array, you can also use other array-specific filters, such aslengthto get the length of the array,firstget the first element,lastget the last element, etc.Ask: If my string contains multiple consecutive spaces,
fieldshow will the filter handle? For example,"安企 CMS 内容"what will it be split into?Answer:fieldsThe filter treats consecutive spaces as a delimiter and automatically removes empty strings. So, for"安企 CMS 内容"such a string, it will be split into["安企", "CMS", "内容"]Such an array containing three elements, which will not produce any extra empty string elements due to the two spaces in the middle.