Strings are cut into arrays according to the specified delimiter

How to cut strings of a specific format into arrays in Anqi CMS templates according to the specified delimiter?

splitFilters can cut strings of specific formats into arrays by specified delimiters. If the delimiter does not exist in the string, an array of length 1 is returned, and the value of the array is the string itself. If the delimiter is empty, it will be split into an array by each utf8 character.

Can also be usedmake_listTo quickly cut strings into arrays.make_listEach character will be split into an array with a value, a letter is one, and a Chinese character is one.

How to use

splitHow to use filters:

{{ obj|split:"split character" }}

make_listHow to use filters:

{{ obj|make_list }}

For example, it is necessary tosplits, the, string, 安企CMSaccording to,Cut into an array, you can write this:

{{ "splits, the, string, Anqi CMS"|split:", "|stringformat:"%#v" }}
# Show results
[]string{"splits", "the", "string", "Anqi CMS"}

splitFilters also support use in assignment labels (sets), storing the judgment results into a variable, such as:

{% set values ​​= "splits, the, string, Anqi CMS"|split:", " %}
{% for item in values ​​%}
><span>{{item}}/</span>
{% endfor %}
# Show results
splits/ the/ string/ Anqi CMS/

Sample Demo

splitFilter

{{ "splits, the, string, Anqi CMS"|split:", " }}
{{values|stringformat:"%#v"}}
# Show results
[]string{"splits", "the", "string", "Anqi CMS"}
{{ "Hello, 99, 3.140000, good"|split:", "|join:", " }}
# Show results
Hello, 99, 3.140000, good

make_listFilter

{{ "john doe"|make_list|join:", " }}
{% for char in "john doe"|make_list %}{{ char }}{% endfor %}
# Show results
j, o, h, n, , d, o, e
john doe