The `split` filter splits an array, if you need to format each element (such as `upper` or `lower`), how to implement chaining?

Calendar 👁️ 70

In Anqi CMS template development, flexibly using various filters (Filters) can greatly facilitate our formatting of content.When we encounter the need to split a string into multiple parts by a specific delimiter, and then to further format each part (that is, each element of the array) for example, to convert them all to uppercase or lowercase, we cannot simply chain calls like we do for a single string.

The AnQi CMS template engine supports syntax similar to Django, providing powerfulsplitA filter that can split a string into an array (or slice) according to a specified delimiter. For example, you might have a field storing article tags, whose content is 'SEO, marketing, content operation', when you usesplitThe filter, when specified with commas and spaces as delimiters, will get an array containing the elements 'SEO', 'Marketing', and 'Content Operation'.

But if you try to directly apply this tosplitthe result of the operation and then apply a filter likeuppersuch as writing it as{{ myString|split:", "|upper }}you will find that this does not give you the desired effect. This is becauseupperThe filter is designed to process a single string, it expects to receive a string value, not a string array. WhensplitThe filter returns an array, upperThe filter does not understand how to convert an array to uppercase, therefore this direct chaining call will fail due to type mismatch.

We need to change our approach to solve this problem: sincesplitThe filter has successfully converted the string into an array containing multiple elements, so we just need to iterate through the array and apply the required formatting filter to each element separately.

here, in the Anqi CMS templateforthe loop tag comes into play.forThe loop allows us to access each element of the array one by one. The specific steps of implementation can be divided into the following steps:

First, we can usesetLabels willsplitThe result of the filter is stored in a variable, which helps to improve the readability and code organization of the template. For example, suppose we have a variablearticleTagsIts value is"SEO, 营销, 内容运营"We can operate like this:

{% set tagsArray = articleTags|split:", " %}

Now,tagsArrayStored an array containing multiple label strings.

Next, we can useforto loop throughtagsArrayEach element in. Inside the loop,forThe loop will provide a current element variable for each iteration (for exampleitem), at this point we can apply any string formatting filter to thisitemvariable.

For example, if we want to convert all labels to uppercase, we can do it like this:

{% set articleTags = "SEO, 营销, 内容运营, CMS" %}
{% set tagsArray = articleTags|split:", " %}

<ul>
{% for item in tagsArray %}
    <li>{{ item|upper }}</li>
{% endfor %}
</ul>

Run this template code, and you will get the following output:

<ul>
    <li>SEO</li>
    <li>营销</li>
    <li>内容运营</li>
    <li>CMS</li>
</ul>

Similarly, if you want to convert to lowercase, justupperReplacelower:

{% set articleTags = "SEO, 营销, 内容运营, CMS" %}
{% set tagsArray = articleTags|split:", " %}

<ul>
{% for item in tagsArray %}
    <li>{{ item|lower }}</li>
{% endfor %}
</ul>

Output Result:

<ul>
    <li>seo</li>
    <li>营销</li>
    <li>内容运营</li>
    <li>cms</li>
</ul>

This is done bysplitGenerate an array, and then combineforThe method of processing elements one by one is a powerful and flexible means for handling complex data formats in Anqi CMS templates. It is not only applicable toupperandlower, but can also be combined withcapfirst(capitalized first letter),trimRemove leading and trailing spaces and other string processing filters are combined to meet various content display needs.


Frequently Asked Questions (FAQ)

1.splitFilters andmake_listWhat are the differences between filters?

splitFilters are used to split strings into arrays according to the specified delimiter, for example"a,b,c"|split:","You will get["a", "b", "c"]If the delimiter does not exist, it will return an array containing the original string and whilemake_listThe filter is to take each character of a string (including Chinese characters, letters, symbols, spaces, etc.) and convert it into a separate element, and then directly convert it into an array. For example"你好"|make_listYou will get["你", "好"]. Which one to choose depends on the rules you need to decompose the string according to.

2. Why can't it be done directly?{{ myString|split:", "|upper }}Why not chain calls like this?

This is because the type of the object on which the filter operates is fixed.splitThe filter takes a string and returns an array, andupperThe filter (as well aslower/capfirstetc.) takes a string and returns a formatted string. Whensplitan array is returned, upperThe filter cannot handle this type of array data because it expects a single string input, so direct chaining will fail. We need to pass throughforLoop through the array elements individually, and then apply a string filter to each element.

