What is the difference between the `make_list` filter and the `split` filter? When should `make_list` be used to split strings?

Calendar 👁️ 75

In AnQi CMS template development, we often need to process string data, such as splitting a long text into multiple independent parts for traversal, display, or further logical judgment. AnQi CMS provides various filters (Filters) to help us complete these tasks, among whichmake_listandsplitAre two commonly used string splitting tools.Although they can all convert strings to lists (or arrays), they have obvious differences in functional focus and use cases.Understanding these differences can help us choose the appropriate tools more efficiently to meet the needs of content operations.

splitFilter: precise cutting, clear boundaries

splitThe filter is a very practical tool, which splits a string into an array of strings based on the delimiter you specify (usually corresponding to slice in Go language templates). Imagine if you get a string of keywords separated by commas from the background, or a segment of parameters connected by a specific symbol.splitWill be of use.

For example, you might have a tag field in an article, whose value is"SEO优化,内容营销,网站运营". If you want to display or iterate over these tags, you can use it like this.split:

{% set tags_string = "SEO优化,内容营销,网站运营" %}
{% set tag_array = tags_string|split:"," %}
{# 此时 tag_array 会是 ["SEO优化", "内容营销", "网站运营"] #}

<ul>
{% for tag in tag_array %}
    <li>{{ tag }}</li>
{% endfor %}
</ul>

splitIts characteristic is that it requires you to provide a clear, identifiable delimiter. If the string does not contain the specified delimiter,splitIt will treat it as a whole, returning a single-element array that contains the original string itself. It is worth noting that if we pass an empty string as a delimiter tosplitFor example"你好世界"|split:""It will also split the string into an array of each UTF-8 character.

make_listFilter: split characters, detailed and meticulous.

withsplitDepending on the delimiter, different,make_listThe filter plays a more detailed role. Its main function is to take every singlecharacterSplit into an independent element in the array.It does not care whether there are delimiters, but treats the string as a sequence of characters directly.Whether it is a letter, Chinese character, number, or punctuation mark, each single character will become a member of the array.

For example, if you have a string"你好世界", usingmake_listYou will get an array containing each Chinese character:

{% set greeting = "你好世界" %}
{% set char_list = greeting|make_list %}
{# 此时 char_list 会是 ["你", "好", "世", "界"] #}

<div>
{% for char in char_list %}
    <span class="char-style">{{ char }}</span>
{% endfor %}
</div>

Even if it is English or other multibyte characters,make_listit will be split at the character granularity. For example,"AnQiCMS"aftermake_listAfter processing, it will become["A", "n", "Q", "i", "C", "M", "S"].

When to usemake_listHow to split a string?

Now let's clarify when to make a choicemake_listInsteadsplitEspeciallysplitAnd it can also achieve word-by-word splitting through space separators:

  1. Clarify the scenarios where word-by-word processing is needed:When your business logic indeed needs to operate on each character of the string independently,make_listIs a more intuitive choice. For example, you may need to assign different styles to each character of a title, or perform some kind of character-level encryption, obfuscation, or display.make_listClearly expressed the intention of "splitting by character."

    {# 假设需要将网站名称的每个字都独立显示,并添加动画效果 #}
    {% set site_name = "安企CMS" %}
    {% for char in site_name|make_list %}
        <span class="animated-char">{{ char }}</span>
    {% endfor %}
    
  2. There is no fixed delimiter in the string, but still needs to be split:Sometimes, the string you get may not have a regular delimiter, but you still need to split it into independent units for processing.make_listIn this case, a simple and direct solution is provided, avoiding the trouble of trying to find or construct a non-existent delimiter.

  3. Handling multibyte characters and international content:due tomake_listIt is split based on UTF-8 characters, which can handle characters of various languages such as Chinese, Japanese, and Korean well, ensuring that each multi-byte character is correctly identified as an independent element. AlthoughsplitYou can also achieve a similar effect with a space separator, butmake_listIt clearly expresses the intention of splitting by character in terms, making the code more readable and maintainable.

In summary, when you are faced with a structured string with clear delimiters, splitIs your preference. When your need is to process each independent character in the string without any fixed delimiter, or the string itself does not contain any fixed delimiter,make_listIt can provide a more concise and semantically clear solution. Understanding the subtle differences between these two filters will make your Aiqi CMS template development work more effortless.


Frequently Asked Questions (FAQ)

Q1:splitandmake_listWhat are the special considerations when processing Chinese characters?A1: They can all handle Chinese characters correctly.splitWhen specifying an empty string""When used as a separator, Chinese characters will be split one by one according to UTF-8, which is similar tomake_list. For example,"你好"|split:""and"你好"|make_listwill split the string into["你", "好"]. The main difference lies inmake_listThe semantics are clearer, it explicitly indicates 'split by character', whereassplitit requires an empty delimiter to simulate this behavior.

Q2: IfsplitWhat happens if the filter cannot find the specified delimiter?A2: IfsplitThe filter does not find the specified delimiter in the target string. It will not raise an error, but will return a single-element array containing the original string as a whole. For example,"安企CMS"|split:","will return["安企CMS"].

Q3: SincesplitIt can also be achieved by splitting words one by one with a space, thenmake_listWhat is the unique advantage ofA3:make_listThe unique advantage lies in itsSemantic clarityandSimplicityWhen your intention is to split it word by word, usemake_listit can more directly express this purpose, and the code readability is better. Althoughsplit:""it can also achieve the same effect, butmake_listNeed not think extra how to construct an "empty separatorIt makes character-level string processing more explicit and simple.

Related articles

`split` filter: How to split a long string into an array by a specific delimiter?

In the powerful functions of AnQi CMS, the flexibility of content display has always been our focus.Sometimes, you may need to enter a long string of information in a field on the back end, such as multiple keywords for an article, multiple parameters for a product specification, or a list of skills for a team member.This information is usually connected by specific delimiters (such as commas, semicolons, or pipes).However, when you want to display this information individually on the front page, even to style each part differently, the problem arises: how can you conveniently split this long string into independent items?

2025-11-08

What are the limitations of the `first` and `last` filters when getting the first and last elements of a string/array?

In Anqi CMS template design, the `first` and `last` filters are two concise and practical tools designed to help us quickly obtain the first or last character of a string, as well as the first or last element of an array.They perform well in handling simple data structures, but in practical applications, if their internal mechanisms are not well understood, they may encounter some unexpected behaviors.Firstly, let us understand their core functions. For string type data, `first` will return the first character, and `last` will return the last character

2025-11-08

How can you quickly get the first element of an array or the first character of a string in a template?

In Anqi CMS template development, efficiently obtaining data is the key to improving website performance and user experience.Whether it is dealing with a document list or image group obtained from the background, or operating on a common string, we often encounter the need to extract the first element of an array or the first character of a string.The Anqi CMS, based on Go language and Django template engine syntax, provides various intuitive and practical methods to achieve this goal.

2025-11-08

Check array or string length: `length_is` filter practical skills in AnQiCMS template?

When building a website, we often need to dynamically adjust the page display or execute different logic based on the length of the content.For example, if a title is too long and needs to be truncated, or if a list is empty and a prompt such as 'No content available' needs to be displayed.In AnQiCMS (AnQiCMS) template system, the `length_is` filter is exactly the tool for handling such precise length judgments.

2025-11-08

Array concatenation into a string: Application scenarios of the `join` filter in AnQiCMS templates?

In website content management, we often encounter the need to display a set of data, such as multiple tags, multiple image URLs, or custom multiple-choice content, in a unified string format.AnQiCMS (AnQiCMS) boasts its efficient architecture based on the Go language and a flexible template engine similar to Django, providing great convenience for content display.Today, let's delve deeper into a very practical tool for handling such needs - the `join` filter.What is the `join` filter? In the AnQi CMS template world

2025-11-08

How to safely truncate the article content using the `truncatechars_html` filter without destroying the HTML structure for website SEO?

In website operation, we often need to display the concise content of articles in article lists, search results summaries, or social media shares.This concise content should not only attract users' attention, but also ensure that the complex HTML structure behind it is not damaged, in order to avoid affecting the layout and user experience.For search engine optimization (SEO), a clean and effective code structure is crucial. AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go programming language, deeply understanding the pain points and needs of website operation.It not only provides multi-site management, a flexible content model

2025-11-08

Template content shows: How does the `truncatewords` filter truncate text by word count?

In the presentation of website content, we often need to condense lengthy text information to meet different layout requirements, such as displaying article summaries on list pages, brief descriptions on product cards, etc.This not only makes the page look neater, but also helps visitors quickly grasp the core information.AnQiCMS provides flexible template filters for this type of need, where the `truncatewords` filter is your powerful assistant for truncating text by word count.### A clever and concise summary, making the content clear at a glance

2025-11-08

How to get a specific digit from a number: Advanced usage of the `get_digit` filter in AnQiCMS template?

In AnQiCMS template development, flexible handling and displaying data is the key to enhancing website functionality and user experience.When faced with a sequence of numbers, such as product numbers, order numbers, or some kind of identification code, sometimes we do not need the entire number, but rather a specific digit at a certain position.At this time, the `get_digit` filter has become a very practical tool.It can help us extract the required part from the numbers accurately, providing more possibilities for the dynamic display of templates and logical judgment.

2025-11-08