How to concatenate an array into a string with a specified delimiter for display in AnQiCMS?

Calendar 👁️ 64

The AnQiCMS handles data flexibly in templates, which is the key to improving the display effect of the website for content operators.Among them, displaying the array as a string concatenated with a specified delimiter is a common need to enhance the readability and aesthetics of the content.Benefiting from the powerful and flexible template engine of AnQiCMS, especially its built-in rich filter functions, we can easily achieve this goal.

Use AnQiCMS template filter skillfully: easily realize array concatenation and beautiful display

In AnQiCMS template design, we often encounter situations where we need to display a series of related data.For example, an article may have multiple keyword tags, a product may have multiple feature descriptions, or a custom field may store a set of image URLs.These data may be stored in an array or list format in the background, but when displayed on the front end, we usually want them to be presented in a more user-friendly string format separated by specific symbols (such as commas, slashes, bars, etc.), rather than the original array format, which not only improves the user experience but also helps maintain the neat layout of the page.

AnQiCMS's template engine adopts a tag marking method similar to Django, one of its powerful features is the 'filter' (Filters).The filter allows us to modify, format, or convert output variables, like a data processing tool.Today, let's delve into how to use these filters to cleverly concatenate the array into the string format we want.

Core tool:joinFilter - the Swiss Army knife of array concatenation

When we need to concatenate all elements of an array (usually referred to as a Slice type of data in AnQiCMS templates) with a specific string,joinThe filter is our first choice. Its function is just like its name, connecting the various parts of the array.

joinHow to use the filterVery intuitive:

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

