In AnQiCMS template development, we often need to iterate through document lists, such as througharchiveListlabel to get the content. In suchforIn the loop, understanding the total length of the current list is a very practical need, as it can help us implement some specific display logic, such as showing 'Article N/Total M' or determining if the list is empty.
The template engine of AnQi CMS adopts Django's syntax, providing a set of intuitive and powerful tags and filters to handle data. To getforin the loop,archivesThe total length of the document list, we can cleverly utilize the filter functions provided by the template engine.
Core Concept: List Data and Filters
In the AnQiCMS template,archiveListEnglish labels obtainedarchivesIt is usually an object of an array (or slice in Go language) type. The template engine provides an object namedlengthThe filter, can directly calculate the number of elements it contains.
lengthThe working principle of the filter is very simple: it receives a string, an array, or a key-value pair and returns the number of elements it contains.For an array or list, it returns the total number of elements.
How to getarchivesTotal length
The most direct and recommended way is toforBefore the loop starts, uselengthThe filter to calculatearchivesThe total length of the list, and store it in a new variable.
Use
archiveListGet the document list:Firstly, you need to usearchiveListTags to get your document list. For example, we get a list containing 10 articles:{% archiveList archives with type="list" limit="10" %} {# 接下来我们会在这个区域处理 archives 列表 #} {% endarchiveList %}Get the total length outside the loop and assign it to a variable:In
forto iteratearchivesBefore that, we can directly operate onarchivesthis variable tolengthThe filter. To use this total length value in subsequent templates, we usually combinesetLabel it and assign it to a new variable, for exampletotal_count.{% archiveList archives with type="list" limit="10" %} {% set total_count = archives|length %} {# 使用 length 过滤器获取总长度并赋值给 total_count #} {# ... 后续的 for 循环和展示逻辑 ... #} {% endarchiveList %}Here are the
archives|lengthThe total will be calculatedarchivesThe actual number of documents in the list.In
forUse the total length in the loop:Get the total lengthtotal_countAfter that, you can use it insideforthe loop flexibly. For example, combiningforloop.Counter(the current loop index, starting from 1) to display the position of the current document in the total list:{% archiveList archives with type="list" limit="10" %} {% set total_count = archives|length %} <p>本批次文章共 <strong>{{ total_count }}</strong> 篇。</p> <ul> {% for item in archives %} <li> <a href="{{ item.Link }}">{{ item.Title }}</a> <span> (第 {{ forloop.Counter }} 篇 / 共 {{ total_count }} 篇)</span> </li> {% empty %} <li>当前分类或条件下暂无文章。</li> {% endfor %} </ul> {% endarchiveList %}
Complete example code
The following is a complete example that combines retrieving document lists, calculating total length, and using it in a loop:
{# 假设这是一个文章列表页,我们想展示该分类下的文章及其总数 #}
{% archiveList articles with type="list" categoryId="1" limit="20" %} {# 获取 ID 为 1 的分类下最多 20 篇文章 #}
{% set total_articles_count = articles|length %} {# 计算文章列表的总长度 #}
{% if total_articles_count > 0 %} {# 判断列表是否为空 #}
<h2>最新文章 (共 {{ total_articles_count }} 篇)</h2>
<div class="article-list">
{% for article in articles %}
<div class="article-item">
<h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
<p class="meta">
发布时间:{{ stampToDate(article.CreatedTime, "2006-01-02") }}
浏览量:{{ article.Views }}
(第 {{ forloop.Counter }} 篇)
</p>
<p>{{ article.Description }}</p>
{% if article.Thumb %}
<img src="{{ article.Thumb }}" alt="{{ article.Title }}" class="article-thumb">
{% endif %}
</div>
{% endfor %}
</div>
{% else %}
<p>抱歉,当前分类下没有找到任何文章。</p>
{% endif %}
{% endarchiveList %}
In this way, you can easily use it in the security CMS template:forRetrieve the total length of the document list in the loop and flexibly display and handle the logic according to the actual requirements.This approach is in line with the design philosophy of the template engine, and ensures that the code is clear and maintainable.
Common Questions (FAQ)
Q1: IfarchivesThe list is empty,archives|lengthwhat will it return?A1: IfarchivesThe list is empty,archives|lengthwill return0. You can use this feature, combinedifwith statements (such as those in the examples),{% if total_articles_count > 0 %}) to determine if there is content in the list, thereby displaying different prompt messages, such as 'No content available'.
Q2:forloop.Counterandarchives|lengthWhat is the difference?A2:forloop.Counteris a special variable used infora loop, representingthe currentnumber of iterations in the loop, starting from1incrementing.archives|lengthis obtained by processing the entirearchiveslist applicationlengthfiltered by thetotalnumber of elements, which remains unchanged throughout the entire loop cycle. Simply put,forloop.CounterThis is the current position,archives|lengthIt is the total length.
Q3: BesidesarchivesI can uselengtha filter to get the length of other variables?A3: Yes,lengthThe filter is not only suitable forarchiveListobtainedarchivesList, it can also be used for any array (slice), string, or key-value pair (map) type of variable. For example, you can use{{ "Hello World"|length }}get the length of a string, or use{{ my_custom_array|length }}Get the length of a custom array.