3. BesidesupperandlowerWhat filters can be applied to a single element?

Once throughforLoop to getsplitThe single element of the array, you can almost apply all the string processing filters provided by AnQi CMS. For example:

  • |capfirstConvert the first letter of a string to uppercase.
  • |trim: Remove spaces or specified characters from the beginning and end of the string.
  • |truncatechars:10: Truncate the string to keep the first 10 characters and add “…”.
  • |replace:"old,new": Replace a specific substring in a string.
  • |urlencode: URL encode the string. These filters can be combined.forLoop to refine the processing of each element in the array.

Related articles

How to pass the array elements split by the `split` filter as parameters to other template tags or filters?

The template system of AnQiCMS (AnQiCMS) is favored by content operators for its powerful flexibility and ease of use.Among them, the `split` filter is a very practical feature that can help us split a string into an array (or called a slice in Go language) according to the specified delimiter.However, when we want to pass a specific part of this array as a parameter to other template tags or filters, we may feel a bit confused.Don't worry, this article will discuss in detail several efficient and commonly used methods to solve this problem.--- ###

2025-11-08

Does the `split` filter support more complex delimiter patterns, such as matching specific prefix and suffix characters?

In AnQiCMS template development, the `split` filter is a very useful tool that can help us split strings into arrays according to specified delimiters, which is especially convenient for handling data connected by specific characters.However, when it comes to more complex delimiter patterns, such as when it is necessary to match specific prefixes and suffixes of characters, the capabilities of the `split` filter are worth exploring in depth.

2025-11-08

How would the `split` filter handle consecutive delimiters in a string, for example `"a,,b"` using a comma as the delimiter?

AnQi CMS has always been favored by users for its flexibility and powerful functions in content display and management.When dealing with dynamic content, we often encounter the need to split strings and extract information.At this time, the `split` filter in the template is particularly practical.It can help us split a continuous string of text data into independent segments according to the specified delimiter, so that it can be displayed and processed more finely on the page.

2025-11-08

How to get the first or last element of an array split by the `split` filter?

When managing content in AnQi CMS, we often encounter the need to process some text information stored in a specific format.For example, the keywords of an article are usually stored in a field in the form of a comma-separated string, such as 'AnQi CMS, content operation, template making'.When we want to highlight the most important keyword on the website front end, such as an article list or detail page, or to obtain the last keyword as a hint, we need to split these strings and extract the specific elements.

2025-11-08

The `split` filter has what typical applications when processing user submitted form data (such as the values of checkboxes)?

The AnQi CMS provides great convenience for managing website content with its flexible content model and powerful template system.In daily operations, we often encounter the need to process form data submitted by users, especially those fields that allow for multiple selections, such as product specifications, article tags, or user interests.These data are usually stored as a string in the background, for example,

2025-11-08

How to use the `split` filter to extract a tag array from the article content in a specific citation format (such as `[tag1][tag2]`)?

When managing content in Anqi CMS, we often need to structure specific information in articles for front-end display or further data analysis.The article content may contain some references marked with a specific format, such as tags used to identify related topics, which appear in the form of `[tag1][tag2]`.}How can this seemingly continuous string be effectively extracted into an independent tag array, which is a practical problem many operators may encounter

2025-11-08

Does the `split` filter support case-sensitive delimiters for the string it processes?

In Anqi CMS template creation, we often need to process strings in various ways, where the `split` filter is a very practical tool that helps us split a long string into multiple parts according to the specified delimiter.However, a common issue when using this filter is: whether it distinguishes between uppercase and lowercase when handling delimiters?Let's delve deeper into this issue. ### The working principle of the `split` filter Firstly

2025-11-08

If you need to limit the maximum length of the array split by the `split` filter, is there a built-in parameter or method?

During the template development process of AnQi CMS, the `split` filter is a very practical tool that can help us conveniently split strings into arrays according to the specified delimiter, which is particularly important in various scenarios such as tag lists and keyword strings.However, some users may want to directly limit the maximum length of the array generated after using the `split` filter. Then, does the `split` filter built into AnQi CMS provide such parameters or methods?A thorough understanding of the system functions

2025-11-08