How to get the number of elements in a list or key-value pair in AnQiCMS template?

Calendar 👁️ 67

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 %}

Related articles

What is the difference between the `length` filter and the `length_is` filter in terms of content length judgment?

In AnQiCMS template design, we often need to judge or obtain the length of content in order to flexibly control the display of content.The `length` filter and `length_is` filter are born for this purpose.Although they are all related to 'length', in actual use, their functions and application scenarios are obviously different.Understanding these subtle differences can help us build templates more efficiently and accurately.### `length` filter: Content length 'counter' `length`

2025-11-08

How to count the character length of article titles, descriptions, or custom fields?

When managing content on AnQi CMS, we often need to pay attention to the character length of article titles, descriptions, or custom fields.This concerns not only SEO optimization, ensuring that the title and description are in line with the practices of search engines, but also affects the reading experience of users on the search results page or within the website's internal list.How can you easily count the character length of this content in Anqi CMS templates?The Anqi CMS built-in template engine provides many practical filters (filters), among which the `length` filter is our tool for counting character length

2025-11-08

What is the speciality of the `index` filter for Chinese position calculation when processing multi-language content?

## A clever approach: The unique features of `index` filter and Chinese position calculation in AnQi CMS multilingual content When managing multilingual content with AnQi CMS, the flexible use of its powerful template engine and rich filters can greatly improve content operation efficiency.Among them, the `index` filter is a very practical tool that helps us quickly locate the first occurrence of a specific substring in a string.However, when our content involves Chinese characters, the `index` filter has some unique behavior in position calculation.

2025-11-08

How to get the first occurrence position of a keyword in a string in AnQiCMS

In content operation, we often need to fine-tune and manage the text content on the website.In order to content review, dynamic display, or SEO optimization, sometimes we need to know the position of the first occurrence of a specific keyword in a text.AnQiCMS (AnQiCMS) is an efficient content management system that provides a convenient way to help us meet this need.Understanding the need: Why do we need to find the position of keywords?Understanding the position of keywords in strings is of great practical value in the process of content publishing and maintenance

2025-11-08

How to split a string containing multiple tags (such as “SEO, keywords, optimization”) into an array?

In the daily operation of Anqi CMS, we often encounter scenarios where we need to flexibly display some structured information in the content.For example, when setting keywords for articles or products, we may enter a string containing multiple tags in the "document keywords" field in the backend, such as Such strings are convenient to enter, but when displayed on the front end, we usually hope to display these tags separately, even to convert them into independent, clickable elements.How to convert such a comma-separated tag string

2025-11-08

What is the difference between the `split` filter and the `fields` filter when splitting strings by spaces?

During the template creation process in AnQi CMS, we often encounter scenarios where we need to split strings, such as extracting keywords from a description or parsing tag strings into independent words.AnQi CMS provides the practical filters `split` and `fields`. Although they can all split strings into arrays, there are subtle and crucial differences in their working methods and application scenarios when splitting strings by spaces.### `split` filter: The versatile separator expert `split` filter

2025-11-08

How to split a Chinese sentence into an array of single characters in AnQiCMS template?

In website content operation, sometimes we need to control Chinese text more finely, such as splitting a sentence into individual characters for display, or applying different styles, animation effects to each character.This need is particularly common when creating some special UI effects, interactive content, or even text games.How does the template system help us achieve this function when using AnQiCMS to build a website?AnQi CMS uses a template engine syntax similar to Django, which provides rich built-in tags and filters for content display

2025-11-08

How to concatenate a dynamically generated array (such as an article ID list) into a URL parameter string?

In website operation and content management, we often encounter such needs: to concatenate a group of dynamically generated data (such as multiple article IDs, tag IDs, etc.) in a specific format as URL parameters, in order to filter, perform batch operations, or display more accurate content.For example, the user may have selected multiple articles for comparison on the frontend, or the backend may need to generate a URL to filter articles within a specific ID range.AnQiCMS (AnQiCMS) leverages its powerful backend performance based on the Go language and flexible Django-style template engine

2025-11-08