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

sliceWhat is a filter?

In simple terms,sliceThe filter is like a precise pair of scissors, which can 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"in the syntax form.

  • objrepresents the string or array variable you want to operate.
  • fromIs the starting index, indicating where to start the cut. Remember that in the AnQiCMS template system, the index usually starts from0.
  • toIt is 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"),表示从开头(索引0)一直截取到to指定的位置。
  • If omittedto(For example}]}obj|slice:"from:"),表示从from指定的位置一直截取到末尾。
  • You can also use negative indices, which can be very convenient in some cases. Negative indices indicate 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 have a wide range of applications in actual content operation:

  • Extracting article summaries or titlesEnglish translation: When the article title is too long or it is necessary to display a content abstract, it can easily truncate the first N characters.
  • Processing navigation or tag listIf the navigation menu or tab count is too many, only a part of the most commonly used or most important ones can be displayed.
  • Extract URL path fragments: Parse a specific part of a URL.
  • Display part of images in the image collectionFor example, only display the first three images in the product image collection.
  • Generate a brief description text: Extract a segment from the longer description as a preview.

Do it yourself:sliceUsage examples of filters

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

1. Extract String

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,轻松 #}

Processing Chinese characters:AnQiCMSsliceThe filter can correctly handle UTF-8 encoded Chinese characters; a Chinese character is treated as a unit rather than multiple bytes.

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

2. Extracting arrays or lists

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

Extract part of the array:

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

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

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

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

Some tips

  1. Index from0Start: This is a common convention in programming languages, getting used to it can avoid many errors.
  2. toIndex does not include:slice:"from:to"The actual extraction isfromtoto-1elements within the range.
  3. Out of bounds handlingIffromortoThe index is out of the actual length of the string or array,sliceThe filter usually does not report errors but intelligently returns the available part or empty values, which is very friendly during template rendering.
  4. Combined with other filtersAs shown in the above example,sliceFrequently withsplitSplit a string into an array andjoinUse the filter together with an array to concatenate strings, to achieve more complex text processing logic.

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


Common Questions (FAQ)

1.slicefilters andtruncatecharsWhat are the differences between filters? sliceThe filter extracts elements from strings or arrays strictly by index position, it does not automatically add an ellipsis after the cut (such as...), nor does it consider the integrity of the word.truncatecharsThe filter is mainly used for strings, it will automatically add an ellipsis after reaching a specified length and will try to avoid cutting off in the middle of a word to maintain the readability of the text. If you need to control the range of truncation precisely and do not want to add an ellipsis automatically, please usesliceIf you want to generate an ellipsis summary,truncatecharsortruncatewordsthis would be a better choice.