How to use the `archiveParams` tag to retrieve and display the content of the custom parameter fields of the document?

In the operation of daily websites, we often encounter such needs: standardized content fields can no longer meet the diversified information display.For example, a product detail page may need to display unique attributes such as "model", "material", "warranty period", etc., while an ordinary article may need to additionally note "source of the article", "estimated reading time", etc.archiveParamsTags, allowing you to easily retrieve and display these customized information exclusive to the documents.

This article will delve into how to utilizearchiveParamsLabel, display the custom parameter content of the document on the website front-end, making your content display more rich and personalized.

Configure custom parameters: Inject vitality into your content model.

Before discussing how to display these parameters, we first need to understand how they are created and added.The custom parameters of AnQi CMS are closely related to the 'Content Model'.Each content model can have its own unique set of custom fields, which will serve as additional information for all documents under the model.

To start the setup, you need to enter the Anqi CMS admin interface, find the "Content ManagementThe system provides the 'Article Model' and 'Product Model' by default, and you can also create new content models according to your business needs.

Select a model you wish to add custom parameters, click the edit button.On the model editing page, you will see a "Content Model Custom Field" area.

  • Parameter NameThis is the field name you see and understand in the background management interface, such as "Product Model", "Article Author".
  • Field invocationThis is the unique identifier used to call the parameter in the template, it is usually recommended to use letters. For example, if the parameter name is "Product Model", the calling field can bemodelNumberIn the template, you will get the value through this field name.
  • Field type: AnQi CMS supports various field types such as single-line text, numbers, multi-line text, single selection, multiple selection, dropdown selection, etc., and you can choose according to the actual information type.
  • Mandatory?: Determines whether the user must fill in this field when publishing a document.
  • Default value: Sets a default value for the field, which will be used if the user does not fill in the field.

After completing the addition and saving of custom fields, when you go to publish or edit the documents under the content model again, in the "Other Parameters" section of the document editing page, you will see these newly added custom fields, and you can fill in exclusive content for each document.

archiveParamsLabel: Display custom data accurately in the template

Now, the core task is here: How can these custom parameters, carefully configured and filled in the background, be presented on your website page? The answer is to use the Anqi CMS providedarchiveParams.

archiveParamsThe tag is mainly used to obtain all custom parameters of a specified document. You can choose to traverse and display all parameters in the form of an array in fixed order, or directly obtain the value of a specific parameter in the form of a key-value pair (Map).

1. Traverse all custom parameters (recommended: an array in fixed order)

On the document details page (or any page where you can obtain the document ID), if you want to display all the custom parameters of the current document, you can usearchiveParamsLabel it and let it return an array. By default,archiveParamslabel'ssortedthe parameter istruewhich means it will return an ordered list containing all the parameters.

Each list item is an object that containsName(Parameter name, that is, the Chinese name set in the background) andValue(Parameter value).

