fieldsFilter, it helps us easily achieve this goal.
fieldsFilter: Converts a string into an array of 'words'.
The template system of Anqi CMS draws on the powerful syntax of Django template engine, making content processing intuitive and efficient.fieldsThe filter is an example, its core function is to split a text string by spaces and convert it into an iterable string array.This means, it treats each substring separated by a space as a 'word'.
Imagine that you have a content variable that contains a description like 'AnQiCMS is an enterprise-level content management system based on the Go language'.If you want to handle or display each word of this sentence individually, it would be difficult to do so directly.fieldsThe process of filter becomes very simple:
{% set content_string = "AnQiCMS 是一个基于 Go 语言开发的企业级内容管理系统" %}
{% set words_array = content_string|fields %}
{# 此时,words_array 会包含一个类似这样的数组:
["AnQiCMS", "是一个", "基于", "Go", "语言开发的企业级", "内容管理系统"]
#}
As you can see,fieldsThe filter effectively splits the original string into multiple parts based on spaces and stores them in a namedwords_array.
Further processing extracts the 'words' obtained
Once we convert the string into an array, we can make use of the loop and conditional judgment features provided by the AnQiCMS template system to perform various flexible operations on these 'words'.
1. Traverse and display
The most basic application is to traverse this array and display each 'word' individually.For example, you might want to add some style to each word or organize them into a list.
<div class="word-list">
{% for word in words_array %}
<span class="highlight-word">{{ word }}</span>
{% endfor %}
</div>
So, each extracted "word" will be wrapped in a tag withhighlight-wordsuch style<span>for easy style control on the front-end.
2. Concatenate and combine
Sometimes, we may need to reassemble these extracted "words" into a new string, but with different delimiters.joinThe filter comes into play. For example, if you want to connect all words with commas and spaces:
<p>重新拼接后的内容:{{ words_array|join(", ") }}</p>
{# 输出可能为:AnQiCMS, 是一个, 基于, Go, 语言开发的企业级, 内容管理系统 #}
This is very useful when generating keyword tags or adjusting the display format of content.
3. English Statistics and Search
Understanding how many "words
Calculate the total count:Use
lengthThe filter can easily obtain the total number of 'words' in the array.<p>这段内容总共提取了 {{ words_array|length }} 个“单词”。</p>Find a specific word:
containThe filter can help you determine if the array contains a specific 'word' match.{% if words_array|contain:"内容管理系统" %} <p>页面内容中提到了“内容管理系统”这个重要的词。</p> {% else %} <p>页面内容中未包含“内容管理系统”。</p> {% endif %}In addition,
countThe filter can calculate the number of times a specific "word" appears in an array.<p>“Go”这个词在内容中出现了 {{ words_array|count:"Go" }} 次。</p>
4. Conditional judgment and filtering
You can also combine conditional judgments to perform special processing on certain specific 'words'. For example, when a certain word appears, give it a special color or style:
<div class="processed-text">
{% for word in words_array %}
{% if word == "AnQiCMS" or word == "Go" %}
<strong style="color: blue;">{{ word }}</strong>
{% else %}
{{ word }}
{% endif %}
{% endfor %}
</div>
This code will iterate over all "words
fieldsFilter is related tosplitThe Difference of Filters
It is worth noting that,fieldsThe filter defaults to using one or more spaces as delimiters. This means that if the "words" in your string are separated by other characters (such as commas, semicolons, pipes, etc.), thenfieldsThe filter may not achieve the effect you expect.
In this case,splitThe filter may be a more flexible choice.splitThe filter allows you to specify any delimiter to split a string into an array. For example, if your keywords are 'SEO optimization, website promotion, content marketing':
{% set keywords_string = "SEO优化,网站推广,内容营销" %}
{% set keywords_array = keywords_string|split(",") %}
{# 此时 keywords_array 会包含:["SEO优化", "网站推广", "内容营销"] #}
<div class="keyword-tags">
{% for keyword in keywords_array %}
<span class="tag">{{ keyword|trim }}</span>{# 使用 trim 过滤器去除可能存在的多余空格 #}
{% endfor %}
</div>
here, we used a comma assplitthe delimiter of the filter, successfully extracted keywords separated by commas.
Summary
fieldsThe filter is a seemingly simple but powerful tool in the AnQiCMS template system, which makes it easy to extract "words" from strings and perform subsequent processing. Whether for content analysis, dynamic display, or better page information management, masteringfieldsand related tojoin/length/contain/count/splitFilters such as auto can significantly improve your content operation efficiency and website flexibility. Encourage you to try more in practice and explore their more wonderful uses in different scenarios.
Common Questions (FAQ)
Q:
fieldsThe filter can only be split by spaces? What if my words are separated by other symbols, such as commas?Answer: Yes,fieldsThe filter is specifically used to split strings by spaces (including multiple consecutive spaces). If your 'words' are separated by commas, semicolons, pipes, and other symbols, you should usesplitFilter.splitThe filter allows you to specify any character as a delimiter, for example.{{ your_string|split(",") }}.Q:
fieldsWill the 'words' extracted by the filter include punctuation?Answer:fieldsreplaceorcutAnd then the filter is processed again.问:Extracted 'word' array is case-sensitive?答:是的,AnQiCMS模板系统在处理字符串时,默认是区分大小写的。This means that if your array contains "CMS" and "cms
lowerorupperThe filter converts all strings to uniform format.