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:
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 %}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.Handling multibyte characters and international content:due to
make_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.