In AnQiCMS template development,sliceThe filter is a very practical tool that allows us to perform precise extraction operations on strings (text) or arrays (lists).Mastering its usage can help you gain more flexible control when handling content display.

sliceThe basic syntax of the filter is{{obj|slice:"from:to"}}. Here,objIt is the string or array variable you want to process, andfromandtothen defines the start and end positions. This is very similar to the slice operation in Go language or other programming languages.fromIs included as the starting index,toIs an excluded end index. If omittedfromThen it defaults to starting from the beginning of the data (i.e., index 0); if omittedtoThen it is cut off at the end of the data. In addition,sliceIt supports negative indices, which allows you to start counting positions from the end of the data, for example-1It represents the first element from the end.

Let's take a look atsliceFilter is a common application scenario in actual content operation.

精细化控制列表内容的展示

In website content operation, we often need to display list data, such as the latest articles, popular products, or selected content under a certain category. AnQiCMS providesarchiveListTags are used to retrieve list data, whilesliceFilters can further refine the display of these lists, especially when it is necessary to perform secondary filtering and display on the data obtained.

For example, you may pass througharchiveListGot a long list of articles, but hope to display different parts of it in different areas of the page: display the latest 5 articles at the top, display 5 "recommended articles" in the sidebar in the middle, and display the latest 3 articles at the bottom. At this point,sliceThe filter is very useful.

Consider the following example, first we get a list of multiple articles, then we useslicethe filter to display their different parts separately:

{# 假设 archives 是通过 archiveList 获取的全部文章列表,例如获取了20篇文章 #}
{% archiveList allArticles with type="list" limit="20" %}

<h3>最新推荐(前5条)</h3>
<ul>
{% for item in allArticles|slice:":5" %} {# 截取前5条文章 #}
    <li><a href="{{item.Link}}">{{item.Title}}</a></li>
{% endfor %}
</ul>

<h3>热门文章(第6到第10条)</h3>
<ul>
{% for item in allArticles|slice:"5:10" %} {# 截取索引5到9的文章,即第6到第10条 #}
    <li><a href="{{item.Link}}">{{item.Title}}</a></li>
{% endfor %}
</ul>

<h3>近期热门(最后3条)</h3>
<ul>
{% for item in allArticles|slice:"-3:" %} {# 截取最后3条文章 #}
    <li><a href="{{item.Link}}">{{item.Title}}</a></li>
{% endfor %}
</ul>

{% endarchiveList %}

This usage allows you to display different subsets of the same dataset on the front end without having to query the database again, which improves the reusability and efficiency of the template.

Flexible in generating content summaries or extracting text

When displaying an article list or product detail page, in order to keep the layout neat or attract user clicks, we often need to display a brief summary of the content. Although AnQiCMS also provides suchtruncatecharsA filter specifically used for text truncation, butsliceThe filter, as a more general data truncation tool, can also handle this task, especially when it is necessary to truncate the original string to a specific length accurately and not want it to automatically add ellipses (although we usually add them manually later).

For example, you may want to limit the title or description of the article to a fixed number of characters,sliceCan be put to use. This is particularly common in card layouts, ensuring that all card titles or descriptions are of consistent length.

{% archiveList articles with type="list" limit="5" %}
{% for item in articles %}
    <div class="article-card">
        {# 截取文章标题的前15个字符,并手动添加省略号 #}
        <h4><a href="{{item.Link}}">{{ item.Title|slice:":15" }}{% if item.Title|length > 15 %}...{% endif %}</a></h4>
        {# 截取文章描述的前50个字符作为摘要,并去除HTML标签 #}
        <p>{{ item.Description|striptags|slice:":50" }}{% if item.Description|striptags|length > 50 %}...{% endif %}</p>
        <a href="{{item.Link}}" class="read-more">阅读更多</a>
    </div>
{% endfor %}
{% endarchiveList %}

BysliceThe filter, AnQiCMS users can gain powerful control over strings and arrays at the template level.No matter the refined display of list content or the flexible generation of text summaries, it can be easily achieved, thereby enhancing the presentation effect and user experience of the website.


Frequently Asked Questions (FAQ)

1.sliceFilters andarchiveListlabel'slimitWhat is the difference between parameters?

archiveListlabel'slimitThe parameter is a data restriction at the database level, it tells the system to only query and return a specified number of contents from the database.sliceThe filter is applied to the data beingarchiveListAfter obtaining the template, perform a second cut on the data in the memory at the template level. In short,limitIt controls how much data is obtained,sliceControls which part of the retrieved data is displayed. When you need to obtain a larger dataset (such as 20 articles) but display different subsets in different areas of the page (such as the first 5, the middle 5), usesliceCan avoid multiple database queries, improve efficiency and the flexibility of templates.

2.sliceFilters andtruncatecharsHow should the filter be selected when truncating text?

sliceThe filter provides a "hard" character or array element truncation, which truncates from the specified position to the specified position precisely, without automatically adding ellipses and without considering the integrity of words. If you need to control the truncation range precisely, or handle non-text arrays,sliceIs a better choice.

Andtruncatechars(ortruncatewordsThe filter is specifically designed for text summarization, it will automatically add an ellipsis ("...") after truncating to a specified length, andtruncatewordsTry to maintain the integrity of words. When your main goal is to generate friendly and suggestive text summaries,truncatecharsusually