In website content management, we often need to define unique attributes for different types of information to meet the diverse display needs of the website.The AnQi CMS provides powerful custom parameter functions, allowing you to flexibly add extra information to documents (articles, products, etc.)When you want to retrieve these custom parameters in the website front-end template and display them in the fixed order set in the background, the template tags built into Anqicms can well help you achieve this.

Understand the custom parameters of the document

The AnQi CMS allows you to create dedicated custom fields for different document types (such as articles, products) through the content model.These fields can include authors, sources, specifications of specific products, event times, etc.These custom parameters have the advantage of not only enriching the document information, but also allowing flexible definition of field types (such as single-line text, numbers, multi-line text, single choice, multiple choice, dropdown selection, etc.), greatly enhancing the flexibility of content management and the adaptability of the website.

Core Tool:archiveParamsTag

To obtain all the custom parameters of a document in the template and ensure that they are displayed in the order you have configured in the background, we need to use a dedicated tag provided by the Anqi CMS template engine—archiveParams. This tag is designed to obtain additional parameters of the document, particularly good at handling ordered parameter lists.

How to usearchiveParamsTag

archiveParamsThe basic usage of the tag is to wrap it in a block.{% archiveParams params %}and{% endarchiveParams %}Here, paramsIs a variable name you define, it is used to store all the custom parameters obtained.

By default,archiveParamsThe tags will be returned in the order you set in the "Content Model" backend, a list containing each custom parameter. Each parameter is an object containing two main properties:

  • NameIt indicates the display name of the custom parameter (for example, 'article author', 'product model').
  • ValueIt indicates the specific value of the custom parameter (for example, "Zhang San", "X-Pro 2023").

We will illustrate in detail how to use this tag in a template through an example.

Practice in the template

Assuming you have already defined custom parameters such as "Product Model", "Color", "Material", etc. for a content model in the background.Now, you would like to display these parameters on the product detail page.

You can use the template file in the product detail page, usually{模型table}/detail.htmlor add the following code snippet to a custom document template

{# 假设您正在产品详情页,并且当前文档对象已被正确加载 #}

<div class="product-specs">
    <h3>产品参数</h3>
    {% archiveParams productParams %} {# productParams 是您自定义的变量名 #}
        {% if productParams %} {# 检查是否存在自定义参数 #}
            <ul>
                {% for item in productParams %}
                    {# 遍历每一个自定义参数,item.Name 是参数名称,item.Value 是参数值 #}
                    <li>
                        <strong>{{ item.Name }}:</strong>
                        <span>{{ item.Value }}</span>
                    </li>
                {% endfor %}
            </ul>
        {% else %}
            <p>暂无其他产品参数信息。</p>
        {% endif %}
    {% endarchiveParams %}
</div>

In this code block:

  1. We first use{% archiveParams productParams %}tags to retrieve all custom parameters of the current document, and assign them to a variable namedproductParams.
  2. {% if productParams %}Check if there are any custom parameters present, to avoid displaying an empty list when there are none.
  3. {% for item in productParams %}Loop throughproductParamseach parameter in the variable. SincearchiveParamsBy default, it is returned in the order set by the background, so hereitemIt will appear in the order defined in the content model.
  4. Inside the loop,{{ item.Name }}The parameter name (such as 'Product Model') will be displayed, whereas{{ item.Value }}the specific value of the parameter (such as 'AnQiCMS Pro') will be displayed.

In this way, you can not only easily obtain all the custom parameters of the document, but also ensure that they are presented to the user in a structured and orderly manner. This is particularly important for scenarios where product specifications, technical documents, and other information display sequences need to be strictly followed.

Get custom parameters of the specified document

The above example defaults to retrieving the parameters of the currently browsing document. If you need to getother specified documentCustom parameters, for example, in a related recommendation module, you can useidparameter to specify the document ID:

{# 假设您要获取 ID 为 123 的文档的自定义参数 #}
<div class="related-document-specs">
    <h4>相关文档(ID: 123)参数</h4>
    {% archiveParams specificDocParams with id="123" %}
        {# 同样可以通过循环遍历 specificDocParams 来显示其内容 #}
        {% for item in specificDocParams %}
            <p>{{ item.Name }}: {{ item.Value }}</p>
        {% endfor %}
    {% endarchiveParams %}
</div>

Emphasis on parameter sorting

archiveParamsLabels are returned by default in the order you have configured in the "Content Model" on the backend. This means you do not need any additional settingssorted=true(Although the parameter is mentioned in the document, it is usually omitted because it is the default behavior). If you indeed need to access key-value pairs in an unordered manner (for example, by accessing a specific parameter directly by name, without caring about its position in the list), you cansortedthe parameter tofalsebut for the need to display in fixed order, keep default or explicitsorted=trueJust do it.

In summary, AnQi CMS'sarchiveParamsTags are an efficient and intuitive tool that makes it exceptionally simple to display custom parameters in templates, while fully respecting the display order you set in the background, thereby greatly enhancing the efficiency of template creation and the expressiveness of content.


Frequently Asked Questions (FAQ)

  1. Ask: How can I display a specific custom parameter instead of all parameters?Answer: If you only need to display a single custom parameter, you can usearchiveDetailLabel and specify the parameter name. For example, if you have a custom parameter namedproduct_modelyou can directly call it in the template:{% archiveDetail with name="product_model" %}. This method is more direct and does not require looping through all parameters.
  2. Ask: If a custom parameter is not filled in, how can I make it not display?Answer: In a loopproductParamsWhen defining a variable, you can{% for item in productParams %}Add a conditional judgment inside the loop. For example,{% if item.Value %}So that, only whenitem.Valuethere is content, will the name and value of the parameter be displayed.
  3. Ask: Can I get the custom parameters of the document on other pages (such as the list page) besides the document detail page?Yes, you can.archiveParamsTags can be used.idSpecify the ID of any document to retrieve the custom parameters of the document.This means you can call this tag by providing a document ID anywhere you need it (such as within each document item in a list page loop or in a related recommendation block) to retrieve and display the custom parameters of the specified document.archiveParamsThe page loading time may increase, it is recommended to optimize according to actual needs and performance.