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

Calendar 👁️ 71

AnQiCMS provides a powerful and flexible template system for website content management, among which, looping through and outputting the article list is an indispensable core function for building dynamic websites.No matter whether you need to display the latest articles, category lists, or implement advanced filtering and pagination, AnQiCMS template tags can help you complete tasks efficiently.

Core function: understandarchiveListTag

The key to obtaining and displaying the article list in the AnQiCMS template lies in usingarchiveListThe tag is a powerful data extraction tool that allows you to filter, sort, and limit the output of articles based on various conditions.

archiveListThe basic syntax structure of the tag is{% archiveList 变量名称 with 参数... %}...{% endarchiveList %}. You need to specify a variable name for the list of articles you have obtained (for examplearchives), followed bywithkeywords are used to pass various filtering parameters.

CommonarchiveListThe parameters include:

  • moduleId: Specify the content model ID of the article you want to retrieve. For example, if you want to get a list of ordinary articles, you would usually setmoduleId="1"(The default ID of the article model); it may be for a product listmoduleId="2".
  • categoryId: Filter by the category ID of the article. You can specify a single category ID, or use multiple IDs separated by commas, for examplecategoryId="1,3,5"To get articles from multiple categories. If you want to get articles from the current page category, usually you do not need to specify this parameter, the system will intelligently judge; but if you explicitly do not want to read the current category, you can set it tocategoryId="0".
  • limit: Controls the number of articles displayed. For examplelimit="10"It will only display 10 articles. It also supportsoffsetmode, specifying the starting and number of articles to retrieve by separating with commas, such aslimit="2,10"Indicates fetching 10 items starting from the 3rd article.
  • type: Defines the type of the list, which is particularly important for pagination functionality.type="list"Will be sorted according tolimitThe number of items specified by the parameter will be output directly; whereastype="page"It means you want to enable the pagination feature, which will be followed by the pagination tagspaginationused together.
  • order: Set the sorting rules for the articles. Common sorting methods include:order="id desc"(Sorted by ID in descending order, i.e., the most recent release),order="views desc"(Sorted by views in descending order, i.e., the most popular),order="sort desc"(Sorted by custom sorting on the backend, the default value).
  • q: Used for keyword search. Whentype="page"生效,可以指定一个搜索关键词来过滤文章列表,例如q="SEO优化"。如果 URL 中包含q=关键词的查询参数,系统也会自动识别并应用。
  • siteIdIn a multi-site environment, if you need to retrieve data from other sites, you can specifysiteIdto achieve.

Loop traversal and content output: Gradually implement the article list

When we go througharchiveListAfter the tag successfully retrieves the article data set, the next step is to use{% for ... in ... %}Loop through the tags and display these data one by one on the page.

Assuming we want to display the latest article list on the homepage, each article includes a title, link, description, thumbnail, and shows its category and publish date. Here is an example code to implement this function:

<div class="latest-articles">
    <h2>最新文章</h2>
    <ul>
        {% archiveList archives with moduleId="1" type="list" order="id desc" limit="10" %}
            {% for item in archives %}
            <li class="article-item">
                <a href="{{ item.Link }}" class="article-link">
                    {% if item.Thumb %}
                        <img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="article-thumb">
                    {% endif %}
                    <div class="article-info">
                        <h3 class="article-title">{{ item.Title }}</h3>
                        <p class="article-description">{{ item.Description }}</p>
                        <div class="article-meta">
                            <span class="article-category">分类:{% categoryDetail with name="Title" id=item.CategoryId %}</span>
                            <span class="article-date">发布于:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
                            <span class="article-views">浏览量:{{ item.Views }}</span>
                        </div>
                    </div>
                </a>
            </li>
            {% empty %}
            <li class="no-content">
                目前还没有任何文章发布,敬请期待!
            </li>
            {% endfor %}
        {% endarchiveList %}
    </ul>
</div>

