In AnQi CMS template design, we often encounter the need to integrate a series of data items into a coherent text.For example, we need to display multiple tags (Tag) in one place, or combine a set of custom parameter values obtained from the database. At this point,joinThe filter comes into play, it can efficiently concatenate the elements of an array into a string with the specified separator.
UnderstandjoinThe role of the filter
joinThe filter is a very practical feature provided by Anqi CMS template engine, whose core function is to connect. Imagine you have a string of beads (elements in the array), andjoinThe filter is that line, you can choose what kind of line (delimiter) to string these beads together, ultimately forming a complete necklace (concatenated string).This greatly simplifies the complexity of handling list data display in templates, avoiding the繁琐work of manually looping and judging the last element to remove extra separators.
joinHow does the filter work?
joinThe syntax of the filter is very intuitive:{{ 变量名 | join:"分隔符" }}.
Here变量名It is usually an array (or sometimes a string), while分隔符is any string you want to use to connect array elements or characters, like a comma (,Pipes symbol ()|Spaces () and even more complex HTML fragments.
Let's understand its basic usage with a simple example. Suppose you have a namedmyTagsAn array that includes several keywords:
{% set myTags = ["安企CMS", "内容管理", "高效部署"] %}
If you want to connect these keywords with commas and spaces, you can do it like this:
<p>文章关键词:{{ myTags | join:", " }}</p>
The output of this code will be:文章关键词:安企CMS, 内容管理, 高效部署
As you can see,joinThe filter automatically handled the connection between elements and did not add a separator after the last element, ensuring a tidy output.
Example of practical application scenarios
In AnQi CMS,joinThe filter can be used with various tags to process data obtained from the backend.
1. Concatenate article tags (Tags)
Suppose you want to display all related tags at the bottom of an article detail page, using a pipe|separated. AlthoughtagListThe label returns an object array, but we can first extract the titles of these objects, or more simply, if your data structure allows, we can directly apply to a string arrayjoin.
If we receive a comma-separated keyword string from a custom field and want to display it in another format,splitandjoinwe can use:
{% set articleKeywords = archive.Keywords|split:"," %} {# 假设 archive.Keywords 是 "SEO,网站优化,内容营销" #}
<p>文章重点:{{ articleKeywords | join:" | " }}</p>
This will convertarchive.KeywordsFrom"SEO,网站优化,内容营销"to convert it into an array, then use" | "Concatenate as a separator and output as:文章重点:SEO | 网站优化 | 内容营销
2. Handle custom list parameters
In AnQi CMS, you can define various custom fields for content models. If the value of a custom field itself is a list (for example, administrators enter multiple hobbies in the background, separated by newline characters), you can also usejoinDisplay in a unified format:
{% archiveDetail myInterests with name="interests" %} {# 假设 interests 字段值为 "阅读\n旅行\n编程" #}
<p>我的爱好:{{ myInterests | split:"\n" | join:" / " }}</p>
First, we usesplit:"\n"Convert a string separated by newline characters into an array, and then usejoin:" / "Concatenate it into阅读 / 旅行 / 编程.
joinFilter considerations
- Handle data types:
joinThe filter is best suited for processing arrays containing basic data types such as strings or numbers. When dealing with arrays of objects, you may need to first figure out a way to extract specific properties from the objects to form an array of basic data types, and then proceed tojoinOperate. In AnQi CMS template engine, using object arrays directlyjoinMay not get the result you want, because the template engine does not automatically know which properties to concatenate. - The special behavior of a string:It is interesting that if you use a filter on a normal string
joinIt will treat the string as a sequence of characters (such as"安企CMS"It will be considered'安'、'企'、'C'、'M'、'S'),then use the specified separator to concatenate each character. For example,{{"安企CMS"|join:", "}}It will output.安, 企, C, M, S. This may have an effect in some character processing scenarios, but it is usually something to be aware of when concatenating arrays. - Choose the appropriate delimiter:The choice of separator should take into account the final display effect and readability. In the context of HTML, you can even use HTML tags as separators, such as
{{ myItems | join:"<br>" }}Insert a newline between each element.
By flexible applicationjoinThe filter allows us to make the content display of the Anqi CMS website more dynamic and beautiful, and also greatly improves the efficiency of template writing.
Frequently Asked Questions (FAQ)
1.joinCan the filter directly concatenate a property of an array containing objects (such as a label object array)?
Anqi CMS template engine'sjoinThe filter is mainly used to concatenate arrays containing basic data types (such as strings, numbers). If your array contains objects (such astagListThe tag returns a tag object, each element is a containingTitle/Linkobject with properties such as"),joinThe filter cannot directly recognize and concatenate the specific attribute of an object (such asTitleIn this case, you usually need toforloop through the array, manually output the required properties of each object, and connect them with a separator, or usesetLabel and advanced techniques to build a new array that only contains the required strings and then proceedjoinOperation.
2.joinFilters andsplitWhat does the filter have to do with it?
joinandsplitThe filter is a complementary operation and can be considered as 'inverse'.joinResponsible for concatenating the elements of an array into a string with a specified separator, andsplitIt is the opposite, it splits a string into an array using a specified delimiter.They are very useful in handling the conversion between strings and arrays, and can facilitate data formatting and reorganization.For example, you can usesplitConvert a comma-separated string obtained from the background into an array, and then usejoinReassemble it with other delimiters.
3. If I try to usejoinWhat will happen if you concatenate a plain string instead of an array?
According to Anqi CMS template engine'sjoinFilter feature, if you use a regular stringjoinA filter that treats the string as a sequence of characters (rune slice) and tries to concatenate each character of the string with the delimiter you specify. For example,{{"欢迎使用安企CMS"|join:", "}}It will output.欢, 迎, 使, 用, 安, 企, C, M, SThis is different from concatenating an array of strings, and it should be noted when used.