When using Anqi CMS for template development, we often need to handle and display data flexibly.This includes string splitting, slicing, and element connection.AnQi CMS provides a rich set of filters (filters) to help us complete these tasks, such asadd/sliceandsplitSometimes, we might consider thatsliceorsplitarray elements obtained after filtering, throughaddFilter them together to form a new string. But whether this idea is feasible and how to correctly implement such operations is worth in-depth discussion.

Deep understandingaddFilter

Let's take a look firstaddThe purpose of the filter in the Anqi CMS template design. 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 a string or a number mixed with a string, it will concatenate 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 that,addThe design intention of the filter is to handle operations between two independent scalar values (i.e., single numbers or strings).It can intelligently identify data types and perform corresponding addition or concatenation operations, but it does not have the function to process 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 AnQi CMS templates.

splitThe filter will split a string into an array (in Go language, 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 a string or array. 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], usingslice:"3:7"it will obtain[4, 5, 6, 7].

These two filters have the common point that their output results are allarrays (or slices), rather than single 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 concatenate the elements of the array individually.It does not have the function to traverse an array and apply a connector. If an array is forced to be passed toaddA filter, the template engine of Anqi CMS may attempt to convert the entire array object into a string that represents its internal structure (for example[element1 element2]This is not the expected result when concatenating, but usually

The correct method is to usejoinFilter

In Anqi CMS template, a special and efficient tool is provided for concatenating strings of array elements——joinA filter that can combine all elements of an array into a single string using a specified delimiter. This issliceorsplitThe correct method to concatenate array elements into a new string.

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

Assuming we have a comma-separated keyword string and we want to convert it to 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 combinationsliceThis is an example. Suppose we have a string of numeric characters, we need to extract a part of it and connect with a vertical line:

{% 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 requirement of joining array elements into a string.

Consideration for practical application

Understanding the specific use of each filter is crucial in the development of Anqi CMS templates.addThe filter is suitable for simple numeric addition and string concatenation, whilesliceandsplitused for array creation and operations. When we need to recombine the elements of these arrays into a single string,joinThe filter is the correct choice. Using the right tool not only ensures the clarity of code logic and high execution efficiency, but also helps to avoid potential errors or unexpected results caused by misuse of the filter.Master these basic but powerful filters and it will make your CMS template development more effortless.


Frequently Asked Questions (FAQ)

Ask 1: If I really try to useaddWhat will happen when the filter tries to concatenate an array?Answer:addThe filter will try to convert the entire array object into a string representation (for example[element1 element2 element3]),then join it withaddThe other string concatenation. The result you get will be a string containing an array structure representation, not the one you expected composed of array elements and separators.Therefore, to get the correct result, it should always be usedjoina filter to join array elements.

Question 2:joinA filter can do more than just join strings? For example, can it join 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 types,joinThe filter will implicitly convert these elements to strings and then concatenate them. Therefore, regardless of the type of data contained in the array,joinThey will try to convert it to a string and concatenate it.

Question 3:splitFilters andmake_listWhat are the main differences when the filter splits a 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 each UTF-8 character. Whilemake_listThe filter is simpler and more forceful, it always splits everycharacterInclude Chinese characters and split them into an independent array element. For example,"你好"|make_listYou will get["你", "好"],"Hello"|make_listYou will get["H", "e", "l", "l", "o"]Choose which one depends on the rules you need to split the string according to.