In Anqi CMS, content management is not limited to preset fields such as titles and content, but can also be enriched and expanded through 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 frontend template is a common requirement in template creation.This article will deeply explore how to usearchiveParamsLabel, flexibly retrieve and display the custom parameters of the document.

A brief review of setting custom parameters

Further inarchiveParamsBefore the label, let's quickly review the settings of custom parameters.In AnQi CMS backend, you can enter the 'Content Management' module under the 'Content Model' settings to select or create a content model.In here, you can add various types of custom fields, such as single-line text, numbers, multi-line text, single selection, multiple selection, dropdown selection, and more.These fields are what we call 'custom parameters', which allow each document to carry more personalized information.

archiveParamsThe core role of tags

archiveParamsThe tag is a tool used specifically in Anqi CMS template to obtain custom parameters for the current document or a specified document.It can provide all the custom fields configured for the document in the background to the front-end template programmatically, greatly enhancing the flexibility and expressiveness of the template.

This label usually needs to be wrapped in{% archiveParams ... %}and{% endarchiveParams %}Between, used to define a variable to hold the parameter data obtained.

archiveParamsThe key parameters of the

archiveParamsThe tag provides some parameters to help you accurately obtain the required data:

  • idThis parameter is used to specify the document ID you want to retrieve. If you use it in the document detail pagearchiveParamstag and do not specifyidThe parameter, it will default to get the document parameters of the current page. If you want to get the parameters of other documents, you need to explicitly specify its ID, for exampleid="10".
  • sortedThis is a very critical parameter, which determines the returned data structure.
    • Whensorted=trueWhen set to the default value,archiveParamsWill return onean ordered array of objectsEach element in the array is a containing.NameandValueobject, with the properties.Nameis the display name of the custom field,ValueThis is the corresponding value. This pattern is suitable for scenarios where you want to iterate and display all custom parameters of the document.
    • Whensorted=falsethen,archiveParamsWill return oneUnordered Map (key-value pair) object. This Map's key is the 'callback field' of the custom field (defined in the backend content model), and the value is a container that includesNameandValueThe object of properties. This pattern is suitable when you already know the custom field's call name and want to directly access specific parameters.
  • siteIdIn a multi-site management environment, if you need to call the document parameters of other sites, you cansiteIdspecify the ID of the target site as a parameter. Usually, this parameter does not need to be filled in.

How to retrieve and display a custom parameter with a specific name?

UnderstoodsortedAfter the difference of parameters, let's look at the two main ways to retrieve them.

Method one: Traverse all custom parameters (sorted=true)

When you want to display all the custom parameters of the document in the document footer, sidebar, or a dedicated "parameter list" area,sorted=trueThe pattern (also the default pattern) is very useful. It returns an array, which you can use convenientlyforto iterate in a loop.

Assuming you have defined custom fields such as "author", "publisher", "ISBN", etc., they will be returned in array form:

{% archiveParams customFields %}
    {% if customFields %} {# 建议添加判断,只有存在自定义参数时才显示 #}
    <div class="custom-params-list">
        <h3>文档额外信息</h3>
        <ul>
        {% for item in customFields %}
            <li>
                <strong>{{ item.Name }}:</strong>
                <span>{{ item.Value }}</span>
            </li>
        {% endfor %}
        </ul>
    </div>
    {% endif %}
{% endarchiveParams %}

In this code block,item.NameIt will display the "parameter name" you set in the background (such as "author"), whereasitem.Valueit will display the corresponding content of the parameter (such as "Zhang San").

Method two: Directly obtain a custom parameter with a specific name (sorted=false)

If you already know the name of a custom parameter's "call field" and only want to display this parameter, sorted=falseThe pattern can help you get it more directly.

For example, you have defined a custom field named "author" (the call field) in the admin content model. You can get its value directly in the following way:

”`twig {% archiveParams paramsMap with sorted=false %}