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.