In the process of making AnQiCMS templates, it is an indispensable skill to flexibly handle strings and arrays. Whether it is to display the abstract of an article or to extract part of the elements from a list,sliceFilters can provide powerful and convenient assistance. It allows you to extract specific ranges of characters from strings or select elements from arrays at specified positions, making content display more accurate and diverse.

sliceWhat is a filter?

In simple terms,sliceThe filter acts like a precise pair of scissors, able to cut out the part you need from a piece of text (string) or a set of data (array, list) according to the specified start and end positions.Its core function is to "cut" or "slice".

sliceHow does the filter work?

sliceThe filter usesobj|slice:"from:to"syntax format.

  • objrepresents the string or array variable you want to operate on.
  • fromIs the starting index, indicating where to start cutting. Remember that in the AnQiCMS template system, the index usually starts from0start counting.
  • toIs the end index, indicating where to stop cutting. It should be noted that,toThe element pointed toWill not be includedIn the cutting result.

You can also omitfromorto:

  • If omittedfrom(For example)obj|slice:":to") indicates from the beginning (index 0) up totothe specified position.
  • If omittedto(For example)obj|slice:"from:") indicates fromfromthe specified position up to the end.
  • You can also use negative indices, which are very convenient in some cases. Negative indices represent counting from the end of a string or an array. For example,-1represents the last element,-2representing the second-to-last element.

Where can it be used?

sliceFilters are widely used in practical content operations:

  • Extracting the abstract or title of an article: When the article title is too long or needs to display a content summary, it can easily extract the first N characters.
  • Handle navigation or tag listsIf the number of navigation menus or tags is too many, only a part of the most commonly used or most important ones can be displayed.
  • URL path segment extractionParsing a specific part of a particular URL.
  • Display some images from the image collectionFor example, only display the first three images from the product image collection.
  • Generate a brief description text: Extract a section from the longer description as a preview.

Practice:sliceExample of filter usage

We understand through some specific examplessliceThe usage of filters. Suppose we have a string"欢迎使用安企CMS,轻松管理您的内容!"and an array["苹果", "香蕉", "橘子", "葡萄", "西瓜", "芒果"].

1. Extracting strings

Basic Extraction (fromfromtoto):

{# 截取字符串的第 1 到第 5 个字符(索引从0开始,所以是索引0到4) #}
{{ "欢迎使用安企CMS,轻松管理您的内容!"|slice:"0:5" }}
{# 显示结果:欢迎使用安企 #}

{# 截取字符串的第 5 到第 9 个字符(索引4到8) #}
{{ "欢迎使用安企CMS,轻松管理您的内容!"|slice:"4:9" }}
{# 显示结果:安企CMS, #}

Extract from the beginning to a specified position:

{# 截取前 7 个字符(索引0到6) #}
{{ "欢迎使用安企CMS,轻松管理您的内容!"|slice:":7" }}
{# 显示结果:欢迎使用安企CMS #}

Extract from a specified position to the end:

{# 从第 11 个字符(索引10)开始截取到末尾 #}
{{ "欢迎使用安企CMS,轻松管理您的内容!"|slice:"10:" }}
{# 显示结果:轻松管理您的内容! #}

Using negative indices (counting from the end):

{# 截取最后 5 个字符 #}
{{ "欢迎使用安企CMS,轻松管理您的内容!"|slice:"-5:" }}
{# 显示结果:您的内容! #}

{# 截取除了最后 8 个字符之外的所有内容 #}
{{ "欢迎使用安企CMS,轻松管理您的内容!"|slice:":-8" }}
{# 显示结果:欢迎使用安企CMS,轻松 #}

Handling Chinese characters:AnQiCMS'sliceThe filter can correctly handle UTF-8 encoded Chinese characters, where a Chinese character is treated as a unit rather than multiple bytes.

{{ "你好世界"|slice:"1:3" }}
{# 显示结果:好世 #}

2. Slice an array or list

When we need to process an array, we usually combinesplitThe filter converts a string to an array or directly operates on an existing array variable.

To extract a portion of the array elements:

{% set fruits = ["苹果", "香蕉", "橘子", "葡萄", "西瓜", "芒果"] %}

{# 截取数组中索引 1 到 4 的元素(香蕉、橘子、葡萄) #}
{{ fruits|slice:"1:4"|join:"-" }}
{# 显示结果:香蕉-橘子-葡萄 #}

{# 截取数组中的前 3 个元素 #}
{{ fruits|slice:":3"|join:", " }}
{# 显示结果:苹果, 香蕉, 橘子 #}

{# 截取数组中的最后 2 个元素 #}
{{ fruits|slice:"-2:"|join:" 和 " }}
{# 显示结果:西瓜 和 芒果 #}

Some tips

  1. Index from0Start: It is a common convention in programming languages, getting accustomed to it can help avoid many mistakes.
  2. toIndex does not include:slice:"from:to"The actual extracted isfromtoto-1within the range of elements.
  3. Out of boundary handlingIf:fromortoThe index exceeds the actual length of the string or array,sliceThe filter usually does not report an error but intelligently returns the available part or an empty value, which is very friendly during template rendering.
  4. Combine with other filters: As shown in the above example,sliceoften withsplitSplit strings into arrays andjoinUse the filter to concatenate arrays into strings, to achieve more complex text processing logic.

Through proficiencysliceFilter, you will be able to display your content more efficiently and flexibly in AnQiCMS, enhancing the user experience of the website.


Frequently Asked Questions (FAQ)

1.sliceFilters andtruncatecharsWhat are the differences between filters? sliceThe filter is purely for slicing strings or array elements by index, it will not automatically add an ellipsis (such as...), nor will it consider the integrity of the word.truncatecharsThe filter is mainly used for strings, it will automatically add an ellipsis after truncating to a specified length, and will try to avoid breaking a word in the middle to maintain readability. If you need to precisely control the truncation range and do not want to automatically add an ellipsis, please usesliceIf you are looking to generate an ellipsis summary,truncatecharsortruncatewordsWould be a better choice.