In the rich feature treasure trove of AnQiCMS, the content model and custom document parameters are undoubtedly the foundation of its flexibility and powerful extensibility.As a website operations expert, I am well aware of how to efficiently and accurately call these custom parameters in templates, which is crucial for achieving personalized content display and optimizing operational efficiency.archiveParamsLabel, to get custom document parameters with specific names.

I. Introduction: Deciphering the value of AnQiCMS custom document parameters.

AnQiCMS is the preferred choice for many small and medium-sized enterprises and content operation teams, largely due to its highly customizable content model.Through custom fields, we can add unique attributes to articles, products, or any other content type, such as adding 'color', 'size', and 'material' for products, or 'author bio', 'original link' for articles, etc.These custom parameters make our content no longer limited to fixed fields, but able to change according to business needs, greatly enriching the dimensions of information display.

In the AnQiCMS template system,archiveParamsThe tag plays a core role, specifically used to extract these custom parameters from the document. 通常,我们会通过循环遍历的方式来展示所有参数,但这在某些场景下,如果我们只想获取某个特定名称的参数值,再进行其他操作,传统的循环方式就显得不够直接。archiveParamsThe tag provides a more refined control method, allowing us to directly access specific custom parameters as if we were accessing object properties.

Second,archiveParamsThe core mechanism of the tag and two modes of invocation

archiveParamsThe tag is used in AnQiCMS template to retrieve all custom parameters of the current document (or a specified document). To understand how to call a specific parameter name individually, we first need to understand its two main data acquisition modes:

  1. Default mode (sorted=true): ThisarchiveParamsis the default behavior of the label. When not specifiedsortedthe parameter or set it totrue, it will return an ordered array object. Each element in this array represents a custom parameter, containingName(Parameter name, such as "Product Color") andValue(Parameter value, such as “red”)。This mode is very suitable for displaying all custom parameters in a loop, such as listing all specifications on a product detail page.

  2. Unordered Map mode(sorted=false)This is the main topic we are discussing today. WhenarchiveParamsSet tagssorted=falseThis will no longer return an ordered array, but an unordered Map (which can be understood as a set of key-value pairs). The 'key' of this Map is exactly the one we define for custom fields in the backend content model.Call field”Name (usually in English lowercase camel case or underscore naming), and "value" is an object containing theNameandValue.

    In the background content model, when we add a custom field for a model (such as "Product ModelproductColorThis 'call field' is the identifier we use to access specific parameters directly in the unordered map mode.

三、精准出击:单独调用特定名称的自定义文档参数

现在,我们聚焦于如何利用archiveParamsThe unordered Map mode of tags, achieving precise calls to custom parameters of specific names.

Core Strategy: Utilizesorted=falseParameters

When we set the value of thearchiveParamsTagssortedparameter settingsfalseWhen, its internal return data structure will become a Map.This means we can directly access the elements in the Map by dot notation, just like accessing properties of a JavaScript object.

Code Practice: Step by step implementation

Let's demonstrate with a specific example.

第一步:Assume background configurationWe take the "Product Model" as an example, assuming that in the AnQiCMS background content model, we have added the following custom fields:

  • Parameter name: “Product Color”Call field:productColor
  • Parameter name: “Product Material”Call field:productMaterial
  • Parameter name: “Application Scene”Call field:applicationScene

Second Step: Template Call Example

Now, in the product detail page template (for exampleproduct/detail.htmlIn this case, if we only want to get the values of the specific parameters 'Product Color' and 'Application Scenario', without iterating through all parameters, we can write the template code like this:

{# 在产品详情页模板中调用,假设当前文档就是产品 #}
{% archiveParams productDetails with sorted=false %}
    <div class="product-specs">
        {% if productDetails.productColor %} {# 建议进行if判断,确保字段存在 #}
            <p><strong>产品颜色:</strong> {{ productDetails.productColor.Value }}</p>
        {% endif %}

        {% if productDetails.applicationScene %} {# 同上,判断字段是否存在 #}
            <p><strong>适用场景:</strong> {{ productDetails.applicationScene.Value }}</p>
        {% endif %}

        {# 如果需要获取参数名本身,也可以这样: #}
        {# <p>{{ productDetails.productColor.Name }}: {{ productDetails.productColor.Value }}</p> #}
    </div>
{% endarchiveParams %}

In this code block:

  • We first use{% archiveParams productDetails with sorted=false %}Get all custom parameters and store them in a Map variable namedproductDetails.
  • Then, throughproductDetails.productColor.ValueandproductDetails.applicationScene.ValueSuch syntax, we directly locate and extract the parameter values of “Product Color” and “Applicable Scene”.
  • {% if ... %}It is a good practice to judge, which can avoid unnecessary blank lines or errors in template rendering when a certain custom field does not exist in some documents.

WitharchiveDetailDifferences and advantages of tags

You may notice that,archiveDetailTags can also directly retrieve the value of a single custom field, for example:{% archiveDetail with name="productColor" %}. This method is indeed more concise, especially when you only need to retrieve a specific field,Value.

However,archiveParamsEnglish (`sorted