AnQiCMS development: Application scenarios of the `slice` filter in handling list pagination or displaying summaries?

Calendar 81

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

Related articles

Batch processing content: How to use `slice` to dynamically truncate strings of different lengths in a loop?

In website content operation, we often encounter situations where we need to display content in bulk, such as article lists, product introduction summaries, etc.In order to ensure the beauty of the page and the clarity of information presentation, we often need to truncate the titles or descriptions of these contents so that they can display different lengths in different areas or under different conditions.AnQiCMS provides powerful template tags and filters, making this requirement simple and flexible.

2025-11-08

How to assign the result of a `slice` filter to a new variable for subsequent use?

In AnQiCMS template development, we often need to process displayed data in various ways, such as truncating a segment of text, or extracting specific items from a list.The `slice` filter is born for this, it can help us accurately slice a part of the string or array.However, it is not enough to simply extract data, how to conveniently assign the extracted results to a new variable for repeated use in the subsequent parts of the template or for more complex logic processing, this is the key to improving template development efficiency and code readability.### `slice` filter basics

2025-11-08

How to avoid Chinese garbled or incomplete truncation caused by the `slice` operation in AnQiCMS template?

When using AnQiCMS for website content development, you may sometimes encounter the situation where, when using the `slice` filter in the template to process Chinese content, the content is truncated and incomplete, which undoubtedly affects user experience and the accuracy of content presentation.Even though the underlying design of the system aims to avoid garbled characters, it is particularly important to understand its mechanism due to the characteristics of Chinese multi-byte characters and the expected display effects.

2025-11-08

Does Go template's `slice` filter support negative indices? How to use it to slice in reverse order?

In AnQiCMS, templates are the key to building dynamic content, allowing us to flexibly display data.In the process of template development, it is often encountered that string or list (usually slice in Go template, which can be understood as an array) truncation operations need to be performed.AnQiCMS provides a powerful `slice` filter to handle such needs.### Deeply understand the basic usage of `slice` filter The `slice` filter is mainly used to extract specified elements from strings or lists

2025-11-08

Template optimization: What is the difference between the `slice` filter and the `truncatechars` filter? When should you choose which one?

In Anqi CMS template development, we often need to control or truncate the displayed content to adapt to different layout requirements and optimize the user experience.At this point, the `slice` filter and `truncatechars` filter have become our commonly used tools.Although they can all achieve text slicing, there is a significant difference in functional focus and application scenarios.Understanding these differences can help us choose the appropriate filters more accurately, making the template code more efficient and the page display more reasonable.

2025-11-08

What result will the `slice` filter return when operating on an empty string or array?

In AnQi CMS template development, we often need to perform fine-grained control and processing of content.Among them, the `slice` filter is a very practical tool that allows us to extract the specified part from a string or array (usually referred to as a slice in Go language).This feature is very convenient when displaying article summaries, partial elements in pagination lists, or processing dynamic data.

2025-11-08

How to use the `slice` filter to get all elements of the array except the first and last?

When managing and displaying content in Anqi CMS, the flexibility of the template is the key to improving website operation efficiency.The built-in Django template engine syntax provides many practical filters, allowing us to handle various data in a concise and efficient manner.Today, let's delve deeply into a very practical filter——`slice`, which can help us accurately cut a specific part of an array (or called slice in the Go language environment), especially on how to get all elements except the first and last one.Content display is the core of website operation, whether it is an article list

2025-11-08

What is the impact of the `slice` filter on performance when handling large strings or large arrays?

Performance considerations for the `slice` filter in AnqiCMS when handling large strings or large arrays The `slice` filter is a very convenient feature in the AnqiCMS template engine, allowing users to extract specified length data segments from strings or arrays.Whether it is to extract an abstract from a long article or to select a portion of elements from a large data list, `slice` can provide flexible control.

2025-11-08