In the template development of AnQi CMS, flexibly using various filters (Filters) can greatly facilitate our formatting of content.When you need to split a string into multiple parts by a specific delimiter and then further format each part (that is, each element of the array), such as converting them all to uppercase or lowercase, you cannot simply chain calls like you would for a single string.
The template engine of AnQi CMS supports syntax similar to Django, which provides powerfulsplitFilter, which can split a string into an array (or slice) according to a specified delimiter. For example, you might have a field storing article tags with the content 'SEO, marketing, content operation', when you usesplitWhen filtering and specifying comma and space as delimiters, it will get an array containing the elements 'SEO', 'Marketing', and 'Content Operation'.
However, if you try to directly apply a result to thissplitoperation with something likeuppersuch a filter, for example written 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 when,upperThe filter does not understand how to convert an array to uppercase, so this direct chaining will fail due to type mismatch.
To solve this problem, we need to change our way of thinking: sincesplitThe filter has successfully converted the string into an array containing multiple elements, so we just need to traverse the array and then apply the required formatting filter to each element of the array.
Here, the template of AnQi CMS comes into play.forThe loop tag comes into play.forThe loop allows us to access each element in the array one by one. The specific implementation steps can be divided into the following steps:
Firstly, we can usesettags tosplitThe result of the filter is stored in a variable, which helps improve the readability and organization of the template. For example, suppose we have a variablearticleTagswith the value"SEO, 营销, 内容运营"We can operate like this:
{% set tagsArray = articleTags|split:", " %}
Now,tagsArrayJust stored an array containing multiple tag strings.
Now, we can useforin a loop to iterate overtagsArrayof each element. 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>
Running this template code will give you the following output:
<ul>
<li>SEO</li>
<li>营销</li>
<li>内容运营</li>
<li>CMS</li>
</ul>
Similarly, if you want to convert to lowercase, justupperwithlower:
{% 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 kind of throughsplitGenerate an array, and then combineforThe way to process elements in a loop is a powerful and flexible method for handling complex data formats in Aanqi CMS templates. It is not only suitable forupperandlowerand can also be used withcapfirst(with the first letter capitalized),trimEnglish
Common Questions (FAQ)
1.splitfilters andmake_listWhat are the differences between filters?
splitEnglish"a,b,c"|split:","You will get["a", "b", "c"]If the delimiter does not exist, it returns an array containing only the original string.make_listThe filter is to take each character (including Chinese characters, letters, symbols, spaces, etc.) in the string separately as an element and directly convert it into an array. For example"你好"|make_listYou will get["你", "好"]. Choose which one depends on the rules you need to decompose the string according to.
2. Why can't it be done directly?{{ myString|split:", "|upper }}Why is it chained like this?
This is because the filter's target object type is fixed.splitThe filter receives a string and returns an array, whileupperThe filter (andlower/capfirstetc.) receives a string and returns a formatted string. Whensplitit returns an array,upperThe filter cannot process this type of array data as it expects a single string input, therefore direct chaining will fail. We need to go throughforExtract elements from the array one by one and apply a string filter to each element.
3. BesidesupperandlowerWhat filters can be applied to a single element?
Once throughforLoop to getsplitThe single element in the array, you can almost apply all the string processing filters provided by the security CMS. For example:
|capfirst: Convert the first letter of a string to uppercase.|trim: Remove spaces or specified characters from the beginning and end of the string.|truncatechars:10: Extract the string, keeping the first 10 characters and adding “…”.|replace:"old,new": Replace a specific substring in the string.|urlencode: URL encode the string. These filters can be combined.forPerform fine-grained processing on each element of the array in a loop.