How to use `split` and `join` filters to standardize user tag inputs?

Calendar 👁️ 75

In the daily content operation of AnQi CMS, the tags (Tag) submitted by users often face a common problem: inconsistent formatting.Some users are accustomed to separating with commas, some use semicolons, and even Chinese commas or spaces directly.These irregular inputs, if directly displayed on the website front-end, not only affect the aesthetics, but may also reduce the usability of the tags, for example, causing trouble when generating tag clouds or performing SEO optimization.

Fortunately, the powerful template engine of Anqi CMS providessplitandjointhese very practical filters, which can help us efficiently standardize the tags entered by users.

Label input 'standardization' challenge

Imagine, when users input labels in the background, they may encounter the following situations:

  • SEO, CMS, AnQiCMS(English comma with space)
  • SEO;CMS;AnQiCMS(English semicolon)
  • SEO,CMS,AnQiCMS(Chinese comma)
  • SEO CMS AnQiCMS(Space)
  • SEO , CMS ; AnQiCMS(Mixed separators with extra spaces)

Our goal is to process any user input into a unified format, such as a clean tag array, or a comma-separated string in English with no extra spaces, ensuring each tag is independent and a valid word without any extra spaces.

splitFilter: Split strings into arrays

splitThe filter, as the name implies, is to split a string into an array (or slice) according to a specified delimiter. Its basic usage is:{{ 变量 | split:"分隔符" }}.

For example, if we have a stringrawTags = "SEO,CMS,AnQiCMS"We can use it like thissplitFilter:

