In website operation, we often need to handle various data, and sometimes these data are stored in a string format.AnQiCMS provides powerful template tags and filters to make content display flexible and efficient.splitA filter is a very practical tool that can split a string into an array according to a specified delimiter, making it convenient for us to traverse and display the data further.
However, when we split a string into an array, we sometimes encounter the need to sort the elements of the array.For example, we may store multiple tags of an article in a field, separated by commas, and hope that these tags can be displayed in alphabetical order on the front end.splitAfter filtering, the array is sorted according to a custom rule?
UnderstandingsplitThe principle of the filter.
First, let's quickly review.splitThe basic usage of the filter. Its function is to split a string into an array of strings according to the delimiter you provide (usually called a slice in the Go language template engine).
For example, if you have a string containing multiple keywords, like this:"网站优化,搜索引擎优化,内容营销,用户体验".
We can usesplita filter to convert it into an array:
{% set keywords_string = "网站优化,搜索引擎优化,内容营销,用户体验" %}
{% set keywords_array = keywords_string|split:"," %}
{# keywords_array 现在是一个包含 ["网站优化", "搜索引擎优化", "内容营销", "用户体验"] 的数组 #}
<ul>
{% for item in keywords_array %}
<li>{{ item }}</li>
{% endfor %}
</ul>
This code will output each element of the array in the order of the original string.
Use cleverlyforrepeatedlysortedParameters are sorted.
The AnqiCMS template engine (based on Django template syntax) provides a very convenient built-in feature that can be used toforSort the data set directly in the loop. This feature isforrepeatedlysortedthe parameter. When we passsplitthe filtered array to theforloop, we can use this parameter to sort directly.
sortedThe parameter will sort the string array in dictionary order (i.e., alphabetical order) and the numeric array in ascending order. Due tosplitThe filter returns a string array, thereforesortedThe parameters will be sorted in dictionary order.
Let's see how to apply it:
{% set tags_string = "SEO优化,网站安全,内容营销,用户体验,多站点管理" %}
{% set tags_array = tags_string|split:"," %}
<h3>按字典序(字母顺序)排列的标签:</h3>
<ul>
{% for tag in tags_array sorted %}
<li>{{ tag }}</li>
{% endfor %}
</ul>
Run this code, and you will find that the labels are sorted in alphabetical order, for example:
- Content marketing
- Multi-site management
- SEO optimization
- Website security
- User Experience
This method is concise and efficient, very suitable for basic alphabetical ordering of split string arrays.
When default sorting is not enough: Deeply customize the rules.
ThoughforrepeatedlysortedThe parameter can meet most basic sorting requirements, but it also has its limitations.It primarily provides default sorting based on the element itself (strings in dictionary order, numbers by value).
- Sort strings by length (longest first or shortest first