How to concatenate the elements of an array into a string separated by a specified delimiter for display?

Calendar 👁️ 77

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':

  1. splitFilter: Split a string into an array

    splitA filter can split a string into an array using a specified delimiter. Its usage is:

    {{ 字符串变量 | split:"您用来切割字符串的分隔符" }}
    

    Now, let's combinesplitandjoinHandle the 'product feature' field mentioned earlier. Suppose you are on the article detail page, archive.FeaturesThe value of the field is"高性能,轻量化,安全稳定".

    `

Related articles

How to convert timestamp data stored in the background into a readable date and time format for display?

When managing website content in Anqi CMS, we often encounter situations where we need to display dates and times.However, the time information stored in the background database is usually in the form of timestamps, which is very efficient for machine processing, but difficult for users to read directly.For example, you might see a string like `1609470335`, which represents a specific moment, but we want it to be displayed in a more friendly format like "2021 January 1 at 12:25:35".fortunately

2025-11-08

How to format user input plain text content (including newline characters) into HTML paragraphs and newline characters for display?

In website content management, we often encounter such needs: when users input a block of plain text content in the background, it often contains some natural paragraphs and line breaks. We hope that these paragraphs and line breaks are rendered correctly on the website front-end as HTML paragraph tags (`<p>`) and line break tags (`<br/>`), rather than being compressed into a blob or ignored by the browser.AnQi CMS provides a very convenient and powerful template filter to solve this problem, allowing plain text content to be presented elegantly on the web.

2025-11-08

How to extract a part of a long string or HTML content and display an ellipsis at the end?

In website content operation, we often encounter such situations: the article list page needs to display the summary of the content, the product detail page needs to show a brief introduction, or in some special modules, we need to extract a part of the long text or content containing HTML tags, and add "..." at the end to prompt the user that the content is not complete.This not only effectively saves page space and keeps the layout neat, but also improves the reading experience and attracts users to click to view the full content.To achieve such a function, if it were to be manually截取 each time, it would undoubtedly consume a lot of time and effort.Fortunately

2025-11-08

How to safely output content containing HTML tags in a template and prevent them from being escaped by the browser?

In website operation, we often need to display information with rich formatting and media content, such as carefully edited article details, product introductions, or customized pages.This content often contains HTML tags, such as image tags `<img>`, link tags `<a>`, paragraph tags `<p>` and so on.However, if you directly output this content to a web page, you may find that it does not render as expected, but rather displays the HTML tags as plain text, greatly affecting the user experience and the beauty of the page.

2025-11-08

How to perform basic arithmetic operations on numbers in templates and display the results?

In website operation, we often need to dynamically display some numerical information, such as total product price, percentage of article reading, discounted price, and so on.The template system provided by AnQi CMS is powerful, it adopts syntax similar to the Django template engine, allowing us to easily implement these requirements when displaying content, perform basic arithmetic operations directly in the template, and display the results.### Master basic arithmetic operations in templates The most direct way to write arithmetic expressions in Anqi CMS templates is within double curly braces `{{ }}`

2025-11-08

How to generate random placeholder text to simulate real content display during template development?

During the process of website template development, we often encounter a challenge: how to fill the page to check the layout, style, and responsiveness before the real content is ready?If the template is empty or only has a few lines of simple text, it is difficult to evaluate the design effect directly.This is a very useful technique for generating random placeholder text. AnQiCMS (AnQiCMS) is an efficient and customizable content management system that fully considers the needs of template developers at this stage.

2025-11-08

How to ensure that a captcha is displayed when users submit comments or messages to prevent robot abuse?

When operating a website, we often encounter bots submitting a large number of spam messages or comments, which not only affects the quality of the website content but also increases the management burden and may have a negative impact on the reputation of the website.Luckily, AnQiCMS provides an in-built captcha mechanism that can effectively help us prevent such abusive behaviors.Next, let's see how to add a captcha for user comments or reviews in AnQiCMS.

2025-11-08

How to retrieve and display specific content from a specified site in a multi-site management environment?

In today's changing network environment, many businesses and content operators are no longer satisfied with a single site.Scenarios where there are multiple brands, product lines, or the need for multilingual promotion are becoming increasingly common.AnQiCMS (AnQiCMS) is exactly under such needs, providing powerful multi-site management capabilities, allowing us to easily manage multiple content platforms with a single system.But having multi-site functionality is not enough. Often, we need to display specific content from one site on another site, to achieve resource sharing and content synergy.For example, the main site wants to display the latest products of a child site

2025-11-08