How to concatenate array elements in an Anqi CMS template with a custom character into a string?

Calendar 👁️ 62

In Anqi CMS template design, we often encounter situations where we need to display list data, such as multiple tags of an article, multiple image links on a product details page, or multiple options stored in a custom field. Directly outputting this array-like data to the page will often result in something similar to[“标签一”, “标签二”, “标签三”]This original format is clearly not aesthetic and does not conform to user reading habits.

At this point, we need a method to concatenate the elements of an array using a custom character (such as a comma, hyphen, or space) to form a natural and smooth string. The powerful template engine of Anqi CMS provides a "filter" function, which includes thejoinThe filter is the tool to solve this problem.

The core feature revelation:joinFilter

joinThe filter is a very practical feature in the Anqi CMS template, its function is to concatenate all elements of an array (or iterable object) with the delimiter you specify, forming a single string.In this way, the originally scattered array elements can be presented in a neat and readable manner on the page.

Its basic usage syntax is very intuitive:

{{ 数组变量|join:"分隔符" }}

Among them,数组变量It is the array you want to operate (for example["元素A", "元素B", "元素C"])分隔符It is the character or string you want to use to connect elements, for example","/" - "/" / "etc.

Next, we will learn in detail through several common practical application scenariosjoinThe specific usage of the filter.

Practical scenario example: connecting arrays from different sources into a string

In Anqi CMS template design, the source of array data is various. Below, we select several typical cases for demonstration.

Example one: Connect the Tag tag of the document.

Assuming your article has a "Tag label" field that may be stored in the background as a comma-separated string, but you want to display it on the front end as "LabelA - LabelB - LabelC" format. Althougharchive.KeywordsThe field is usually a string, but if your template can obtain an array of label names through some method (such as a custom function or a more complex model processing), or if you need to convert an existing comma-separated string into an array and then concatenate it,splitThe filter will bejoinyour partner.

For example,archive.KeywordsIt is a comma-separated string: "SEO, content marketing, website operation", do you want to connect it with a hyphen:

{% set keywords_array = archive.Keywords|split:"," %}
{{ keywords_array|join:" - " }}

The steps here are:

  1. archive.Keywords|split:",": First usesplitThe filter willarchive.KeywordsSplit by comma,Split into a string array.
  2. |join:" - ": Then use the elements of this array to-and connect them.

The output may be:SEO - 内容营销 - 网站运营

Example two: Display the image link of multi-image fields

In the document model, if a "cover group image" field is setImagesIt can often be used directly as an array of image URLs in the template.You may need to concatenate the URLs of all the images for debugging or other specific display requirements.

{% archiveDetail images_array with name="Images" %}
<p>所有图片链接:{{ images_array|join:"|" }}</p>

here,archiveDetailTags retrieved from comments.images_arrayThe variable is already an array of image URLs, which we can use directlyjoinFilter it with a vertical line|connect.

The output may be:<p>所有图片链接:/uploads/img1.jpg|/uploads/img2.jpg|/uploads/img3.jpg</p>

Example three: Connecting arrays in custom fields

If your content model has a custom field, for examplefeaturesIt may be a multi-select box, storing multiple featured options on the back-end. If these options are exposed to the template as an array, you can directly connect them.

{% archiveParams params %}
{% for item in params %}
    {% if item.FieldName == "features" %}
        <p>产品特色:{{ item.Value|join:" / " }}</p>
    {% endif %}
{% endfor %}
{% endarchiveParams %}

Assumeitem.ValueAt this moment is a like["防水", "防震", "耐磨"]An array, the output may be:<p>产品特色:防水 / 防震 / 耐磨</p>

Example four: Manually create an array and connect

In the template, sometimes we also need to manually create a temporary array and then perform a connection operation.listThe filter helps us quickly define an array in the template.

`twig {% set my_data

Related articles

How does the `split` filter split a string into an array of characters?

When using AnQiCMS for website content creation and template development, string processing is a common requirement.Sometimes, we need to split a complete string into smaller parts, such as breaking it down into an array of characters.This article will delve into the powerful `split` filter feature in AnQiCMS and provide practical examples.

2025-11-08

How to split a string into an array using a specific delimiter in Anqi CMS template?

In the template development of AnQi CMS, we often encounter situations where we need to process specific strings.For example, a keyword of an article may be stored as a comma-separated string, and we want to display them as separate tags on the front end.At this point, splitting a string into an array with a specific delimiter becomes the key step in handling such issues.The AnQi CMS template engine supports syntax similar to Django, and it provides a rich set of filters (filters) to help us process data.

2025-11-08

How would AnQi CMS handle if the string format is incorrect when using the `list` filter to define an array?

In website content operation, we often need to display various list data in templates, such as a set of keywords, product features, or navigation links.AnQiCMS provides a powerful template engine, where the `list` filter is a very practical feature, allowing us to directly convert string-formatted data into an iterable array object in the template, greatly enhancing the flexibility of the template.

2025-11-08

How to iterate over an array defined in an AnQi CMS template using the `list` filter?

In Anqi CMS template design, we often encounter situations where we need to define and iterate over some fixed or small array data.Although most dynamic content is retrieved through built-in tags such as `archiveList`, `categoryList`, etc., it is more convenient to directly process arrays in the template for some localization configurations, custom options, or static lists.AnQi CMS provides the `list` filter, combined with the `for` loop tag, it can easily meet this requirement.

2025-11-08

How does the `join` filter behave when processing non-array objects (such as strings)?

In Anqi CMS template development, the `join` 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 apply the `join` filter to non-array objects, especially strings, its behavior may be different from what we initially imagine, but understanding its working principle can help us use it more flexibly.### The basic functionality of `join` filter review First, let's review the most common uses of the `join` filter

2025-11-08

How to extract elements within a specified range from an array or string in Anqi CMS template?

In AnQiCMS template development, we often need to finely control the displayed content, such as extracting part of the information from a long text, or displaying only the first few elements from a list.Fortunately, AnQiCMS uses a Django-like template engine that provides a very useful tool - the `slice` (slicing) filter, which can help us easily meet these needs.The `slice` filter is a very flexible feature that is not only applicable to string (text) but also to array (list) data

2025-11-08

How does the `slice` filter implement the function of extracting elements from the end of an array?

In AnQiCMS template development, the `slice` filter is a very practical tool that allows us to flexibly extract a part of the string or array content.But do you know? It can also realize a particularly clever function - to extract elements from the end of an array or list, which is very convenient when it comes to displaying the latest data or removing old data.Today, let's delve into how the `slice` filter achieves this functionality through negative indices.Understanding the basic usage of the `slice` filter As the name implies

2025-11-08

How to determine whether an array or string in an Anqicms template contains a specific keyword?

In AnQi CMS template development, we often need to dynamically display information based on specific attributes or keywords of the content.For example, determine whether an article title contains a certain product name, or check whether a document's tag list contains a hot keyword.This can not only make the website content more targeted, but also improve user experience and SEO effect.The AnQi CMS template engine provides powerful and flexible features to meet these needs.Among them, it is easy to determine whether an array or string contains a specific keyword by using the built-in `contain` filter.

2025-11-08