{# 假设我们正在一个文档详情页,它会自动获取当前文档的自定义参数 #}
<div class="custom-parameters">
    <h3>详细参数</h3>
    {% archiveParams params %} {# params 是您为获取到的参数定义的变量名 #}
        {% for item in params %}
            <p><strong>{{ item.Name }}:</strong>{{ item.Value }}</p>
        {% endfor %}
    {% endarchiveParams %}
</div>

By this code, your website will traverse and display all the custom parameters and their corresponding values of the current document, such as "Product Model: XYZ-2023", "Material: Stainless Steel", and so on, greatly enhancing the information content of the content.

2. Retrieve specific custom parameters (unordered key-value pairs or directly througharchiveDetail)

Sometimes, you may not need to iterate through all the parameters, but just want to retrieve and display a specific custom parameter, such as only displaying the "product model" or "article author". At this time, you can letarchiveParamsReturn an unordered key-value pair (Map), or use a more concise way toarchiveDetail.

Method one: usearchiveParamsReturn an unordered key-value pair

By settingsorted=false,archiveParamsThe label returns a key-value pair object, where the key is the field you set in the background, and the value is the object that containsNameandValuethe object.

{# 假设要获取调用字段为 'modelNumber' 和 'author' 的自定义参数 #}
<div class="specific-parameters">
    {% archiveParams customFields with sorted=false %}
        <p>产品型号:{{ customFields.modelNumber.Value }}</p>
        <p>文章作者:{{ customFields.author.Value }}</p>
    {% endarchiveParams %}
</div>

This method is suitable for cases where you need to retrieve multiple but not all custom parameters, throughcustomFields.调用字段.ValueYou can directly access it in this form.

Method two: access directlyarchiveDetailtag to obtain

For scenarios where only a single specific custom parameter is needed, AnQi CMS provides a more direct and concise method: usingarchiveDetailLabel. If you know the "calling field" of the custom parameter, you can use it directlyarchiveDetailto get its value, just like getting system-built-in fields such as document title, content, etc.

<div class="quick-access-parameters">
    <p>产品型号:{% archiveDetail with name="modelNumber" %}</p>
    <p>文章作者:{% archiveDetail with name="author" %}</p>
    {# 如果自定义字段是组图类型,例如调用字段为 'productImages' #}
    {% archiveDetail productImages with name="productImages" %}
        {% if productImages %}
            <div class="product-gallery">
                {% for imgUrl in productImages %}
                    <img src="{{ imgUrl }}" alt="产品图片">
                {% endfor %}
            </div>
        {% endif %}
    {% endarchiveDetail %}
</div>

This method is very suitable for scenarios where your page layout is fixed, only a few known custom parameters need to be displayed, and the code is more concise and readable.

3. Get custom parameters for a specified document.

This example defaults to retrieving the custom parameters of the current document. If you need to retrieve the custom parameters of *other* documents on the current page, you can specify the document ID throughidthe parameter.

{# 获取ID为123的文档的所有自定义参数 #}
{% archiveParams otherDocParams with id="123" %}
    {% for item in otherDocParams %}
        <p><strong>{{ item.Name }}:</strong>{{ item.Value }}</p>
    {% endfor %}
{% endarchiveParams %}

{# 获取ID为456的文档的某个特定自定义参数 'material' #}
<p>其他文档的材质:{% archiveDetail with name="material" id="456" %}</p>

In addition, for users managing multiple sites, if they need to call data from other sites, they can also add parameters in the tag to specify the site ID.siteIdto specify the site ID.

Make flexible use of custom parameters to enrich your website content

ByarchiveParamsandarchiveDetailLabel, you can fully utilize the custom parameter function of AnQi CMS.Whether it is to add detailed specifications to the product list, supplement additional information to the article, or display registration details on the event page, these custom parameters can help you build more expressive and business-demand-aligned content.They not only make your content more attractive, but also provide more possibilities for structured data in SEO optimization, allowing search engines to better understand and display your page.

MasteredarchiveParamsLabel, you have the key tool to achieve highly customized content in Anqi CMS. Dare to try and let your website content soar to new heights!


Frequently Asked Questions (FAQ)

Q1: Why did I add custom fields but they do not display in the template?

A1: The most common reason is that the 'called field' used in the template does not match the background settings, or you have usedarchiveParamsLabel but did not correctly traverse the returned structure. Please check the following points:

  1. Spell the field name: Confirm the template in thecustomFields.fieldName.Valueor{% archiveDetail with name="fieldName" %}offieldNameConsistent with the "call field" set in the background content model, including case sensitivity.
  2. sortedParameter impactIf you have used:archiveParamstag, sorted=true(Default) It returns an array, which needs to be{% for item in params %}{{ item.Name }}: {{ item.Value }}{% endfor %}traversed in this way. Andsorted=falseReturns a key-value pair, it can be used directly{{ params.fieldName.Value }}Access. Make sure you use the access method that matches the return type.