The 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 from strings or arrays. Whether you want to display article summaries, limit the number of images, or handle other data snippets, it can come in handy.

sliceThe working 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"which defines the range you want to extract.

UnderstandingsliceThe key points of the filter are as follows:

  1. zero-based indexinglike many programming languages,slicethe filter from0Start calculating the index of elements. That is, the index of the first element is0The second one is1and so on.
  2. Left closed and right open interval:sliceThe range to be cut is 'left closed and right open'. This means the starting indexfromThe element at the end index will be included in the result.toThe element at this index will not be included.
  3. Flexible omission rules:
    • If omittedfrom(For example}]}":to") will start from the beginning (index0Start extracting from the end.
    • If omittedto(For example}]}"from:"From here, it will extract.fromExtract from the index to the end.
    • IffromandtoAll are omitted (for example)":"Or not using directlysliceIf a parameter is omitted, it means to truncate the entire string or array (equivalent to making a copy).
  4. negative indicesYou can also use negative indices, which count from the end. For example,-1Represents the last element.-2Represents the second-to-last element, and so on.

Next, let's see through specific examples.sliceHow filters work in strings and arrays.

String slicing: Flexible control of displayed content

When you need to extract a part of the 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 endIf you only want to get the content from index6to the end (i.e.,"AnQiCMS World!"), you can write it like this:{{ "Hello AnQiCMS World!"|slice:"6:" }}The result will be:AnQiCMS World!

  • Truncate from the beginning to the specified positionIf you only want to get the content from the beginning to the index5(i.e.,"Hello"), you can write it like this:{{ "Hello AnQiCMS World!"|slice:":5" }}The result will be:Hello

  • cutting a specified rangeIf you want to get the content from the index6to the index13(i.e.,"AnQiCMS"), you can write it like this:{{ "Hello AnQiCMS World!"|slice:"6:13" }}The result will be:AnQiCMS

  • Use negative index If you want to get the last word of a string (i.e.,"World!"), you can use negative index:{{ "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, which has good support for UTF-8 encoded characters. Therefore, when usingsliceWhen filtering strings containing Chinese characters, there is no need to worry about乱码 or incomplete truncation. Each Chinese character is treated as an independent 'character' for indexing.

Extract array (or list): Precisely extract data segments

sliceThe filter works on an array (or list) in a manner similar to string slicing, except that the object being operated on is the elements within the array.

Assuming you have an array containing numbers[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]we can directly get its value throughsplitFilter simulation generates an array like this:{% set numList = "1,2,3,4,5,6,7,8,9,10"|split:"," %}

Now, we can perform on)numListThis array is truncated:

  • cut from the specified position to the endIf you want to get the content from the index3From the beginning to the end elements (i.e.,)[4, 5, 6, 7, 8, 9, 10]):{{ numList|slice:"3:"|join:"," }}(Note:)join:","Just to display array elements as a comma-separated string,.sliceThe operation itself returns a new array. The result will be:.4,5,6,7,8,9,10

  • Truncate from the beginning to the specified position If you want to get the elements from the start to index5(i.e.,[1, 2, 3, 4, 5]):{{ numList|slice:":5"|join:"," }}The result will be:1,2,3,4,5

  • cutting a specified rangeIf you want to get the content from the index3to the index7(i.e.,[4, 5, 6, 7]):