When developing templates for Anqi CMS, we often need to handle and display data flexibly.This includes string splitting, truncation, and element concatenation.add/sliceandsplitSometimes, we might consider tosliceorsplitAn array element obtained after filter processing, throughaddFilters are connected to form a new string. But is this idea feasible, and how to correctly implement such operations, is worth our in-depth discussion.

Deep understandingaddFilter

Firstly, let's take a look ataddThe design purpose of the filter in the Anqi CMS template. According to the document,addThe filter is mainly used to add or concatenate numbers and strings.When it operates on two numbers, it performs arithmetic addition; when it operates on strings or a mix of numbers and strings, it concatenates them together.

For example, if we have the following code:

{{ 5|add:2 }} {# 输出结果:7 #}
{{ "安企"|add:"CMS" }} {# 输出结果:安企CMS #}
{{ 5|add:"CMS" }} {# 输出结果:5CMS #}

From these examples, we can see,addThe design intention of the filter is to handle the operation between two independent scalar values (i.e., individual numbers or strings).It can intelligently identify data types and perform corresponding addition or concatenation operations, but it does not have the function to handle 'multiple elements in an array' and concatenate them into a string.

sliceandsplitThe role of the filter

sliceandsplitThe filter plays the role of extracting substrings from strings or splitting strings into multiple parts in the Anqi CMS template.

splitThe filter will split a string into an array (in Go language, it's called a slice) according to the specified delimiter. For example,"安企,CMS,模板,开发"|split:","you will get an array containing four string elements.["安企", "CMS", "模板", "开发"].

sliceThe filter is used to extract elements at specified positions from strings or arrays. When it acts on an array, it returns a new sub-array. For example, if there is a numeric array[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]Useslice:"3:7"you will get[4, 5, 6, 7].

The common point of these two filters is that their output results arearrays (or slices), rather than individual strings.

addincompatible with arrays.

Understoodadd/sliceandsplitAfter each function, we can clearly answer the question at the beginning:addFilterCannot be used directly for connectionsliceorsplitArray elements after filter processing to form a new string.

This is becauseaddThe filter does not understand how to concatenate an array as a whole with other strings, or to join the elements of the array individually.It does not have the function to traverse the array and apply the connector.addFilter, the template engine of AnQi CMS might try to convert the entire array object into a string representing its internal structure (for example[element1 element2]This usually is not the result we expect when concatenating, then joining.

The correct method: usingjoinFilter

In the AnQi CMS template, a special and efficient tool is provided for concatenating strings of array elements.joinFilter. It can combine all elements of an array into a single string using a specified delimiter (separator). This is exactlysliceorsplitThe correct method to concatenate elements of an array into a new string.

Let's demonstrate how to use it through an example.splitorsliceCombinejoinTo achieve the purpose using a filter:

Suppose we have a comma-separated keyword string and we want to convert it into a new string connected by hyphens:

{% set original_string = "安企,CMS,模板,开发" %}
{% set words_array = original_string|split:"," %} {# 此时 words_array 为 ["安企", "CMS", "模板", "开发"] #}
{% set new_string = words_array|join:"-" %} {# 使用 join 过滤器将数组元素用短横线连接 #}
{# 输出 new_string 的结果:安企-CMS-模板-开发 #}

Let's see another combinationsliceThe example. Suppose we have a string of digits and need to extract a part of them and connect them with a vertical bar:

{% set numbers_string = "1,2,3,4,5,6,7,8,9,10" %}
{% set numbers_array = numbers_string|split:"," %} {# 得到数组:["1", "2", "3", ..., "10"] #}
{% set sliced_numbers = numbers_array|slice:"3:7" %} {# 截取数组的一部分,得到 ["4", "5", "6", "7"] #}
{% set joined_numbers = sliced_numbers|join:"|" %} {# 使用 join 过滤器将截取后的数组元素用竖线连接 #}
{# 输出 joined_numbers 的结果:4|5|6|7 #}

Through these two examples, we can clearly seejoinHow the filter perfectly solves the need to concatenate array elements into a string.

Considerations for practical applications

In the development of Anqi CMS templates, understanding the specific purpose of each filter is crucial.addFilters are suitable for simple arithmetic addition and string concatenation, whilesliceandsplitUsed for array creation and manipulation. When we need to recombine the elements of these arrays into a single string,joinFilter is the correct choice.Using the right tools not only ensures that the code is logically clear and executes efficiently, but also helps avoid potential errors or unexpected results from misuse of filters.Master these basic but powerful filters, and it will make your security CMS template development more at ease.


Common Questions (FAQ)

Question 1: If I really try to useaddWhat happens when you filter an array?Answer:addThe filter will try to convert the entire array object into a string representation (for example)[element1 element2 element3]), and then compare it withaddThe concatenation of another string.The result you get will be a string represented by an array structure, not the string composed of array elements and separators that you expected.joinJoin array elements with a filter.

Question 2:joinCan a filter do something else besides joining strings? For example, joining a numeric array?Answer:joinThe filter is specifically designed to concatenate elements of an array into a string. It handles string arrays well, but if the array contains numbers or other non-string data,joinThe filter will implicitly convert these elements to strings first, and then concatenate them. Therefore, regardless of the type of data contained in the array,joinWill try to convert it to a string and concatenate.

Question 3:splitfilters andmake_listWhat are the main differences when the filter splits the string into an array?Answer:splitThe filter is based on what you specifySeparatorSplit strings, for example"A,B,C"|split:","You will get["A", "B", "C"]. If the delimiter is an empty string"", it will split at each UTF-8 character. Andmake_listthe filter is simpler and more brute force, it always takes eachCharacter(including Chinese characters) are split into a separate array element. For example,."你好"|make_listYou will get["你", "好"],"Hello"|make_listYou will get["H", "e", "l", "l", "o"]. The choice depends on the rules you need to split the string by.