The order of display of the website content list is crucial for user experience and information communication.Whether it is news updates, product displays, or article archives, flexible control of the output order of lists is a common requirement in website operations.AnQiCMS provides a powerful and easy-to-use template engine, allowing us to easily control the arrangement of lists, including reverse order and sorting by specific rules.
AnQiCMS template engine basics
AnQiCMS's template system adopts the syntax style of the Django template engine, making the logic of content display clear and easy to understand through concise tags and variable declarations. When handling list data, forThe loop tag is our core tool, it allows us to traverse arrays or slices and other data collections.
BasicforThe loop structure is as follows:
{% for item in archives %}
{# 在这里处理每个列表项 item #}
<p>{{ item.Title }}</p>
{% empty %}
{# 如果列表为空,会显示这里的内容 #}
<p>暂时没有内容可供显示。</p>
{% endfor %}
archivesIt is usually a data collection obtained from the backend.itemIt is the current data item being processed in the loop. WhenarchivesWhen the collection is empty,{% empty %}and{% endfor %}The content between them will be output.
To achieve the reverse order output of the list
AnQiCMS'forThe loop has a very convenient built-inreversedKeywords can directly traverse the data set in reverse order.This is very useful for scenarios where you need to display the latest content first (such as time reversal) without any additional processing at the data source.
Just inforAfter adding the variable name of the loopreversedand it is done:
{% for item in archives reversed %}
<div class="list-item">
<h3>{{ item.Title }}</h3>
<p>发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
</div>
{% endfor %}
This code willarchivesoutput the data from the list in reverse order to the page.
Implement custom sorting output: strategy and practice
When we need to control the list sorting more finely, for example, sorting by views, ID, or other custom fields, AnQiCMS provides two main strategies.
Strategy one: utilizingforrepeatedlysortedAttribute (for simple scenarios)
forLoop also provides onesortedThe keyword can try to sort the data set in ascending order by default.Although the document states that the array is sorted by int, in practice, if the objects in the collection contain comparable default fields (such as ID), it may be sorted by these fields.Please note that this sorting is performed at the template level. If the data volume is large or a complex sorting based on a specific field is required, it is usually recommended to complete the sorting at the data retrieval stage.
{% for item in archives sorted %}
{# 这里的 item 会尝试按照某种默认规则升序排列 #}
<p>{{ item.Title }} - ID: {{ item.Id }}</p>
{% endfor %}
Strategy two: combinearchiveListlabel'sorderParameters (more powerful, recommended method)
For content lists (such as articles, products), the most powerful and flexible sorting method is to specify the sorting rule at the data source acquisition stage.archiveListTags providedorderParameters allow us to combine multiple fields and ascending or descending order