Here:

  • 数组变量Is the array you want to concatenate (for example, a field value obtained from the background, if the field stores a list).
  • 分隔符is a string, you want to use it to connect each element of the array. It can be a comma (,), a space (), a slash (//), or a vertical bar (|) Even more complex strings, as long as you enclose them in double quotes.

For example:Suppose we have a custom field namedproduct_features, which stores a set of product features such as["防水", "防震", "耐磨"]. If output directly in the template{{ archive.product_features }}, it may result in an unreadable raw array format. But by usingjoina filter, it can be displayed beautifully:

{% archiveDetail productFeatures with name="product_features" %}
  产品特性:{{ productFeatures | join:"、" }}
{% endarchiveDetail %}

This code may be displayed on the page:产品特性:防水、防震、耐磨.

Even though your 'array' is actually a string, you want to concatenate each character of it,joinThe filter can also be useful. It splits a string into individual characters and then connects them with a specified delimiter. For example:

{{ "安企内容管理系统" | join:", " }}

This will output:安, 企, 内, 容, 管, 理, 系, 统Of course, this usage is relatively rare, but it may be useful under specific layout requirements.

splitFilter:joinpartner

In practice, the data we retrieve from the database may not be an array directly, but a string that has been separated by commas or specific symbols. For example, a custom field may storeSEO优化,内容营销,网站搭建Such text. At this time, if we need to further process these "pseudo-arrays" (such as splitting first, then rejoining with other symbols, or only getting a part of them),splitThe filter is particularly important.

splitThe role of the filter is tojoinExactly the opposite, it can split a string into an array according to the specified delimiter.

splitHow to use the filter:

{{ 字符串变量 | split: "分隔符" }}

For example:If we have a fieldkeywords_stringStored isSEO优化,内容营销,网站搭建and we want to display them in the form of/This form can be displayed by usingsplitconverted to an array, and thenjoinconcatenated:

{% archiveDetail keywordString with name="keywords_string" %}
  {% set keyword_array = keywordString | split:"," %}
  关键词:{{ keyword_array | join:" / " }}
{% endarchiveDetail %}

This will output:关键词:SEO优化 / 内容营销 / 网站搭建.

BysplitandjoinThe combination of use, you can flexibly handle various string and array data, ensuring that the content is presented in the most elegant and design-friendly way on the front end.

Quick view of other related filters

exceptjoinandsplitThis is a golden pair, AnQiCMS also provides many other useful filters that can assist you in achieving more functions when dealing with arrays or strings:

  • firstandlastFilter:When you only want to display the first or last element of an array, these filters are very convenient.{{ 数组变量 | first }}Or `{{ array variable | last }}

Related articles

How to determine if a variable exists or is empty in a template and display different default content based on the result?

During the development and maintenance of website templates, we often encounter situations where variables may not exist, have empty values, or do not meet expectations.If these variables are not properly handled, it may lead to page display anomalies, or even cause errors, affecting user experience.AnQiCMS (AnQiCMS) provides a powerful and flexible template engine based on Django syntax, allowing us to easily determine the state of variables and display different content accordingly.

2025-11-08

How AnQiCMS affects the display effect of the website under different template modes (adaptive, code adaptation, PC + mobile)?

When building and operating a website, we often consider how to make the website display beautifully on different devices.AnQiCMS (AnQiCMS) fully understands this point and therefore provides three flexible template modes to meet different needs: adaptive, code adaptation, and PC + mobile independent site mode.Each pattern has its unique implementation method and impact on the display effect of the website, understanding them can help us better choose the solution suitable for our project.### One, Adaptive Mode: One template, multiple screens Adaptive mode, as the name suggests

2025-11-08

How does AnQiCMS set the 'default thumbnail' to unify the display effect when images are missing?

In website content operation, images play a vital role. They can attract users' attention, enhance the visual appeal of the content, and help users quickly understand the theme of the content.However, in the process of daily content publishing, we sometimes encounter situations where some articles fail to upload exclusive thumbnails for various reasons, or the content itself does not have suitable images to extract.If the front-end page directly displays blank, broken image icons, or disordered layout, it will undoubtedly severely affect user experience and the professionalism of the website.To solve this problem, AnQiCMS

2025-11-08

How to dynamically retrieve and display the contact information of a website, such as phone number, email, and social media links?

The contact information of the website, such as phone number, email, and social media links, is an important bridge for users to connect with us.It is crucial that the accuracy and accessibility of this information is maintained in the operation of the website.AnQiCMS as an efficient content management system provides a very convenient way to manage and dynamically display this contact information, allowing us to easily maintain the consistency of website content and quickly update it when needed.This article will delve into how to set contact information in AnQiCMS, as well as how to dynamically retrieve it in templates and display it in a user-friendly manner.### One

2025-11-08

How to split a string into an array with a specified delimiter and iterate through it in AnQiCMS?

In website operation, we often encounter the need to store some seemingly simple, but actually containing multiple pieces of information data in an article, a product description, or some custom field.For example, multiple tags of a blog, multiple features list of a product, or some dynamic display related words.These data are usually connected into a long string with a specific delimiter (such as a comma, semicolon, or pipe).

2025-11-08

How to display the current year or a custom time format in AnQiCMS templates?

In website operation, the accuracy and display of time information has a significant impact on user experience and content professionalism.Whether it is the copyright year in the footer or the publishing or updating time of the article content, a clear and consistent date format can enhance the overall perception of the website.AnQiCMS provides very flexible and powerful features in templates to meet these time display requirements.### Show the current year: Use the `{% now %}` tag to quickly display Many website footers display the current year as part of the copyright statement, for example “© 2023

2025-11-08

How to ensure that the custom system parameters set in the AnQiCMS backend can be correctly displayed in the frontend template?

The flexible application of custom system parameters in Anqi CMS is the key to improving website maintainability and operational efficiency.Many times, we may need some information that is not covered by the default system fields, such as specific external links, additional corporate honor information, or exclusive copywriting for a specific event.AnQi CMS provides a convenient way to add these custom parameters and ensures they are presented accurately in the front-end template.### One, understand the setting location of custom system parameters Firstly, we need to clarify the setting entry of custom system parameters in the Anqi CMS background

2025-11-08

How to customize structured data with Json-LD tags in AnQiCMS to optimize the display of search results?

AnQiCMS: Custom JSON-LD structured data, the secret weapon to light up search results In today's fiercely competitive information ocean, making our website content better understood and displayed by search engines is the key to improving online visibility.Structured data, especially JSON-LD, is playing an important role.It can help search engines accurately interpret page information, thereby presenting it in a more rich and attractive form in search results, which is what we commonly refer to as "Rich Snippets".AnQiCMS knows this

2025-11-08