The template engine of Anqi CMS provides a name calledsliceThe powerful filter, which can help us easily meet this need.This filter works in a manner similar to slice operations in the Go language, allowing us to obtain a subset of a data collection (whether an array, slice, or string) by specifying the start and end indices.

UnderstandingsliceBasic syntax of filters

sliceThe basic syntax of the filter is very intuitive:{{ obj|slice:"from:to" }}.

Here:

  • objThis is the source data you want to perform the cutting operation on, it can be an array (or a slice in Go language), a string, and other indexable data types.
  • "from:to"It is a string used to specify the range to be截取的范围。
    • fromIt represents the starting index (including the element at this position).
    • toIt represents the ending index (not including the element at this position).
  • It should be noted that, like most programming languages, indexing starts from0.

For example, if you have a collection containing numbers[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]The array, and you hope to get a part from the 3rd element (index 2) to the 7th element (index 6), you can use it like thissliceFilter:

{% set myNumbers = "[1,2,3,4,5,6,7,8,9,10]"|split:"," %}
{{ myNumbers|slice:"2:7"|join:"," }}

The output of this code will be:3,4,5,6,7.

Use flexiblyfromandtoParameters

sliceThe filter in the specifiedfromandtoParameters also provide a lot of flexibility:

  1. OmittedfromIndex: If you omit it,fromIndex (for example,":to"), it will start cutting from the beginning of the collection (index 0).

    {% set myNumbers = "[1,2,3,4,5,6,7,8,9,10]"|split:"," %}
    {{ myNumbers|slice:":5"|join:"," }} {# 输出: 1,2,3,4,5 #}
    
  2. OmittedtoIndex: If you omit it,toIndex (for example,"from:"),它将从指定的Englishfrom索引开始,一直截取到集合的末尾English

    {% set myNumbers = "[1,2,3,4,5,6,7,8,9,10]"|split:"," %}
    {{ myNumbers|slice:"7:"|join:"," }} {# 输出: 8,9,10 #}
    
  3. Use negative index:slice过滤器还支持使用负数索引,这使得从集合末尾开始计数变得非常方便English

    • "-N:"Represents from the lastNelement to the end.
    {% set myNumbers = "[1,2,3,4,5,6,7,8,9,10]"|split:"," %}
    {{ myNumbers|slice:"-3:"|join:"," }} {# 输出: 8,9,10 (从倒数第3个开始) #}
    
    • ":-N"Represents from the beginning to the lastNelement before.
    {% set myNumbers = "[1,2,3,4,5,6,7,8,9,10]"|split:"," %}
    {{ myNumbers|slice:":-2"|join:"," }} {# 输出: 1,2,3,4,5,6,7,8 (从开头到倒数第2个之前) #}
    
    • Combine negative indices:
    {% set myNumbers = "[1,2,3,4,5,6,7,8,9,10]"|split:"," %}
    {{ myNumbers|slice:"-5:-2"|join:"," }} {# 输出: 6,7,8 (从倒数第5个开始,到倒数第2个之前) #}
    

The application of different data types

sliceThe filter is not limited to numeric arrays, it also applies to strings and other array types:

  • Truncate the string:

    {% set myString = "安企CMS:高效内容管理系统" %}
    {{ myString|slice:"0:5" }} {# 输出: 安企CMS: #}
    {{ myString|slice:"6:" }} {# 输出: 高效内容管理系统 #}
    {{ myString|slice:"-4:" }} {# 输出: 管理系统 #}
    

    It is worth mentioning that,sliceFilter handles Chinese string processing very intelligently, it cuts according to characters rather than bytes to ensure the integrity of Chinese content.

  • Cuts an array of objects (such as an article list): Assume you havearchiveListan article list from the tagarchives, and you only want to display the first 3 articles:

    {% archiveList archives with type="list" limit="10" %}
        {% for item in archives|slice:":3" %}
            <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
        {% endfor %}
    {% endarchiveList %}
    

Attention Points and **Practice

  • Index out of bounds:When you specifyfromortothe index is out of the actual length of the source data (array or string),sliceThe filter usually does not cause an error but returns the largest valid part it can capture. For example, when executing on an array with only 10 elements,slice:":99"It will still return all 10 elements.
  • Handling of null values: Use for empty arrays or empty strings.sliceFilter, the result is usually an empty array or an empty string.
  • Combined with other filters:sliceThe filter is often used in combination with other filters, for example,splitConvert a string to an array,joinRecombine the array into a string, making it more flexible to handle data.

MastersliceFilter, allowing you to handle various lists and string data more skillfully in AnQiCMS template development, thus achieving more refined and user-friendly content display effects.

Common Questions and Answers (FAQ)

  1. sliceFilter index starts from 0 or 1? sliceFilter index starts from0Starting the count, which means the index of the first element is 0, the second is 1, and so on.

  2. IffromortoDo the parameters exceed the actual length of the array or string, will the template report an error?In most cases, there will be no direct error. The security CMS ofsliceThe filter handles this situation gracefully, returning the largest valid portion it can capture. For example, if an array has only 5 elements, you tryslice:":10"It will still return all 5 elements.

  3. sliceFilter can be used for what types of data? sliceThe filter is mainly used for strings and arrays (or slices in Go language). It can effectively extract elements within the specified range of these data types.