In this example:

  1. We first usearchiveListLabel, set the article model (moduleId="1") under the latest 10 articles (order="id desc" limit="10") assign toarchivesVariable.
  2. Then,{% for item in archives %}The loop will iterate overarchiveseach article, and assign the current article's data toitemVariable.
  3. Within the loop, we can access the various properties of the article byitem.字段名for exampleitem.TitleGet the title,item.LinkGet the link,item.DescriptionGet an overview.
  4. item.Thumbthe thumbnail path provided for the article, we use it as<img>label'ssrcAttribute, and combine{% if item.Thumb %}Make a judgment to avoid a placeholder image appearing when there is no thumbnail.
  5. Article publish dateitem.CreatedTimeIt is a timestamp, we use{{ stampToDate(item.CreatedTime, "2006-01-02") }}The label formats it into a readable date string (AnQiCMS uses the Go language date formatting standard, '2006-01-02' is a commonly used format template).
  6. To obtain the name of the category to which the article belongs, we utilizeditem.CategoryIdand passed it as a parameter to{% categoryDetail with name="Title" id=item.CategoryId %}the tag, so that the category name is displayed in the article list.
  7. Finally,{% empty %}Tags provide a graceful way to handle the case where the article list is empty, it willarchivesdisplay a preset message when there is no content instead of a blank page.

Advanced Application: List Filtering and Pagination

For scenarios requiring more interactivity and data volume, AnQiCMS also provides a comprehensive solution.

If you want users to be able to browse multi-page articles instead of loading all the content at once, you canarchiveListoftypethe parameter totype="page". At this point, you need additional coordinationpaginationtags to generate pagination navigation:

This is a template for the article list page article/list.html

{% archiveList archives with moduleId="1" type="page" order="id desc" limit="10" %}
    {# 循环输出文章列表内容,结构与上面示例类似 #}
    {% for item in archives %}
        <li class="article-item">
            <a href="{{ item.Link }}" class="article-link">
                {% if item.Thumb %}
                    <img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="article-thumb">
                {% endif %}
                <div class="article-info">
                    <h3 class="article-title">{{ item.Title }}</h3>
                    <p class="article-description">{{ item.Description }}</p>
                    <div class="article-meta">
                        <span class="article-category">分类:{% categoryDetail with name="Title" id=item.CategoryId %}</span>
                        <span class="article-date">发布于:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
                        <span class="article-views">浏览量:{{ item.Views }}</span>
                    </div>
                </div>
            </a>
        </li>
    {% empty %}
        <li class="no-content">
            目前还没有任何文章发布,敬请期待!
        </li>
    {% endfor %}
{% endarchiveList %}

{# 分页导航区域 #}
<div class="pagination-nav">
    {% pagination pages with show="5" %}
        <ul>
            <li class="page-item {% if pages.FirstPage.IsCurrent

Related articles

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 does AnQiCMS implement the switching and display of multilingual content to enhance the international user experience?

In today's globalized digital world, a website that can present its content in multiple languages undoubtedly can greatly expand its user base and significantly enhance the user experience.AnQiCMS as a powerful content management system, fully considers the needs of international operation, and provides a flexible and efficient mechanism to switch and display multilingual content.### Why is multilingual content so important? Imagine a user from Japan visiting your website. If they can read product descriptions or service instructions in their native language, their trust and engagement will immediately increase

2025-11-08

How to utilize the flexible content model of AnQiCMS to customize unique display fields for different types of content (such as articles, products)?

AnQiCMS, this is a content management system driven by the Go language, which performs very well in content management, especially its flexible content model function.As website operators, we often need to publish various types of content, such as detailed product introductions, professional technical articles, vivid case studies, and so on.Each content has its unique display requirements and information structure.Traditional content management systems may require cumbersome secondary development to meet these differences, but AnQiCMS's flexible content model is exactly born to solve this pain point, allowing us to easily customize

2025-11-08

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 implement reverse or custom sorting output of AnQiCMS's `for` loop?

The order of website content listings is crucial for user experience and information delivery.Whether it is news updates, product displays, or article archives, flexibly controlling the output order of lists is a common demand in website operation.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 The AnQiCMS template system adopts the syntax style of the Django template engine, using concise tags and variable declarations

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