In the template development of AnQi CMS,joinA filter is a very practical tool, mainly used to concatenate elements of an iterable object (such as an array or list) into a single string with a specified separator. However, when we willjoinThe filter is applied to non-array objects, especially when strings, and its behavior may be different from what is initially imagined, but understanding its working principle can help us use it more flexibly.

joinBasic filter functionality review

First, let's take a look backjoinThe most common use of a filter. Usually, when you have a list or array of multiple elements and you want to connect these elements with a specific character to form a string, joinThe filter comes into play. For example, if you have a list that includes["苹果", "香蕉", "橙子"]and use|join(", "), you will get"苹果, 香蕉, 橙子"such a result.

Whenjoinfilter encountering a string: connect character by character

It's interesting, in the Anq CMS.joinThe filter does not treat a string object as an indivisible whole when processing it.On the contrary, it implicitly splits the input string into a sequence of individual characters, and then connects these independent characters with the delimiter you specify.

For example, suppose you have a string variablemyString = "安企CMS". If you apply it tojoinFilter, for example{{ myString|join("-") }}, you will find that the output is"安-企-C-M-S"This indicates that the filter treats each Chinese and English character as an independent element for processing and connection. This is similar to treating a character array (such as["安", "企", "C", "M", "S"])join("-")The result is completely consistent.

Speculation and suggestions on the behavior of other non-array objects

When dealing with basic non-array types such as numbers or boolean values,joinThe filter will typically implicitly convert these values to strings first, and then process them according to the above character-by-character concatenation rules. For example,{{ 12345|join("-") }}is likely to output"1-2-3-4-5"because it will convert numbers12345first convert it to a string"12345"then perform character concatenation.

However, for more complex non-array objects, such as a struct (struct) or a hash table (map), etc., if they do not have the iterative characteristics or do not have a clear definition of how to be split into a sequence, then they can be used directly.joinThe filter may cause template rendering errors, or simply output an empty string, the string representation of the original object (if it exists) without any concatenation effect. This is becausejoinThe filter needs a clear sequence of elements to operate.

Therefore, when using the filter on a non-array objectjoinThe best practice is:

  1. Understand its implicit conversion mechanismEspecially for string, number and other basic types, it is clear that they will be decomposed into individual characters or numbers for concatenation.
  2. Explicit preprocessingIf your data is a complex object and you need to concatenate its content, it is recommended to first convert it to a string or an explicit array structure using other methods.joinA filter to ensure predictability of behavior.

Consideration of practical application scenarios.

AlthoughjoinThe character-by-character behavior of a filter when processing strings may not be commonly used, but it can also be effective in certain specific scenarios:

  • Formatting identifier: For example, display a continuous numeric ID (such as order number, user ID) in a certain delimiter format, such as"123456"|join("-")Get"1-2-3-4-5-6".
  • cooperatesplitFilter: When you need to concatenate 'words' in a string (not characters), you can first usesplitThe filter splits the string into an array of words and then usesjoinThe filter connects. For example,{{ "Hello World"|split(" ")|join("_") }}You will get"Hello_World".

In short, the Anqi CMS'sjoinThe filter breaks a string into a character sequence for concatenation. Understanding this mechanism allows you to control the display of content more accurately in template development.


Frequently Asked Questions (FAQ)

1.joinCan the filter directly connect data of numeric type?Yes, it can be connected directly. WhenjoinWhen a filter encounters a number, it usually first implicitly converts the number to a string, then it treats each digit character of the string as an independent element and connects them using a specified separator. For example,{{ 12345|join("-") }}It will output.1-2-3-4-5.

How can I concatenate words in a string instead of characters?If your purpose is to concatenate the "words" (separated by spaces or other delimiters) in a string, rather than individual characters, you should usesplitThe filter splits a string into an array of words and then passes this array tojoinFilter. For example,{{ "安企 CMS 是一个系统"|split(" ")|join("_") }}You will get安企_CMS_是一个_系统.

3. WhenjoinWhat happens when the filter is applied to an empty string?WhenjoinThe filter is applied to an empty string, for example{{ ""|join("-") }}), it will return an empty string. This is because there are no character elements to concatenate in an empty string.