How to split a Chinese sentence into an array of single characters in AnQiCMS template?

Calendar 👁️ 71

In website content operation, sometimes we need to control Chinese text more finely, such as splitting a sentence into individual characters for display, or applying different styles and animation effects to each character.This need is especially common when creating some special UI effects, interactive content, or even text-based games.How does the template system help us achieve this function when building a website using AnQiCMS?

AnQi CMS uses a template engine syntax similar to Django, which provides rich built-in tags and filters to help us easily implement various content operation requirements. For the need to split a Chinese sentence into an array of individual characters, the AnQi CMS template provides a very convenient and powerful filter:make_list.

Get to knowmake_listFilter

make_listThe filter, as the name implies, is used to split a string (including Chinese, English, numbers, and punctuation) into individual characters one by one, and then convert it into an array of characters.At the low-level implementation, it ensures that each Chinese character is recognized as an independent character and not incorrectly split into bytes, which is particularly important for handling multi-byte UTF-8 encoded Chinese.

This filter greatly simplifies the complexity of character-level operations.Once a string is split into an array, we can use the loop structure of the template engine to independently process each character in the array, thereby achieving various refined display effects.

Actual operation example

Let's take a look at several specific examplesmake_listHow does the filter work in the Anqi CMS template.

Suppose we have a Chinese sentence stored in a template variable

{% set chinese_sentence = "欢迎使用安企CMS!" %}

1. Split the Chinese sentence into an array and view its content

We can combinechinese_sentencethe variable throughmake_listThe filter processes it and then passes the result through againjoinThe filter concatenates them into a string so that the split array elements can be displayed intuitively on the page

{% set char_array = chinese_sentence|make_list %}
<p>拆分后的数组内容:{{ char_array|join(", ") }}</p>

After running, the page may display:拆分后的数组内容:欢, 迎, 使, 用, 安, 企, C, M, S, !

This clearly showsmake_listHow each Chinese character and English punctuation mark is split into an independent element.

2. Traverse the character array and render each character independently.

The most common use of a split character array is to cooperateforLoop through each character to apply different styles or perform other dynamic operations.

<div class="character-display">
    {% for char in chinese_sentence|make_list %}
        <span style="display:inline-block; margin-right:5px; padding:3px; border:1px solid #ccc; background-color:#f9f9f9;">{{ char }}</span>
    {% endfor %}
</div>

This code will output each character of the sentence, wrapping each character in a simple style<span>In the tag. You can imagine, by CSS or JavaScript, you can make these independent<span>Add various animations, color transitions, or click events to achieve rich interactive effects.

3. Combine conditional judgments for more complex processing.

When iterating over characters, you can also make conditional judgments based on the character itself to achieve more flexible display logic.For example, we may want to apply different styles to punctuation marks and Chinese characters.

