How to loop through a document list in an Anqi CMS template and output custom parameters for each document?

Calendar 👁️ 64

AnQiCMS provides powerful template customization capabilities, allowing us to flexibly display various content according to the actual needs of the website.When we not only need to loop through the document list but also want to output unique custom parameters for each document, AnQiCMS template tags can help us achieve this very well.This is particularly important for a content model with diverse attributes for displaying product details, real estate information, job openings, etc.

The first step: master the loop display of document lists

First, we need to usearchiveListTag to get and loop display the document list. This tag is the core tool used for content list display in AnQiCMS.

We usually use templates to{% archiveList %}tags to specify the document collection to be retrieved, and{% for item in archives %}use such a loop structure to iterate over each document.

For example, if we want to get a list of articles under a certain category, we can write the code like this:

{% archiveList archives with categoryId="1" type="list" limit="10" %}
    {% for item in archives %}
        <div class="document-item">
            <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
            <p>发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
            <p>浏览量:{{ item.Views }}</p>
            <p>{{ item.Description }}</p>
            {# 在这里我们将展示自定义参数 #}
        </div>
    {% empty %}
        <p>当前分类下没有文档。</p>
    {% endfor %}
{% endarchiveList %}

In this code block:

  • archivesis the custom variable name, which will containarchiveListthe set of document data returned.
  • categoryId="1"Specified to retrieve the document with category ID 1. You can replace it with the actual category ID you need to display.
  • type="list"Indicates a non-paginated list,limit="10"It limited the display quantity to 10 items.
  • item.Link/item.Title/item.CreatedTime/item.Views/item.DescriptionThey are built-in fields of the document, which can be accessed directly.item.By adding the field name to access.stampToDateThe filter is used to format timestamps.

Step two: Output custom parameters for each document

AnQi CMS allows users to customize content model fields in the background (such as adding "author", "source", "price", and other fields under "Content Management" -> "Content Model"). These custom fields can be accessed in templates in two main ways:

Method 1: Directly access the known name of the custom parameter

If you know the name of the 'call field' of the custom parameter (the English lowercase field name set when defining the content model in the background, for exampleauthor/priceIt can be directly passed throughitem.自定义字段名in the form ofarchiveListAccess them in the loop. This is the simplest and most efficient way.

Suppose you have added a field named "Article Author" for the article model in the background,authorThe custom parameter, as well as a field named "article source", is calledsourceThe custom parameter.

{% archiveList archives with categoryId="1" type="list" limit="10" %}
    {% for item in archives %}
        <div class="document-item">
            <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
            <p>发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
            <p>浏览量:{{ item.Views }}</p>
            <p>{{ item.Description }}</p>
            {% if item.author %}
                <p>作者:{{ item.author }}</p>
            {% endif %}
            {% if item.source %}
                <p>来源:{{ item.source }}</p>
            {% endif %}
        </div>
    {% endfor %}
{% endarchiveList %}

This approach is intuitive and easy to maintain, suitable for scenarios where you know exactly which custom parameters to display.

Method two: cyclically display all custom parameters of the document.

When you need to display all custom parameters of a document, or when the parameter names are not fixed (for example, when you want a template to adapt to the display of custom parameters under different content models), you can usearchiveParamsThe tag can retrieve all custom parameters of the specified document and allows you to iterate through them.

archiveParamsthe tag needs aidParameters to specify which document's custom parameters to retrieve.archiveListIn the loop, this ID is the ID of the current loop documentitem.Id.

{% archiveList archives with categoryId="1" type="list" limit="10" %}
    {% for item in archives %}
        <div class="document-item">
            <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
            <p>发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
            <p>浏览量:{{ item.Views }}</p>
            <p>{{ item.Description }}</p>

            <div class="custom-params">
                <h3>自定义属性:</h3>
                {% archiveParams params with id=item.Id %}
                    {% for param in params %}
                        <p>{{ param.Name }}:{{ param.Value | safe }}</p>
                    {% endfor %}
                {% endarchiveParams %}
            </div>
        </div>
    {% empty %}
        <p>当前分类下没有文档。</p>
    {% endfor %}
{% endarchiveList %}

In this code block:

  • {% archiveParams params with id=item.Id %}Obtained the current loop document (item) and stored all custom parameters inparamsthe variable.
  • {% for param in params %}Loop through these custom parameters.
  • param.NameDisplay the Chinese name of the custom parameter (such as "Article Author").
  • param.ValueDisplay the specific value of the custom parameter.
  • | safeThe filter is very important here. If your custom parameters may contain HTML content (for example, if you have used a rich text editor to fill in the background),safeThe filter will prevent the template engine from escaping these HTML codes, thus allowing them to render normally.

Choose the method based on your specific needs. If you need to precisely control which custom parameters are displayed and know their names, direct access mode is more concise.If you need to universally display all custom parameters, or if the set of custom parameters often changes, the loop traversal method is more flexible.

Practical skills and precautions

  • Conditional judgmentWhen displaying custom parameters, it is best to use{% if item.自定义字段名 %}or{% if param.Value %}Make a judgment to avoid blank or unnecessary labels when the parameter is not filled in some documents.
  • Data type processingThe custom parameters of AnQiCMS can be set to various types (single-line text, multi-line text, numbers, single-choice, multiple-choice, etc.). In the template,param.Valueoritem.自定义字段名It will directly output its content. For special types, such as images or files, the value is usually the URL of the resource, and you can use it directly as<img>label'ssrcproperty or<a>label'shrefan attribute.
  • CSS styleThe display effect of custom parameters is entirely dependent on the CSS styles you apply in the template, and you can adjust it flexibly according to your design requirements.
  • Custom parameters for images and filesIf the custom parameter is an image field,item.图片自定义字段名orparam.Valueit will directly output the image URL. You can use<img>tags to display it, for example<img src="{{ item.image_field }}" alt="{{ item.Title }}" />.

By using the above method, you can flexibly display the document list in the AnQiCMS template, and output specific custom parameters for each document, thereby building a website page with rich features and diverse content.

Frequently Asked Questions (FAQ)

Q1: How can I filter the document list page based on the value of custom parameters?A1: AnQiCMS providesarchiveFiltersThe tag is used specifically to filter according to the custom parameters of the document's various items.You can use this tag at the top of the list page to generate filtering conditions, such as house types, price ranges, etc. Along witharchiveListlabel'stype="page"and appropriatemoduleIdThe page can dynamically display a list of matching document lists based on the custom parameter values selected by the user.

Q2: If a document does not fill in a custom parameter, will the template report an error? How to handle it?A2: If a document does not fill in a custom parameter, access it directly.{{ item.自定义字段名 }}or{{ param.Value }}It usually does not cause template errors, but may output empty values.In order to enhance user experience, it is recommended to add conditional judgment before use.For example, use `{% if item.author %}`}

Author: {{ item.author

Related articles

How does the `archiveFilters` tag display the filter list containing the 'All' option?

In website content operation, it is crucial to provide users with convenient and intuitive filtering functions.Especially when your website content structure is complex, containing various attributes and categories, a well-designed filter list can greatly enhance user experience, helping them quickly find the information they need.AnQi CMS provides a powerful `archiveFilters` tag, specifically designed to build this type of filter list based on document parameters.Today, we will delve into how to use this tag, especially how to cleverly add an 'All' option to make your filtering function more perfect and user-friendly.

2025-11-08

How to dynamically generate filter conditions for content models in AnQi CMS templates (e.g., filtering by properties)?

When building and operating a website, providing users with an efficient content filtering function is crucial for improving user experience and content discoverability.AnQiCMS (AnQiCMS) leverages its flexible content model and powerful template tag system to help us easily implement dynamic generation of content model filtering conditions in templates, such as filtering by attributes. ### Content Model and Custom Fields: The Basis of Filtering One of the core strengths of AnQi CMS is its highly flexible content model.

2025-11-08

What is the difference between the `default_if_none` filter and the `default` filter in handling null values?

In the development of content management system templates, we often encounter situations where variable values are uncertain.Sometimes, the data may not have been filled in; sometimes, the data may exist, but its value is an empty string, zero, or a boolean false.To ensure the completeness and user experience of the website page display, it is particularly important to provide a friendly default display for these 'empty' values.

2025-11-08

How to set a default display value for possibly empty variables (such as arrays or strings) in Anqi CMS templates?

During the process of building a website with AnqiCMS, we often encounter such situations: a variable needs to be displayed in the template, but it may be empty for various reasons, such as not setting a thumbnail, a list without content, or optional information not filled in.If these empty variables are not processed, the page may appear with ugly blank areas, error messages, or layout errors, which undoubtedly affects the user experience.

2025-11-08

How to combine the `archiveList` tag with query parameters in the URL to implement dynamic search and filtering?

Build an interactive and easy-to-navigate website on Anqi CMS, with dynamic search and filtering functions being indispensable.`archiveList` tag as the core tool for content output, cleverly combines URL query parameters, and can help us implement a powerful content discovery mechanism, allowing users to easily locate the information they are interested in. ### The core function of the `archiveList` tag The `archiveList` tag is used in AnQi CMS to retrieve and display document lists.

2025-11-08

How to retrieve all custom parameters of a document in an Anqi CMS template and display them in a fixed order?

In website content management, we often need to define unique attributes for different types of information to meet the diverse display needs of the website.The AnQi CMS provides powerful custom parameter functionality, allowing you to flexibly add additional information to documents (articles, products, etc.)When you want to obtain these custom parameters in the website front-end template and display them in the fixed order set in the background, the built-in template tags of Anqi CMS can well help you achieve this.

2025-11-08

How to retrieve and display custom document parameters for a specific named using the `archiveParams` tag?

In AnQi CMS, content management is not limited to preset fields such as titles, content, etc., but can also be enriched and expanded by custom parameters.When you set unique custom fields for articles, products, or other content models, how to accurately obtain and display these specific parameter names in the front-end template is a common requirement in template creation.This article will deeply explore how to flexibly obtain and display the custom parameters of the `archiveParams` tag.### Custom parameter setting

2025-11-08

How to build a multi-level category navigation in Anqi CMS template and judge whether each category has subcategories?

In today's increasingly rich website content, a clear and efficient navigation system is crucial for improving user experience and search engine optimization (SEO).Especially for websites with a large amount of content or multiple product categories, multi-level category navigation can help users quickly find the information they need while also showing the structural hierarchy of the website to search engines.Anqi CMS with its flexible and powerful template engine makes it simple and intuitive to build such a navigation.This article will deeply explore how to cleverly construct a multi-level classification navigation in Anqi CMS template, and intelligently judge whether each category contains subcategories

2025-11-08