In the template development process of AnQi CMS, we often encounter situations where we need to process strings, such as converting a segment of text separated by a specific symbol into a list, or concatenating multiple items in a list into a continuous text.The template engine of Anqi CMS provides powerful filters (Filters) to help us easily achieve these operations, greatly enhancing the flexibility of the template.

Anqi CMS Template Engine Basics

The template engine syntax of Anqi CMS is very user-friendly, similar to Django template engine. It mainly uses double curly braces{{变量}}to output variable content, and through{% 标签 %}Structure to control the logic flow, such as conditional judgments and loops. When dealing with strings and arrays, we often use the pipe character|to use various built-in filters.

Splitting strings into arrays: flexiblesplitFilter

Imagine that your website's backend has a custom field to store multiple keywords for articles, separated by commas,Separate, for example,<li>)Present. At this time,splitThe filter comes into play.

splitThe filter can split a string into an array of strings (or a list) according to the 'delimiter' you specify. Its basic usage is very intuitive:

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

For example,item.KeywordsContains strings like

{% set keywordList = item.Keywords | split:"," %}

Now,keywordListIt becomes a container that includes["网站优化", "SEO", "内容营销"]The array. Next, you can use the template'sforloop tags to iterate over and display these keywords:

<div class="article-tags">
    {% set keywordList = item.Keywords | split:"," %}
    {% for keyword in keywordList %}
        <a href="/tag/{{ keyword | urlencode }}">{{ keyword | trim }}</a>
    {% endfor %}
</div>

In this example, we not only usedsplitbut also combined withurlencodeThe filter ensures that keywords in the URL are safe, as well astrimThe filter removes any existing leading and trailing spaces.

It should be noted that if the specified delimiter is not present in your string,splitThe filter will treat the entire string as a single element, returning an array that contains only that string. In addition, if the delimiter is an empty string"",splitThe string will split each character into a separate array element.

There is also another related filter ismake_listIt is associated withsplitdifferent,make_listAlways split each character of a string (including Chinese characters) into separate array elements. It will be very convenient if you need to split by character rather than a specific delimiter.make_listIt will be very convenient.

Translate array elements into a single string:joinFilter

WithsplitOn the contrary,joinThe filter can concatenate all elements of an array (list) using the specified "glue" to form a single string.This is very useful when it is necessary to display multiple options, tags, or any list data in a uniform format.

joinThe usage of filter is as follows:

{{ 您的数组变量 | join:"拼接符" }}

Assuming you already have a namedcategoriesarray that may contain["新闻", "公告", "活动"]such classification names, now you want to join them with commas:to display them together:

<p>文章分类:{{ categories | join:"、" }}</p>

This will output as: "Article Category: News, Announcement, Event."

If you wish to fromsplitThe results obtained from the filter, you can also reassemble them, or you can directly perform chaining operations:

{% set rawTags = "tag1, tag2, tag3" %}
{% set processedTags = rawTags | split:"," | join:" - " %}
<p>处理后的标签:{{ processedTags }}</p>

This code will first split the string "tag1, tag2, tag3" into["tag1", " tag2", " tag3"], then use-Spliced, it will be displayed as “tag1 - tag2 - tag3”. Please note.splitThe elements may contain spaces, and if you want to concatenate without extra spaces, you cansplitapply to each element aftertrima filter, or consider spaces insplitthe separator.

An interesting feature is that, ifjoinThe object upon which the filter operates is itself a string rather than an array, and it will insert the specified concatenation symbol between each character. For example,{{ "安企CMS" | join:"-" }}it will output:安-企-C-M-S.

Summary and Application

splitandjoinThe filter is a powerful tool for data transformation and formatted output in the AnQi CMS template.They make it simple and efficient to extract information from structured data or to integrate fragmented information for display.No matter whether you are dealing with tags for user input, multiple value options in custom fields, or need to dynamically generate specific text formats based on data, these two filters can provide great convenience.Familiarize yourself with them, as it will help you build more dynamic, flexible, and easy-to-maintain website templates.


Common Questions (FAQ)

1.splitfilters andmake_listWhat are the differences between filters?

splitThe filter splits the string based on the delimiter you specify explicitly. For example,"a,b,c" | split:","You will get["a", "b", "c"].make_listThe filter separates each character of a string into an individual array element, regardless of whether the character is English, numeric, or Chinese characters. For example,"你好" | make_listYou will get["你", "好"]. Typically, when you have specific separators (such as commas, semicolons) usesplit, and when you want to process the string character by character usemake_list.

2. If the content I split or concatenate contains HTML tags, how can I ensure that they are parsed correctly and not displayed as plain text?

The AnQi CMS template engine defaults to escaping the output HTML content for security reasons to prevent XSS attacks. If you are sure that the split or concatenated string is safe HTML content, and you want the browser to parse it as HTML elements rather than plain text, you can use it when outputting variables.safefilter. For example:{{ processedContent | safe }}。Please ensure that the content source is reliable to avoid security risks.

3. If I try to usesplitWhat happens if the filter splits a string but the string does not contain the specified delimiter?

WhensplitThe filter does not raise an error when it cannot find the specified delimiter in a string. Instead, it will return the entire original string as a single element, resulting in an array that contains only this one element. For example,"Hello World" | split:","the result will be["Hello World"]This means you can still iterate over this result orjoinperform operations without worrying about the program breaking.