English CMS (EnglishCMS) provides an intuitive and efficient way to display website content with its flexible template engine.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 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.
幸运的是,AnQiCMS 的模板语法提供了多种便捷的方式来帮助我们实现这一目标。
Smart uselength过滤器——通用且强大的计数器
In AnQiCMS templates, the most direct and commonly used method is to uselengthFilter.This filter is powerful, it can not only calculate the number of elements in a list (array, slice), but also get the character length of a string, and even apply it to key-value pairs (Map) to get the number of key-value pairs.
When you use in templatesarchiveListto get the article list, or throughcategoryListWhen getting the category list, they all return a set 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|lengththe filter, we can easily get the total number of elements in this list. No matter how this list is obtained througharchiveList/categoryList/pageListOrtagDataListsuch tags,lengthThe filter can accurately calculate the number of elements in it.
For variables of type (Map),lengthThe filter applies equally. For example, if you have set multiple custom parameters for a document in the background and are in non-sorting mode,sorted=false) to the same port of the AnQiCMS application instance (the default is usuallyarchiveParamsLabel 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,archiveParamsUsesorted=true(Default value),it returns an array of objects, each containingNameandValueproperties, at this time|lengththe calculation is still the number of objects in the array.
In addition, some tags (such asarchiveDetail/categoryDetail)'s some properties are lists themselves, for example, the document details ofImagesattributes (a set of image links) orContentTitlesattributes (a list of article content titles), and you can also use these properties directlylengthA filter to get the number of their elements:
English
{% if currentArticle|length > 0 %}
<p>本文共有 {{ currentArticle|length }} 张配图。</p>
{# 循环展示图片 #}
{% for image in currentArticle %}
<img src="{{ image }}" alt="图片描述" />
{% endfor %}
{% else %}