How to loop and display document lists in Anqi CMS template and output custom parameters for each document?

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

Step 1: Master the loop display of document lists

Firstly, we need to usearchiveListUse tags to get and loop display the document list. This tag is a core tool used for content list display in AnQiCMS.

We usually use it in templates.{% archiveList %}To specify the document collection to retrieve, and by{% for item in archives %}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 our custom variable name, which will containarchiveListthe set of document data returned.
  • categoryId="1"Specified the document to be retrieved with category ID 1. You can replace it with the category ID you need to display according to your actual situation.
  • type="list"Indicates a non-paginated list.limit="10"The display quantity is limited 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 throughitem.by adding the field name.stampToDateThe filter is used to format timestamps.

Step two: Output custom parameters for each document

The '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 customized fields can be accessed in the template in two main ways:

Method one: Directly access the custom parameter by its known name

If you know the "call field" name of the custom parameter (the English lowercase field name set when defining the content model in the background, for exampleauthor/price), you can directly accessitem.自定义字段名in the form ofarchiveListAccess them in the loop. This is the most concise and efficient way.

Suppose you have added a field named "Article Author" for the article model in the background,authorCustom parameters, as well as a field named "Article Source", calledsourceCustom parameters.

{% 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 method is intuitive and easy to maintain, suitable for scenarios where you clearly know 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 the names of custom parameters are not fixed (for example, hoping a template can adapt to the display of custom parameters under different content models), you can usearchiveParamsThis tag can retrieve all custom parameters of a specified document and allows you to iterate through them.

archiveParamsThis tag requires aidCustom parameters to specify which document to retrieve.archiveListIn the loop, this ID is the current document in the loop.item.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 %}Retrieved the current loop document (itemAll custom parameters of ) are stored.paramsthe variable.
  • {% for param in params %}Loop through these custom parameters.
  • param.NameDisplay the Chinese name of custom parameters (e.g., “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 a rich text editor was used to fill in the background),safeThe filter will prevent the template engine from escaping these HTML codes, thus allowing them to render normally.

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

Useful Tips and Considerations

  • Conditional judgmentIt is best to use when displaying custom parameters.{% if item.自定义字段名 %}or{% if param.Value %}Make a judgment to avoid blank or unnecessary tags when some documents do not fill in the parameter.
  • Data type handlingThe 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 resource URL, which you can use directly as<img>Tagssrcproperties or<a>Tagshrefa property.
  • CSS styleThe display effect of custom parameters depends entirely on the CSS styles you apply in the template, and you can adjust it flexibly according to your design requirements.
  • Custom parameters' images and filesIf a custom parameter is an image field,item.图片自定义字段名orparam.Valuethe image URL will be output directly. 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 loop through document lists in the AnQiCMS template and output specific custom parameters for each document, thereby building a website page with rich functions and diverse content.

Common Questions and Answers (FAQ)

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

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 directly{{ item.自定义字段名 }}or{{ param.Value }}The content usually does not cause template errors, but may output empty values.To provide a better user experience, it is recommended to add conditional judgment before use.

Author: {{ item.author