In Anqi CMS, with the increasing trend of website globalization operation, we often encounter situations where we need to handle multilingual content.Among them, document tag management is a typical example.splitThe application of the filter in the template is particularly important. It helps us convert label data stored as strings into traversable lists. However, in the context of cross-language content,splitThe handling of delimiters by the filter is not always intuitive, which requires our special attention.

UnderstandingsplitThe core function of the filter

In the AnQi CMS template system,splitThe filter is a very practical tool, its main function is to split a string into an array (or list) according to a specified delimiter.For example, when we add multiple tags to an article in the background, these tags are usually connected into a string with commas, semicolons, or other specific symbols and stored in the database.splitit can be put to use.

The basic usage is{{ 变量名|split:"分隔符" }}. For example, if there is a variableitem.Tagshas a value of"SEO,内容运营,用户体验", using{{ item.Tags|split:"," }}it can get a containing["SEO", "内容运营", "用户体验"]array.

splitThe filter has several characteristics to be mindful of when handling delimiters:

  • Literal matchIt will strictly match the literal delimiter string you provide. This means that if the delimiter you specify is", "(a comma followed by a space), then it will not match a string that only contains a comma",".
  • The delimiter does not exist: If the string does not contain the specified delimiter,splitThe filter will return a single-element array containing the original string.
  • The delimiter is empty: If you accidentally set the delimiter to an empty string (for example|split:""The filter will split the string at each UTF-8 character, including Chinese characters, with each character being treated as a separate character for splitting.

Challenge of separators in cross-language content

In multi-language websites, the separators of tags are often not just simple English commas. You may encounter the following situations:

  1. Pure English contentFor example, the label may be"SEO,Content Marketing,User Experience"In this case, use English comma","Or comma followed by a space", "As a separator is usually fine

  2. Pure Chinese content: In Chinese environment, labels usually use Chinese commas",“or full stops"、"For example,"搜索引擎优化,内容运营,用户体验". At this point, if you still use English commas","to split,splitThe filter cannot recognize it, treating the entire string as a tag. Conversely, if you want to cut it correctly, you need to specify the Chinese comma explicitly.",“as a separator.

  3. Mixed language or irregular inputThis is the most common and easiest to make a mistake scenario. Imagine that a label string might be"SEO, 多语言,CMS, AnQiCMS"There are English commas, Chinese commas, and even other separators such as semicolons inside";". BecausesplitThe filter is a literal match and cannot intelligently recognize all possible delimiters. If you only use English commas to split, the parts connected by Chinese commas cannot be separated; conversely, as well.

Strategies and Practices for Response

In order to efficiently and accurately handle tag separators in the multilingual environment of AnQi CMS, we have some practical strategies that can be adopted:

1. Standardize backend content input (strongly recommended)

The most fundamental and most effective solution is to standardize the delimiter of tags in the content management background. We suggest that content editors use a single one regardless of the language of the tags they enter.Common and standardized English separatorFor example, English comma",".

For example,

  • English label:"SEO, Content, Marketing"
  • Chinese label:"搜索引擎优化, 内容运营, 用户体验"
  • Mixed label:"SEO, 搜索引擎优化, AnQiCMS"

The benefit of doing this is that the front-end template can use the samesplit:","filter to greatly simplify the template logic, reducing the likelihood of errors.

2. Multi-stepsplitorreplaceCombined processing

If for various reasons, the label separator input from the background is difficult to unify completely, then you can take a multi-step processing approach in the template. This usually involves first replacing all non-standard separators with a standard separator, and then performing the finalsplitOperation.

Assuming you know that labels may use English commas,, Chinese commaOr full stopAs a separator, you can handle it like this:

{% set raw_tags = archive.Tags %} {# 假设 archive.Tags 的值是 "SEO, 多语言,CMS、Go语言" #}

{# 1. 将所有中文逗号替换为英文逗号 #}
{% set standardized_tags_step1 = raw_tags|replace:',',',' %} {# 结果: "SEO, 多语言,CMS、Go语言" #}

{# 2. 将所有顿号替换为英文逗号 #}
{% set standardized_tags_step2 = standardized_tags_step1|replace:'、',',' %} {# 结果: "SEO, 多语言,CMS,Go语言" #}

{# 3. 最后使用英文逗号进行分割 #}
{% set tag_list = standardized_tags_step2|split:',' %}

{# 现在您可以遍历 tag_list 来显示每个标签了 #}
<ul>
{% for tag in tag_list %}
    <li>{{ tag|trim }}</li> {# |trim 过滤器可以去除标签首尾可能多余的空格 #}
{% endfor %}
</ul>

This method may increase the complexity of the template code, but it is very effective in handling irregular data input. You can also add more based on the types of delimiters that may actually appear.replaceStep.

3. Use.trimFilter spaces.

Whether it is any kind of delimiter, the user may inadvertently leave spaces before or after the tag when entering (for example" 标签1, 标签2 ")splitThe filter keeps these spaces at the beginning or end of each tag element after slicing. Therefore, when traversing the tag list, it is usually combined with|trimThe filter is used to clean up these extra spaces to ensure the label is displayed neatly.

{{ tag|trim }}Can remove all leading and trailing spaces, newlines, or other characters you specify, making your tags look more standardized.

Summary

Manage multilingual content in Anqi CMS, especially when dealing with data that needs to be listed, such as tags.splitThe filter is an indispensable tool. Understand its literal matching characteristics and combinereplaceandtrimWith other filters, we can flexibly deal with various delimiters. However, the simplest and most efficient way is stillUnify delimiter specifications at the content input stageThis not only simplifies template development, but also improves the efficiency and data quality of content management.


Frequently Asked Questions (FAQ)

Q1:splitCan the filter automatically identify commas or semicolons in different languages and then split them?

A1:No.splitThe filter performs literal matching and strictly adheres to the characters you specify in the filter parameters (such as","or",“) to identify the delimiter.It does not have the ability to intelligently recognize the concept of 'comma' or 'semicolon' in different language environments.replaceThe filter first unifies them, and then performssplitOperation.

**Q2: If my tag string contains both English commas and Chinese commas, how do IsplitFilter correct