In AnQiCMS template development,sliceFilter is a very practical tool that allows us to perform precise extraction operations on strings (text) or arrays (lists).Master its usage, it can help you gain more flexible control when handling content display.
slicethe basic syntax of filters is{{obj|slice:"from:to"}}Here,objThis is the string or array variable you want to process,fromandtothen defines the start and end positions of the slice. This is very similar to the slice operation in Go language or other programming languages.fromIs included as the starting index,toIs excluded as the end index. If omittedfrom, then the default is to start cutting from the beginning of the data (i.e., index 0); if omittedto, then it cuts to the end of the data. In addition,sliceSupports negative indices, which allows you to calculate positions from the end of the data, for example-1Represents the last element.
Let's take a looksliceCommon application scenarios of filters in actual content operation.
Refine the display of list content
In website content operation, we often need to display list data, such as the latest articles, hot products, or selected content under a certain category. AnQiCMS providesarchiveListLabel to get list data, andsliceFilter can further refine the display of these lists, especially when there is a need for secondary filtering and display of the data obtained.
For example, you may havearchiveListGained a longer list of articles, but hope to display different parts of them in different areas of the page: display the latest 5 articles at the top, 5 recommended articles in the middle sidebar, and the latest 3 articles at the bottom.sliceThe filter is very useful.
Consider the following example, we first obtain a list containing multiple articles, then we useslicea filter to display different parts of them:
{# 假设 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 flexibly display different subsets of the same dataset on the front end without re-querying the database, which improves the reusability and efficiency of the template.
Generate content abstracts or extract text flexibly
When displaying article lists or product detail pages, to keep the layout neat or attract user clicks, we often need to show a brief summary of the content. Although AnQiCMS also provides such astruncatecharsUsed for text truncation specifically,sliceFilter as a more general data truncation tool, it can also handle this task, especially when it is necessary to accurately truncate the original string to a specific length, and you do 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 especially 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 %}
PasssliceFilter, AnQiCMS users can gain powerful control over strings and arrays at the template level.Whether it is the refined display of list content or the flexible generation of text summaries, it can be easily realized, thereby improving the presentation effect and user experience of the website.
Common Questions (FAQ)
1.slicefilters andarchiveListTagslimitWhat are the differences between parameters?
archiveListTagslimitThe 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 secondary cut of the data in memory at the template layer. In short,limitControl the amount of data 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), usesliceIt can avoid multiple database queries, improving efficiency and the flexibility of templates.
2.slicefilters andtruncatecharsHow should one choose when truncating text with a filter?
sliceThe filter provides "hard" character or array element slicing, which accurately slices from the specified position to the specified position without automatically adding ellipses and without considering the integrity of words. If you need to precisely control the slicing range or handle non-text arrays,slicethis is a better choice.
whiletruncatechars(or}truncatewordsThe filter is specifically designed for text summarization, and it will automatically add an ellipsis ("...") after reaching the specified length.truncatewordsThe integrity of the words will also be maintained as much as possible. When your main goal is to generate a friendly and informative text summary,truncatecharsusually