The display order of website content lists is crucial for user experience and information delivery.Whether it is news updates, product displays, or article archives, flexibly controlling the output order of the list 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 follows the syntax style of Django template engine, making the logic of content display clear and easy to understand through concise tags and variable declarations. When handling list data,forLoop tags are our core tool, allowing us to iterate over 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 collection of data obtained from the backend,itemThen is the current data item being processed in the loop. WhenarchivesThe collection is empty,{% empty %}and{% endfor %}The content between them will be output.

Implementation of list reverse output

AnQiCMSforThe loop has a very convenient built-inreversedKeyword, you can directly traverse and output the data set in reverse order.This is very useful for scenarios where the latest content needs to be displayed first (e.g., time in reverse order) without any additional processing at the data source level.

Just need toforThe variable name is appended to the loop.reversedas follows:

{% for item in archives reversed %}
    <div class="list-item">
        <h3>{{ item.Title }}</h3>
        <p>发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
    </div>
{% endfor %}

The above code will.archivesThe data in the list is output in reverse order from the last element to the page.

Implement custom sorting output: strategies and practices.

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: utilizingforLoopingsortedProperty (for simple scenarios)

forLoop also providessortedThe keyword, it can try to sort the data set in ascending order by default.Although the documentation 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 according to 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: combinedarchiveListTagsorderParameters (more powerful, recommended method)

For content lists (such as articles, products), the most powerful and flexible sorting method is to specify the sorting rules at the data source acquisition stage.archiveListTags provideorderParameter, allows us to combine multiple fields in ascending and descending order