How to sort the array split by the `split` filter according to custom rules?

Calendar 👁️ 76

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

Related articles

Does the AnQiCMS template have a direct method to remove duplicate elements from an array split by the `split` filter?

In AnQi CMS template development, we often use various filters (filters) to process and handle data to meet the needs of front-end display.Among them, the `split` filter is undoubtedly a very practical tool that can help us split strings of specific formats into arrays, such as converting comma-separated tag strings into a list of tags.However, when these sliced arrays contain duplicate elements, many friends will naturally think of: 'Does AnQiCMS template support a direct method to remove these duplicate elements?' Today

2025-11-08

How to combine `split` filter with `urlencode` filter to handle multi-value strings in URL parameters?

In the powerful template system of AnQi CMS, flexibly handling and displaying website data is the key to improving user experience and SEO effectiveness.Among them, converting a specific format string stored in the database into a safe and usable multivalue string in the URL parameters is a common requirement.This article will deeply explore how to巧妙ly combine the `split` and `urlencode` filters in the AnQiCMS template to solve this practical problem.

2025-11-08

What are the special requirements for handling delimiters in the `split` filter across multilingual content (such as multilingual tags)?

In AnQi CMS, with the increasing trend of website globalization operations, we often encounter situations where we need to deal with multilingual content.Among them, the management of document tags (Tag) is a typical example.In order to better organize and display these tags, the application of the `split` filter in the template is particularly important.It can help us convert label data stored as strings into a traversable list.However, in the context of cross-language content, the handling of delimiters by the `split` filter is not always intuitive, and this requires our special attention.

2025-11-08

What is the recommended method to debug the splitting result of the `split` filter in the template?

In Anqi CMS template development, flexible string handling is an essential part of content presentation.The `split` filter is a powerful tool that can split strings of a specific format into arrays according to a specified delimiter, which is particularly useful in scenarios such as handling article tags and multi-value fields.

2025-11-08

Does the `split` filter apply to extracting specific attribute values from HTML content, such as `data-items="item1|item2"`?

In AnQi CMS template development, we often encounter situations where we need to handle different types of data.When it comes to extracting specific attribute values from HTML content, such as `data-items="item1|item2"`, and you want to further process these values, the `split` filter is a very useful tool.However, its applicability is not directly aimed at HTML parsing, but rather at the **string data already obtained**.###

2025-11-08

What error message or default behavior will occur if the input received by the `split` filter is not a string type?

Anqi CMS is an efficient enterprise-level content management system that provides a rich set of tags and filters for template creation, helping us to flexibly display content.Among them, the `split` filter is a very practical tool that can split a string into an array according to a specified delimiter, which is particularly convenient in handling scenarios such as keyword lists, multi-value fields, etc. ### `split` filter's working principle and expected input We all know that the main function of the `split` filter is to "split strings".Imagine that

2025-11-08

The `split` filter combined with `archiveDetail` or `categoryDetail` tags, what are some advanced usages to extract and process fields?

In AnQi CMS template development, the combination of the `split` filter with `archiveDetail` or `categoryDetail` tags and others provides powerful flexibility for us to extract and process data from fields.This not only makes the display of website content more refined and dynamic, but also better meets the specific content operation needs.

2025-11-08

How to split and render multi-level navigation path strings when creating a dynamic navigation menu using the `split` filter?

In the daily operation of Anqi CMS, we often need to build flexible and diverse navigation menus to adapt to the ever-changing content structure and user needs.Although AnQi CMS provides a powerful `navList` tag for managing background configuration navigation, in certain specific scenarios, such as when we need to dynamically generate multi-level navigation based on a string storing complete path information, or render a breadcrumb navigation with a depth far exceeding two levels, the built-in tag may not fully meet our refined needs.

2025-11-08