In the template development of AnQiCMS,sliceThe filter is a very practical tool that allows us to flexibly extract a portion of the string or array content.But do you know?It can also realize a particularly clever function——to extract elements from the end of an array or list, which is very convenient when it comes to displaying the latest data or removing old data.sliceHow does the filter implement this feature through negative indices.
UnderstandingsliceBasic usage of the filter.
As the name implies,sliceThe filter is used for "slicing" data, which can extract the part you need from a longer sequence (such as a string, array, or list). Its basic syntax is{{ obj|slice:"from:to" }}.
Here, fromandtoRepresent index positions:
from: Indicates the start index (including the element at this position).to: Indicates the end index (not including the element at this position).
For example, if you have a string"AnQiCMS模板制作"To truncate the first 5 characters, you can use this:
{{ "AnQiCMS模板制作"|slice:"0:5" }}
{# 显示结果: AnQiCMS #}
For arrays,sliceThe filter is also effective. Suppose we have a list of numbers[1, 2, 3, 4, 5]To get the elements from index 1 to index 3 (not inclusive), you would typically go throughsplitThe filter converts the string to an array and then performs the operation:
{{ "1,2,3,4,5"|split:","|slice:"1:4"|join:","}}
{# 显示结果: 2,3,4 #}
As you can see,sliceThe filter follows the 'left closed right open' principle, which means the range to be截取包含fromelements corresponding to the index, but does not includetoelements corresponding to the index.
sliceThe secret of the filter from the end: negative indices
When we need to operate from the end of the sequence,slicethe negative index of the filter comes into play. A negative index indicates counting down from the end, for example-1it means the last element,-2represents the second-to-last element, and so on.
By cleverly combining negative indices, we can achieve various needs for截取 from the end:
1. Get the last N elements
If you want to get the last N elements of an array or string, you can setfromto a negative number, andtothen you can omit it (indicating the end of the sequence):
{# 假设我们有一个列表:["A", "B", "C", "D", "E"] #}
{{ "A,B,C,D,E"|split:","|slice:"-2:"|join:","}}
{# 显示结果: D,E —— 获取最后2个元素 #}
{# 如果想获取最后3个元素 #}
{{ "A,B,C,D,E"|split:","|slice:"-3:"|join:","}}
{# 显示结果: C,D,E #}
Here-2:Start from the second-to-last element and continue to the end of the sequence.
2. Get all elements except the last N elements.
If you need all elements but want to exclude the last N, you cantoto a negative number, andfromThen it can be omitted (indicating from the beginning of the sequence):
{# 假设我们有一个列表:["A", "B", "C", "D", "E"] #}
{{ "A,B,C,D,E"|split:","|slice:":-2"|join:","}}
{# 显示结果: A,B,C —— 排除最后2个元素 #}
{# 如果想排除最后3个元素 #}
{{ "A,B,C,D,E"|split:","|slice:":-3"|join:","}}
{# 显示结果: A,B #}
Here:-2It indicates starting from the beginning and cutting off to the second-to-last element (but not including the second-to-last element).
3. From a specific position at the end to the end (not including).
This usage is slightly complex, but very flexible. You can specify starting from the Mth element from the end, to the element before the Nth from the end:
{# 假设我们有一个列表:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] #}
{{ "1,2,3,4,5,6,7,8,9,10"|split:","|slice:"-5:-2"|join:","}}
{# 显示结果: 6,7,8 —— 从倒数第5个元素 (6) 开始,到倒数第2个元素 (9) 之前结束 #}
In the above example,-5Point to6,-2Point to9So the cut is6,7,8.
Application scenarios in practice
Understood the negative index