In the template development of Anqi CMS, the flexibility of data display is crucial.We often need to present data lists obtained from the backend in a beautiful and consistent way on the front end, which includes the need to concatenate elements of an array (or list) into a string.joinA filter that not only handles common arrays containing multiple elements but also exhibits unique and practical behavior in special cases such as empty arrays or arrays containing only a single element.
joinThe filter is a very practical tool in the Anqi CMS template, which mainly serves to receive an array or list, and then concatenate all elements in the array into a new string using the delimiter you specify. For example, if you have a list containing["安企CMS", "内容管理", "高效"]The array, using commas and spaces as separators,joinThe filter will convert it to,"安企CMS, 内容管理, 高效"Such a string. This method is very convenient when displaying article tags, category lists, or any content that needs to be displayed in a tiled manner.
Then, when there is only a single element in the array,joinHow will the filter handle it?The answer is: it will directly return the string representation of the element without adding any separators.This may seem trivial, but it greatly simplifies the logic in actual development."SEO优化"UsejoinApply a filter and specify comma as the delimiter, what you expect to get is"SEO优化"Instead"SEO优化,".joinThe filter works just like this. It understands that in this case, there are no other elements to connect, so there is no need to introduce a separator, ensuring the output is concise and accurate.
Perhaps a more common scenario might be handling an array that may be empty.For example, a newly published article may not have been assigned any tags yet.joinFilter, the security CMS template engine will return an empty string.This means that your page will not display any extra separators and will not produce errors or blank placeholders due to attempting to connect non-existent elements.joinFilter, making template code more concise and maintainable.
For example, in the AnQi CMS template, we may usearchive.Tagsto get the list of article tags. Thisarchive.TagsA possibly multiple tags array, an array of tags, or an empty array. We can use it like this:jointo filter and display them:
{% set articleTags = archive.Tags %} {# 假设 article.Tags 是一个数组,例如 ["网站优化", "内容营销"] #}
<div>
标签:
{{ articleTags|join(", ") }}
</div>
IfarticleTagscontaining multiple elements, such as["网站优化", "内容营销"]The output will be"标签:网站优化, 内容营销".
IfarticleTagsit contains only a single element, such as["SEO"]The output will be"标签:SEO".
IfarticleTagsis an empty array, the output will be"标签:"—very clean, with no unnecessary separators.
This intelligent processing mechanism makesjoinThe filter has become a powerful tool in the development of Anqi CMS templates, reducing the complexity of conditional judgments, making your template code more elegant. By understandingjoinHow can a filter handle arrays in different states, allowing us to build dynamic content displays more confidently and efficiently, ensuring consistent user experience.
Common Questions and Answers (FAQ)
1.joinCan the filter be used for non-array data types?
joinThe filter is primarily designed for array (or list) type data.If applied to a single string, it will try to concatenate each character of the string as a separate element."你好"|join("-")It will output"你-好". But if a number or boolean is passed, it may cause unexpected behavior or errors. Therefore, it is recommended to usejoinBefore, ensure the data type is an array or a string.
2. How to not display any text (including the prefix "Label: ") when the array is empty?If you want to hide the prefix text (such as "Label: ") along with the array when it is empty, you can combine use ofifcondition judgment andjoinfilter. For example:
{% set articleTags = archive.Tags %}
{% if articleTags %}
<div>
标签:
{{ articleTags|join(", ") }}
</div>
{% endif %}
In this way, only whenarticleTagsNot empty, the whole<div>block will be displayed.
3.joinFilter is related tosplitWhat is the relationship between filters?
joinfilters andsplitFilters can be considered as inverse operations.joinJoin array elements into a string, andsplitThen split a string into an array using a specified delimiter.If you need to split a string into an array for processing and then reconnect them, they would be a great pair.