{% set rawTags = "SEO,CMS,AnQiCMS" %}
{% set tagArray = rawTags|split:"," %}
{# tagArray 现在是一个包含 ["SEO", "CMS", "AnQiCMS"] 的数组 #}

If the user is using semicolons to separate, we just need to change the separator to semicolon:

{% set rawTags = "SEO;CMS;AnQiCMS" %}
{% set tagArray = rawTags|split:";" %}
{# tagArray 现在是一个包含 ["SEO", "CMS", "AnQiCMS"] 的数组 #}

But when the user's input becomes complex, such asSEO, CMS; AnQiCMS, 网站优化In cases where multiple separators and extra spaces are mixed, using a single separator may not be able to fully decompose. In this case, we can combinereplaceFilter, first replace all irregular delimiters with one, and then proceedsplitOperation.

For example, we can first replace semicolons and Chinese commas with English commas:

{% set rawTags = "SEO, CMS; AnQiCMS, 网站优化" %}
{% set tempTags = rawTags|replace:";","," %} {# 将分号替换为逗号 #}
{% set tempTags = tempTags|replace:",","," %} {# 将中文逗号替换为逗号 #}
{% set tagArray = tempTags|split:"," %}
{# tagArray 此时可能包含 ["SEO", " CMS", " AnQiCMS", " 网站优化"],注意其中的空格 #}

By this step, we have unified all tags with English commas separated, although some extra spaces may still remain, but it has laid a foundation for the next step of processing.

joinFilter: Join array elements into a string

jointhe filter meetssplitThe function does the opposite, it takes an array and joins all the elements of the array into a single string with a specified separator. Its basic usage is:{{ 数组 | join:"分隔符" }}.

For example, if we have a label arraytagArray = ["SEO", "CMS", "AnQiCMS"]We can use it like thisjoinFilter:

{% set tagArray = ["SEO", "CMS", "AnQiCMS"] %}
{% set tagString = tagArray|join:", " %}
{# tagString 现在是 "SEO, CMS, AnQiCMS" #}

Combine usesplitandjoinStandardize labels

Now, let's combine these two filters to implement standardized processing of user tags.Our goal is to display the tags in a clean and uniform comma-separated format, or as individual tag links, regardless of the format of the tags entered by the user.

Suppose the user fills in the tag input box in the background:搜索引擎优化,CMS ;AnQiCMS ,Go语言

{% set userInputTags = "搜索引擎优化,CMS ;AnQiCMS ,Go语言" %}

{# 第一步:标准化分隔符。将分号和中文逗号替换为英文逗号 #}
{% set standardizedDelimiterTags = userInputTags|replace:";","," %}
{% set standardizedDelimiterTags = standardizedDelimiterTags|replace:",","," %}

{# 第二步:使用 split 过滤器将字符串拆分为数组。 #}
{% set tagArray = standardizedDelimiterTags|split:"," %}

{# 第三步:遍历数组,对每个标签进行清理(去除首尾空格),并输出。
   这一步可以直接用于生成独立的标签链接,或者为下一步的 join 做准备。 #}
<p>标准化后的标签:</p>
<ul>
{% for tagItem in tagArray %}
    {% set cleanTag = tagItem|trim %} {# 使用 trim 过滤器去除标签前后的空格 #}
    {% if cleanTag != "" %} {# 确保不是空标签 #}
        <li><a href="/tags/{{ cleanTag|urlencode }}" rel="tag">{{ cleanTag }}</a></li>
    {% endif %}
{% endfor %}
</ul>

{# 第四步(可选):如果需要一个统一的逗号分隔字符串来展示,可以使用 join 过滤器 #}
{% set finalDisplayTags = [] %}
{% for tagItem in tagArray %}
    {% set cleanTag = tagItem|trim %}
    {% if cleanTag != "" %}
        {% set finalDisplayTags = finalDisplayTags|add:cleanTag %} {# 将清理后的标签添加到新数组 #}
    {% endif %}
{% endfor %}
<p>统一格式的标签字符串:{{ finalDisplayTags|join:", " }}</p>

In the above example, we first go throughreplaceThe filter unified the delimiter, ensuring that all tags are separated by English commas. Next,splitThe filter splits this string into an initial array. While traversing this array, we used a filter to process each label item.trimThe filter is used to remove excessive leading and trailing spaces, and thenif cleanTag != ""Judge and filter out empty tags caused by consecutive delimiters.

Finally, we demonstrated two common uses:

  1. Generate a list of independent tag links: Directly inforGenerate for each cleaned tag in the loop<a>Link, convenient for users to click and browse related content.
  2. Generate a label string in a unified format.If you need to display a row of neatly arranged tags at a certain location (such as the bottom of an article page), you can collect the cleaned tags into a new array and then use themjoinThe filter is connected in a unified format (for example,).

By flexible applicationsplitandjoinThese are two simple and powerful filters, you can easily handle the label data entered by users in the AnQi CMS, making it unified, tidy and easy

Related articles

How to combine a dynamically generated array (such as a tag list) into a string with a specified delimiter for display in a template?

In website content operation, we often need to display a series of related information in the form of a list to users, such as all tags of an article, multiple characteristics of a product, or a set of image URLs.AnQiCMS's template system usually provides these data in the form of a dynamic array (or sometimes called slices, lists).

2025-11-07

What are the main differences and application scenarios between the `make_list` filter and the `split` filter when splitting strings into arrays?

In AnQi CMS template development, it is often encountered that a string needs to be split into multiple parts for further processing or dynamic display.To meet this requirement, AnQiCMS template engine provides two very useful filters, `make_list` and `split`.Although they can both convert strings to arrays, there are obvious differences in their core functions, splitting logic, and applicable scenarios in actual use.Understanding these differences can help us handle content more efficiently and accurately.

2025-11-07

What are the different splitting behaviors when the delimiter is empty or not present?

In AnQi CMS template development, the `split` filter is a very practical tool that can help us split strings into arrays according to the specified delimiter, which is very convenient when dealing with list data, tag content, or any structured text.However, when the delimiter is an empty string or does not exist in the target string, the behavior of the `split` filter shows some clever characteristics, understanding these characteristics is crucial for precise template control and avoiding potential errors.

2025-11-07

How to split a comma-separated keyword string configured in the AnQiCMS backend into an iterable keyword list?

In AnQiCMS (AnQiCMS) daily content operations, we often need to display related keywords on articles or product detail pages.These keywords not only help search engines understand the content of the page, but also guide users to discover more relevant information.In most cases, we will enter these keywords in a text box on the backend, separated by commas, for example: "website operation, SEO optimization, content marketing.

2025-11-07

How to truncate the title of the article and automatically add an ellipsis in the AnQiCMS template?

In AnQi CMS template design, in order to ensure the beauty of the page and the uniformity of the layout, we often need to truncate the article title and automatically add an ellipsis after truncation.AnQiCMS provides a simple and efficient template filter to meet this requirement, making content display more flexible.

2025-11-07

What is the essential difference between the `truncatechars` and `truncatewords` filters, and how should one choose?

In AnQi CMS template development, in order to better display content summaries or control page layouts, we often need to truncate text.At this moment, the `truncatechars` and `truncatewords` filters come into play.They can all help us shorten long texts and add an ellipsis at the end, but there is an essential difference in their truncation logic. Understanding these differences is crucial for choosing and using them correctly.

2025-11-07

How to safely truncate text to a specified length for articles containing HTML tags using `truncatechars_html`?

When using AnQiCMS to manage website content, we often encounter such a scenario: to make the article content more beautiful in layout, various HTML tags are used to enrich the visual effects, such as paragraphs (`<p>`), images (`<img>`), links (`<a>`), bold (`<strong>`), and so on.However, on the article list page or in the related recommendation module, we often need to display the abstracts or parts of these articles, but we cannot directly display the entire long discourse. At this time

2025-11-07

Can the `truncatewords_html` filter correctly handle nested HTML tags while truncating, preventing page layout chaos?

In website operations, we often need to summarize long content to attract users to click and view the details.However, directly truncating rich text content containing HTML tags often leads to page structure chaos, for example, a `<p><b>important information</p>` truncated to `<p><b>important` such a missing tag, not only ruins the visual beauty of the page, but may also lead to the entire page layout disorder.This is undoubtedly one of the most headache issues for content operators. AnQi CMS understands this pain point.

2025-11-07