In the Aanqi CMS, content management is not limited to predefined fields such as titles and content, but can also be enriched and expanded by custom parameters.How to accurately obtain and display these specific parameter names in the front-end template when you set unique custom fields for articles, products, or other content models, is a common requirement in template creation.archiveParamsLabels, flexibly obtain and display custom parameters of documents.

Settings of custom parameters: a brief review

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

archiveParamsThe core function of the tag

archiveParamsThe label is a tool specifically used in the AnQi CMS template to obtain custom parameters for the current document or a specified document.It can provide all custom fields configured for the document by the backend to the front-end template in a programmable way, greatly enhancing the flexibility and expressiveness of the template.

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

archiveParamsKey parameters of the tag

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 the parameter. If you use it in the document detail pagearchiveParamsthe tag and do not specifyidIt is a parameter, which will default to retrieving the document parameters of the current page. If you want to get parameters of other documents, you need to specify their ID explicitly, for example,id="10".
  • sortedThis is a very critical parameter that determines the data structure of the returned data.
    • Whensorted=truewhen set to (default value),archiveParamsit will return aordered array object. Each element in the array is a container that includesNameandValueThe object of the attribute, whereNameis the display name of the custom field.Valueis its corresponding value. This pattern is suitable for the scenario where you want to iterate through and display all custom parameters of the document.
    • Whensorted=falsewhenarchiveParamsit will return aDisordered Map (key-value pair) object. This Map's key is the custom field's 'Call Field' (the English name defined in the background content model), and the value is a container that includesNameandValueThe object of properties. This pattern is suitable for the scenario where you already know the names of the custom field's calling field names and want to directly access specific parameters.
  • siteIdIn the environment of multi-site management, if you need to call document parameters from other sites, you can do so throughsiteIdSpecify the ID of the target site as a parameter. Usually, this parameter does not need to be filled in.

How to get and display a specific named custom parameter?

UnderstoodsortedAfter the distinction of parameters, let's take a look at the two main ways to obtain them.

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

When you want to display all custom parameters of the document in the document footer, sidebar, or a dedicated "Parameter List" area,sorted=truePattern (also the default pattern) is very practical. It returns an array, which you can use convenientlyforTraverse in a loop.

The content of the field 'author', 'publisher', 'ISBN', and other custom fields you define in the background 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,item.NameIt will display the parameter name set in the background (such as “Author”), whereasitem.Valueit will display the corresponding content of the parameter (such as “Zhang San”).

方式二:直接获取特定名称的自定义参数(English)sorted=false)

如果您已经明确知道某个自定义参数的“调用字段”名称,并且只想显示这一个参数,English)sorted=falseThe pattern allows you to obtain it more directly.

For example, you have defined a custom field named "author" (call field) in the background content model. You can directly obtain its value by the following method:

auto