In AnQiCMS template development, we often need to iterate over document lists, such as througharchiveListtag to get the content. In such aforIn a loop, knowing 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 of M' or determining if the list is empty.
The AnQi CMS template engine borrows the syntax of Django, providing a set of intuitive and powerful tags and filters to process data. To getforLooparchivesThe total length of the document list, we can take advantage of the filter functions provided by the template engine.
Core concept: List data and filters
Through the AnQiCMS template,archiveListget tags fromarchivesIt is usually an array (or a slice in Go language) object. The template engine provides a name for this type of array object calledlengthThe filter, which can directly calculate the number of elements it contains.
lengthThe principle of the filter's operation 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 obtainarchivestotal 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:First, you need to usearchiveListTag 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
forLoop througharchivesBefore that, we can directly apply toarchivesthis variable.lengthFilter. We usually combine it withsetlabel to assign it to a new variable, such astotal_count.{% archiveList archives with type="list" limit="10" %} {% set total_count = archives|length %} {# 使用 length 过滤器获取总长度并赋值给 total_count #} {# ... 后续的 for 循环和展示逻辑 ... #} {% endarchiveList %}Here
archives|lengthand calculatearchivesThe actual number of documents in the list.In
forUsing the total length in the loop:Obtained the total lengthtotal_countAfter that, you can use it inforThe loop internally. For example, combineforloop.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
This is a complete example that combines obtaining a document list, calculating the 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 Anqi CMS template.forRetrieve the total length of the document list in a loop and flexibly display and handle the logic according to actual needs.This approach conforms to the design philosophy of the template engine and ensures the clarity and maintainability of the code.
Frequently Asked Questions (FAQ)
Q1: IfarchivesThe list is empty,archives|lengthwhat will be returned?A1: IfarchivesThe list is empty,archives|lengthit will return0. You can use this feature, combined withif statements (such as those in the example){% if total_articles_count > 0 %}To determine whether the list has content, so as to display different prompt information, such as 'No content'.
Q2:forloop.Counterandarchives|lengthWhat is the difference?A2:forloop.CounterIs inforA special variable used inside the loop, it representscurrentThe number of iterations, starting from1starts increasing. Whilearchives|lengthis obtained by applying to the entirearchiveslistlengthfilter obtainedTotalThe number of elements, it remains unchanged throughout the loop cycle. In simple terms,forloop.Counteris the current position,archives|lengthis the total length.
Q3: BesidesarchivesI can uselengthDoes the filter get the length of other variables?A3: Yes,lengthThe filter is not only applicable to passing througharchiveListobtainedarchivesA list, 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 }}to get the length of a string, or use{{ my_custom_array|length }}Get the length of a custom array.