How to cut strings of a specific format into arrays in Anqi CMS templates according to the specified delimiter?
split
Filters 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_list
To quickly cut strings into arrays.make_list
Each character will be split into an array with a value, a letter is one, and a Chinese character is one.
How to use
split
How to use filters:
{{ obj|split:"split character" }}
make_list
How to use filters:
{{ obj|make_list }}
For example, it is necessary tosplits, the, string, 安企CMS
according 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"}
split
Filters 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
split
Filter
{{ "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_list
Filter
{{ "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