In AnQiCMS template development, we often need to process displayed data in various ways, such as truncating a section of text, or extracting specific items from a list.sliceThe filter is exactly for this, it can help us accurately截取string or array part.However, simply extracting data is not enough. How to conveniently assign the extracted results to a new variable for repeated use in the subsequent part of the template or for more complex logical processing is the key to improving template development efficiency and code readability.
sliceBasic Filter: How to accurately extract data
sliceThe filter is a powerful tool provided by the AnQiCMS template engine, used to extract specified elements from strings (string) or arrays (slice/list). Its basic usage is very intuitive: {{ obj|slice:"start:end" }}.
objThis is the source data you want to extract, which can be a string variable or an array variable."start:end": This is a key string parameter that defines the start and end positions of the cut.start: Represents the start index. The element at this position isincludein the result. If omittedstartfor example":end"),then it will start cutting from the beginning of the data.end: represents the end index of the cut. This element is theDo not includein the result. If omittedendfor example"start:"),then it will start cutting fromstartposition and continue cutting to the end of the data.- An index can be positive or negative. Positive indices start counting from 0 (the first element is 0). Negative indices indicate counting from the end, for example
-1represents the first element from the end,-2represents the second element from the end.
Let us understand through some simple examplessliceThe basic working method of filters:
{# 截取字符串的前三个字符 #}
{# "AnQiCMS"的索引是0,1,2,3,4,5,6。截取到索引3(不包含)就是0,1,2 #}
{{ "AnQiCMS"|slice:":3" }} {# 结果: AnQ #}
{# 截取字符串的第二个到第四个字符(索引1到4,不包含索引4) #}
{# 索引1是'n',索引2是'Q',索引3是'i' #}
{{ "AnQiCMS"|slice:"1:4" }} {# 结果: nQi #}
{# 截取数组的倒数第二个到末尾 #}
{# 假设 intList 是一个包含数字的数组,如 [1, 2, 3, 4, 5]。负数索引-2对应数字4 #}
{{ intList|slice:"-2:"|join:", " }} {# 结果: 4, 5 #}
tosliceThe result is assigned to a new variable:setandwithTag
In the template syntax of AnQiCMS, we can take advantage of{% set %}and{% with %}These tags are used to create new variables. ForsliceThe result after the filter is processed, the most commonly used and recommended way in most scenarios is to use{% set %}.
{% set %}The tag allows you to declare a new variable and assign a value anywhere in the template.The scope of this variable is local, starting from the point of definition to the end of the template file, unless it is overwritten by the same variable name.Its basic syntax is:{% set variableName = value %}.
{% with %}Tags are used to define temporary variables within a specific code block. These variables are only valid within{% with %}and{% endwith %}. Although it can also be used for assignment, in most cases,{% set %}It provides broader flexibility because the variables defined can be accessed in a larger part of the template.
CombinesliceWith the filter, we can save the extracted data into a new variable like this:
`twig