AnQiCMS (AnQiCMS) with its flexible template engine makes the display of website content intuitive and efficient.In the operation of actual websites, we often need to dynamically adjust the page layout, display different content, and even perform some complex logical judgments based on the number of elements in a list or a key-value pair (Map).Mastering how to obtain the number of these elements in the template is an indispensable skill for advanced customization and optimization.
Luckily, AnQiCMS's template syntax provides various convenient ways to help us achieve this goal.
Smart uselengthFilter - general and powerful counter
In AnQiCMS templates, the most direct and commonly used method is to uselengthThe filter is powerful, it can not only calculate the number of elements in a list (array, slice), but also get the length of a string, and even apply it to key-value pairs (Map) to get the number of key-value pairs.
When you navigate through the templatearchiveListTo retrieve the list of articles, or throughcategoryListWhen getting the category list, they all return a collection containing multiple elements. For example, if we want to know how many articles are under a certain category, or how many products are displayed on the page, we can do it like this:
{% archiveList latestArticles with type="list" limit="5" %}
{# 此处是循环展示文章的实际内容,例如: #}
{# <div>{{ item.Title }}</div> #}
{% endarchiveList %}
{% if latestArticles|length > 0 %}
<p>最新文章共有:{{ latestArticles|length }} 篇。</p>
{% else %}
<p>暂无最新文章可显示。</p>
{% endif %}
In the above example,latestArticlesis a list of articles, through|lengtha filter, we can easily obtain the total number of elements in this list. No matter how this list is obtained througharchiveList/categoryList/pageListOrtagDataListetc tags,lengthThe filter can accurately calculate the number of elements within it.
For variables of the Map type,lengthThe filter applies equally. For example, if you have set multiple custom parameters for a document in the background, and in non-sorting mode,sorted=falsePassedarchiveParamsLabel acquisition, it may return a key-value pair structure. At this point, you can use|lengthTo calculate how many custom parameters there are:
{% archiveParams articleCustomParams with sorted=false %}
{# 假设这里获取到了一个键值对集合,包含如'作者'、'来源'等参数 #}
<p>这篇文档共设置了 {{ articleCustomParams|length }} 个自定义参数。</p>
{% endarchiveParams %}
It should be noted that if:archiveParamsUsesorted=true(Default value), it will return an array of objects, each containingNameandValueproperties, at this time|lengthIt still calculates the number of objects in the array.
In addition, some tags (such asarchiveDetail/categoryDetailSome properties are lists themselves, such as the document details'Imagesproperty (a set of image links) orContentTitlesproperty (a list of article content titles), you can also use these properties directlylengthFilter to get the number of their elements:
`twig {% archiveDetail currentArticle with name=“Images” %}
{% if currentArticle|length > 0 %}
<p>本文共有 {{ currentArticle|length }} 张配图。</p>
{# 循环展示图片 #}
{% for image in currentArticle %}
<img src="{{ image }}" alt="图片描述" />
{% endfor %}
{% else %}