When managing and displaying content in AnQi CMS, we often encounter situations where we need to combine a set of related information into a coherent text.For example, a product may have multiple features, and we want to display the list of these features uniformly using commas or slashes.Or, an article is associated with multiple keywords and tags, and we want to summarize these tags into a single line of text.At this moment, the powerful template filter of AnQi CMS can be put into use, especiallyjoinThe filter can help us easily achieve this goal.
UnderstandingjoinFilter: Join array elements into a string
joinThe core function of the filter is to concatenate all elements of an array (Slice or array in Go language) with the specified delimiter to form a complete string.This is like you have a string of beads, then use a thread to string them together.
Its basic usage is very intuitive:
{{ 数组变量 | join:"您想要的分隔符" }}
For example, if you have an array representing colors in a template:
{% set colors = '["红色", "绿色", "蓝色"]'|list %}
If you want to connect them as
<p>可用颜色:{{ colors|join:" | " }}</p>
It will be displayed on the page:
可用颜色:红色 | 绿色 | 蓝色
Here|listIs an auxiliary filter used to define a string array directly in the template.In actual use, your array usually comes from the Anqi CMS backend data, such as custom fields or tag lists.
Flexible application: Handling multi-value custom fields
The strength of AnQi CMS lies in its flexible content model and custom fields.Many times, we may define a custom field for a document or product to store multiple related values, such as the "product features" field, which may contain a comma-separated string like "high performance, lightweight, safe and stable.
To convert this storage method's data into a more user-friendly display format, we need to first 'split' this string into an array and then 'join' it together. This is where we need to usesplita filter, which isjoinThe 'reverse operation':
splitFilter: Split a string into an arraysplitA filter can split a string into an array using a specified delimiter. Its usage is:{{ 字符串变量 | split:"您用来切割字符串的分隔符" }}Now, let's combine
splitandjoinHandle the 'product feature' field mentioned earlier. Suppose you are on the article detail page,archive.FeaturesThe value of the field is"高性能,轻量化,安全稳定".`