During the development of AnQi CMS templates, we often need to flexibly handle the data displayed on the page, especially when the data is presented in the form of a list or sequence.Imagine you are designing a product list page, needing to pick out the top 5 most popular products from an array of dozens of products to display at the top of the page;Or, you may be on an article detail page, where you need to truncate a long string as a summary.How to accurately slice an array or specify a range of elements in a string in AnQiCMS template has become a very practical skill.

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

UnderstandingsliceBasic syntax of filter

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

Here:

  • objIs the source data you want to extract, which can be an array (or a slice in Go language), a string, and other indexable data types.
  • "from:to"Is a string used to specify the range of truncation.
    • fromRepresents the starting index (including the element at this position).
    • toRepresents the ending index (not including the element at this position).
  • It should be noted that, like most programming languages, indexing starts from0and counting.

For example, if you have a list of numbers[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]an array, and you want to get the 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.

Flexible applicationfromandtoParameter

slicethe filter to specifyfromandtoParameters 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:"),it will start from the specifiedfromindex and continue to the end of the collection.

    {% set myNumbers = "[1,2,3,4,5,6,7,8,9,10]"|split:"," %}
    {{ myNumbers|slice:"7:"|join:"," }} {# 输出: 8,9,10 #}
    
  3. using negative indices:sliceThe filter also supports using negative indices, which makes it very convenient to count from the end of the collection.

    • "-N:"Means from theNelement 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"Means from the beginning to theNBefore an element.
    {% 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个之前) #}
    
    • Combined with 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个之前) #}
    

Application to different data types.

sliceFilters are not limited to numeric arrays; they also apply to strings and other array types:

  • String slicing.:

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

    It is worth mentioning,sliceThe filter is very intelligent when processing Chinese string, it cuts according to characters instead of bytes, ensuring the integrity of Chinese content.

  • Truncate object array (such as article list)Assuming you have passedarchiveLista list of articles labeledarchivesand you want to display only 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 %}
    

Cautionary notes and **practice

  • Index out of rangeWhen you specify:fromortoThe 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 performing on an array with only 10 elements,slice:":99"It will still return all 10 elements.
  • Handling empty valuesUsed for empty arrays or empty strings:sliceFilter, the result will usually be an empty array or an empty string.
  • Combined with other filters:sliceFilters are often combined with other filters, for examplesplitConvert a string to an array,joinRecombine the array into a string, for more flexible data processing.

MastersliceA filter that allows you to handle various lists and string data more easily in AnQiCMS template development, thereby achieving a more refined and user-friendly content display effect.

Frequently Asked Questions (FAQ)

  1. sliceIs the index in the filter counted from 0 or from 1? sliceThe index in the filter is from0Counting begins, which means the index of the first element is 0, the second is 1, and so on.

  2. IffromortoWill the template error if the parameter exceeds the actual length of the array or string?In most cases, it will not report an error directly. Anqi CMS'ssliceThe filter will elegantly handle this situation, it will return the maximum valid part it can capture. For example, if an array only has 5 elements, you tryslice:":10"It will still return all 5 elements.

  3. sliceWhat types of data can the filter be used for? 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.