AnQiCMS template engine provides a rich set of filters, giving you great flexibility in displaying content. Among them,sliceA filter is a very practical tool that can help you accurately extract elements within a specified range of strings or arrays. Whether you want to display an article summary, limit the number of images, or process other data segments, it can come in handy.
sliceThe principle of the filter.
sliceThe syntax of the filter is concise and clear:{{ obj|slice:"from:to" }}. Here,objIt can be the string or array variable you need to operate, and"from:to"it defines the range you want to extract.
UnderstandingsliceThe key points of the filter are as follows:
- 0-based indexinglike many programming languages,
slicefilters from0Start calculating the index of the element. That is, the index of the first element is0, the second one is1and so on. - Left-closed, right-open interval:
sliceThe range extracted is 'left-closed, right-open'. This means the starting index isfromThe element at this position will be included in the result, while the end indextoThe element at this position will not be included. - Flexible ellipsis rule:
- If omitted
from(For example)":to") will start from the beginning (index0) will start cutting from. - If omitted
to(For example)"from:"), will start fromfromand continue to the end. - If
fromandtoAll of them are omitted (for example":"or not usingslicethe parameter), it means to cut the entire string or array (equivalent to copying a copy).
- If omitted
- Negative index: You can also use negative indices, which indicate counting from the end. For example,
-1represents the last element,-2represents the second-to-last element, and so on.
Next, let's see through specific examplessliceHow filters work in strings and arrays.
String slicing: Flexible control of displayed content
When you need to extract a portion of content from a longer text,slicethe filter is very efficient.
Suppose you have a string"Hello AnQiCMS World!":
cut from the specified position to the end: If you only want to get the content from index
6to the end (i.e.,"AnQiCMS World!"), you can write it like this:{{ "Hello AnQiCMS World!"|slice:"6:" }}The result will be:AnQiCMS World!Cut from the beginning to the specified position: If you only want to get the content from the beginning to the index
5that is"Hello"), you can write it like this:{{ "Hello AnQiCMS World!"|slice:":5" }}The result will be:Hellocut the specified range: If you want to get the content from the index
6to index13that is"AnQiCMS"), you can write it like this:{{ "Hello AnQiCMS World!"|slice:"6:13" }}The result will be:AnQiCMSusing negative indices: If you want to get the last word of a string (i.e.
"World!"), you can use negative indices:{{ "Hello AnQiCMS World!"|slice:"-7:" }}(starting from the seventh character from the end). The result will be:World!Or, if you want to get all content except the last character:{{ "Hello AnQiCMS World!"|slice:":-1" }}The result will be:Hello AnQiCMS World
It is worth mentioning that AnQiCMS is developed based on the Go language and has good support for UTF-8 encoded characters. Therefore, when usingsliceWhen filtering a string containing Chinese characters, there is no need to worry about garbled characters or incomplete truncation. Each Chinese character is treated as an independent 'character' for indexing.
Extract array (or list): accurately extract data fragments
sliceThe filter processes arrays (or lists) in a manner similar to string slicing, but the object being operated on is the elements of the array.
Suppose you have an array containing numbers.[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]we cansplitThe filter simulates generating an array like this:{% set numList = "1,2,3,4,5,6,7,8,9,10"|split:"," %}
Now, we can performnumListThis array is subjected to a slicing operation:
cut from the specified position to the end: If you want to get the content from the index
3The elements from the beginning to the end (i.e.,[4, 5, 6, 7, 8, 9, 10]):{{ numList|slice:"3:"|join:"," }}(Note:join:","To simply display the array elements as a comma-separated string,sliceThe operation itself returns a new array.) The result will be:4,5,6,7,8,9,10Cut from the beginning to the specified position: If you want to get from the start to the index
5that is[1, 2, 3, 4, 5]):{{ numList|slice:":5"|join:"," }}The result will be:1,2,3,4,5cut the specified range: If you want to get the content from the index
3to index7that is[4, 5, 6, 7]):