In AnQiCMS, content operators often need to flexibly handle and display text information.Sometimes, we may need to extract all the 'words' from a long string, whether it is for keyword analysis, making tag clouds, or simply for reorganizing content display.At this time, the AnQiCMS template system provides a very practical tool——fieldsA filter that can help us easily achieve this goal.

fieldsA filter: converts a string into an array of 'words'

The AnQi CMS template system, inspired by the powerful syntax of Django template engine, makes content processing intuitive and efficient.fieldsThe filter is an example of this, 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 you have a content variable that contains descriptions similar to 'AnQiCMS is an enterprise-level content management system developed based on the Go language.'If you want to process or display each word in this sentence individually, it would be difficult to do so directly.However, throughfieldsFiltering, this process 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 split the original string into multiple parts by spaces and stored them in a namedwords_array.

Further processing extracted the "word"

Once we convert the string into an array, we can use the loop and conditional judgment functions provided by the AnQiCMS template system to flexibly process 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>

Thus, each extracted "word" will be wrapped in a tag withhighlight-wordstyled one.<span>label 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. At this time,joinThe filter comes into play. For example, you might 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.

3. Counting and Searching

Understanding how many 'words' are extracted, or checking for the existence of a specific 'word', is also a common requirement.

  • Calculate the total:UselengthThe filter can easily obtain the total number of "words" in the array.

    <p>这段内容总共提取了 {{ words_array|length }} 个“单词”。</p>
    
  • Search for a specific word: containThe filter can help you determine whether the array contains an exact match of the "word".

    {% if words_array|contain:"内容管理系统" %}
        <p>页面内容中提到了“内容管理系统”这个重要的词。</p>
    {% else %}
        <p>页面内容中未包含“内容管理系统”。</p>
    {% endif %}
    

    Furthermore,countThe filter can calculate the number of times a specific matching "word" appears in an array.

    <p>“Go”这个词在内容中出现了 {{ words_array|count:"Go" }} 次。</p>
    

4. Conditional judgment and filtering

You can also combine conditional judgment to perform special processing on certain 'words'. For example, when a 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", and if it encounters "AnQiCMS" or "Go", it will be displayed in bold blue, and the rest will be displayed normally.

fieldsthe filter meetssplitThe difference between filters

It is worth noting that,fieldsThe filter defaults to using one or more spaces as separators. 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 will 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 use the comma assplita separator for the filter, successfully extracting 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 it is for content analysis, dynamic display, or better management of page information, masteringfieldsand relatedjoin/length/contain/count/splitAll filters can significantly improve your content operation efficiency and website flexibility. Encourage you to try them out more in practice and discover more uses in different scenarios.


Frequently Asked Questions (FAQ)

  1. Question:fieldsCan the filter only be split by spaces? What should I do 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(",") }}.

  2. Question:fieldsWill the 'word' extracted by the filter contain punctuation?Answer:fieldsThe filter is separated by spaces. This means that if a "word" is next to a punctuation mark (such as "system.")Or (AnQiCMS), and if it is without spaces between the next word, then these punctuation marks will be considered as part of the word.If you need to remove these punctuation marks, you may need to combinereplaceorcutand other filters are processed twice.

  3. Ask: Is the 'word' array case-sensitive?Yes, the AnQiCMS template system is case-sensitive by default when processing strings.This means that if your array contains "CMS" and "cms", they are considered two different "words".If you need to perform case-insensitive processing, you can uselowerorupperThe filter converts all strings to