<div class="styled-text">
    {% for char in chinese_sentence|make_list %}
        {% if char == "!" or char == "," or char == "。" %} {# 假设要对标点符号特殊处理 #}
            <span style="color:red; font-weight:bold;">{{ char }}</span>
        {% else %}
            <span style="color:blue;">{{ char }}</span>
        {% endif %}
    {% endfor %}
</div>

Such, all punctuation marks will be displayed in red bold, and other Chinese characters will be displayed in blue, adding more possibilities to the visual presentation of the page.

Summary

Bymake_listThe filter, Anqi CMS template makes character-level operations on Chinese text extremely simple and intuitive.In order to achieve a unique visual design, or to develop interactive functions that require precise text control, this filter provides powerful tools for content managers and template developers.Combine the other features of the Anqi CMS template engine, such as loops, conditional judgments, and various auxiliary filters, and you will be able to build highly customized and expressive website content.


Frequently Asked Questions (FAQ)

1.make_listCan the filter handle English characters, numbers, or mixed text?Yes,make_listThe filter can recognize and correctly handle any Unicode character.It not only can split Chinese characters into independent characters, but also will split English letters, numbers, punctuation marks, and mixed Chinese and English text into single characters, generating the corresponding character array.

2. Can you directly access a character at a specific position in a split character array? For example, do you want to get the first character of a sentence?Of course you can.make_listThe filter generates a standard array structure, you can access characters at specific positions like any other array. For example, if you assign the split array tochar_arrayA variable, then{{ char_array[0] }}Will get the first character of the array,{{ char_array[1] }}Then get the second character, and so on.

3. Besidesmake_listCan Anqi CMS template provide other filters to split strings by words or spaces?Yes, Anqi CMS template providessplitandfieldsThese two filters are used to split strings by word or by space.splitThe filter allows you to specify any delimiter (such as a comma, space, or specific word) to split a string into an array. For example:{{ "苹果,香蕉,橘子"|split:"," }}.fieldsThe filter is simpler, it defaults to splitting the text into an array of words based on spaces in the string. For example:{{ "Hello AnQiCMS World"|fields }}These filters are very useful when dealing with English phrases or structured text.

Related articles

What is the difference between the `split` filter and the `fields` filter when splitting strings by spaces?

During the template creation process in AnQi CMS, we often encounter scenarios where we need to split strings, such as extracting keywords from a description or parsing tag strings into independent words.AnQi CMS provides the practical filters `split` and `fields`. Although they can all split strings into arrays, there are subtle and crucial differences in their working methods and application scenarios when splitting strings by spaces.### `split` filter: The versatile separator expert `split` filter

2025-11-08

How to split a string containing multiple tags (such as “SEO, keywords, optimization”) into an array?

In the daily operation of Anqi CMS, we often encounter scenarios where we need to flexibly display some structured information in the content.For example, when setting keywords for articles or products, we may enter a string containing multiple tags in the "document keywords" field in the backend, such as Such strings are convenient to enter, but when displayed on the front end, we usually hope to display these tags separately, even to convert them into independent, clickable elements.How to convert such a comma-separated tag string

2025-11-08

How to get the number of elements in a list or key-value pair in AnQiCMS template?

AnQiCMS (AnQiCMS) makes website content display intuitive and efficient with its flexible template engine.In website operation, we often need to dynamically adjust the page layout, display different content, and even perform complex logical judgments based on the number of elements in a list or key-value pair (Map).Mastering how to obtain the number of these elements in a template is an indispensable skill for advanced customization and optimization.Luckyly, AnQiCMS template syntax provides a variety of convenient ways to help us achieve this goal.### Clever Utilization

2025-11-08

What is the difference between the `length` filter and the `length_is` filter in terms of content length judgment?

In AnQiCMS template design, we often need to judge or obtain the length of content in order to flexibly control the display of content.The `length` filter and `length_is` filter are born for this purpose.Although they are all related to 'length', in actual use, their functions and application scenarios are obviously different.Understanding these subtle differences can help us build templates more efficiently and accurately.### `length` filter: Content length 'counter' `length`

2025-11-08

How to concatenate a dynamically generated array (such as an article ID list) into a URL parameter string?

In website operation and content management, we often encounter such needs: to concatenate a group of dynamically generated data (such as multiple article IDs, tag IDs, etc.) in a specific format as URL parameters, in order to filter, perform batch operations, or display more accurate content.For example, the user may have selected multiple articles for comparison on the frontend, or the backend may need to generate a URL to filter articles within a specific ID range.AnQiCMS (AnQiCMS) leverages its powerful backend performance based on the Go language and flexible Django-style template engine

2025-11-08

How to replace all old keywords with new keywords in AnQiCMS template?

During website operation, we often encounter the need to update a specific text string in a website template.This may be due to a change in brand name, which requires a unified adjustment of a product or service description, or it may simply be to correct a global spelling error.Manually search and modify these strings in all related template files, which is not only time-consuming and labor-intensive but also prone to errors.Fortunately, AnQiCMS provides a very practical template filter named `replace`, which can help us efficiently and accurately complete the batch replacement of strings

2025-11-08

Can the `replace` filter be used to batch modify specific links or text in article content?

In website content operation, we often encounter such situations: a brand name has been updated, or a batch of external links have failed and need to be replaced uniformly, or the internal link structure of the website has been adjusted, and it is necessary to synchronize update the old links or text in a large number of articles.At this point, we would naturally think that the `replace` filter of AnQiCMS (AnQiCMS) can help us handle these modifications in bulk?The answer is, the `replace` filter is not used for batch modifying article content, its function is in another direction

2025-11-08

How can extra spaces or special characters be removed from user input or dynamic content?

In website operation, we often face an indispensable challenge: how to ensure that the user input or dynamically generated content is clean, tidy and safe.Extraneous spaces, invisible control characters, even unprocessed special symbols, may affect the layout beauty of the website, the search engine optimization (SEO) effect, and even bring potential security risks.AnQiCMS as an efficient and customizable enterprise-level content management system has fully considered these operational pain points.It provides a series of powerful and flexible template filters

2025-11-08