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

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