How to implement reverse or custom sorting output of AnQiCMS's `for` loop?

Calendar 👁️ 60

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

Related articles

How to get the current loop item index or remaining item count in an article?

When using AnQi CMS for website content display, we often need to finely control each item in the list.This may include adding numbers to articles, highlighting the first or last item in a list, or applying different styles based on the position of the item in the list.The AnQi CMS template system uses a syntax similar to the Django template engine, where the key to handling list loops is the `for` loop tag.

2025-11-08

How to loop through and output the article list in AnQiCMS template?

AnQiCMS provides a powerful and flexible template system for website content management, among which, the loop traversal and output of the article list is an indispensable core function for building a dynamic website.Whether you need to display the latest articles, category lists, or implement advanced filtering and pagination, AnQiCMS template tags can help you complete it efficiently.

2025-11-08

How to customize the display layout of the article detail page, including images, descriptions, and custom parameters

In AnQi CMS, the display layout of the article detail page has extremely high flexibility, whether it is to show the core content of the article, beautiful pictures, or customized business parameters, the system provides an intuitive and powerful way to help you meet your personalized needs.This is due to the template engine of AnQiCMS based on Django-like syntax, which separates content from design, allowing even users without a strong programming background to adjust layouts through simple tags.### Understanding the Composition of Article Detail Page In AnQiCMS, articles (or products

2025-11-08

How to retrieve and display the article list in the template and control the number of articles displayed per page?

When using AnQiCMS to manage website content, it is an operational requirement to flexibly display article lists on the front-end page.Whether it is the latest dynamic on the homepage, the collection of articles under the category page, or the content aggregation page with pagination function, AnQiCMS provides powerful and easy-to-use template tags to help us achieve these functions.Today, let's delve into how to efficiently retrieve and display the article list in AnQiCMS templates and accurately control the number of articles displayed per page.### Core: `archiveList` tag

2025-11-08

How to display the default prompt information when the article list obtained through `archiveList` is empty?

When using Anqi CMS to build a website, we often need to display lists of articles, products, or other content.These lists are usually dynamically retrieved and rendered using such template tags as `archiveList`.}However, if a list is empty for various reasons (such as temporarily no content under a category, or empty search results), the user experience will be greatly reduced if the page is just empty.

2025-11-08

How to alternate different CSS classes or styles while looping through list items?

In web design, to enhance visual aesthetics and user experience, we often encounter situations where we need to alternate different CSS classes or styles for list items (such as article lists, product lists, navigation menus, etc.).For example, make the background color of odd and even rows different, or apply a unique layout style every few items.AnQiCMS (AnQiCMS) leverages its flexible template engine to provide a variety of simple and efficient methods to meet this requirement.

2025-11-08

How to safely output content containing HTML tags in AnQiCMS templates (e.g., article content)?

Guide to safely output HTML content in AnQiCMS templates When building a website with AnQiCMS, we often need to display content with rich formatting, such as article content, product descriptions, category details, or single-page content.This content is usually entered through the backend rich text editor, which will naturally contain HTML tags such as `<p>`, `<strong>`, `<em>`, `<a>`, and so on.How to correctly and safely output this content with HTML tags in the front-end template is a very important issue.If handled improperly

2025-11-08

How to use the `include` tag to reuse header, footer, and other common code segments in AnQiCMS templates?

Building website templates in AnQiCMS, efficient code reuse is the key to improving development speed and maintenance efficiency.Imagine, the header (Header) and footer (Footer) of a website appear almost on every page. If the same code is written on each page, it is not only time-consuming, but also, once a modification is needed, it has to be searched and updated on each page, which is undoubtedly a huge amount of work.

2025-11-08