How to call the `archiveParams` tag individually in AnQiCMS template to get custom document parameters with a specific name?
In the rich feature treasure 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.Today, let's delve deeply into a powerful and often overlooked trick in the AnQiCMS template: how to call it individuallyarchiveParamsLabel to obtain custom document parameters with a specific name.
I. Introduction: Deciphering the value of AnQiCMS custom document parameters.
AnQiCMS can become the preferred choice for many small and medium-sized enterprises and content operation teams, largely due to its highly customizable content model.By custom fields, we can add unique properties to articles, products, or any other content type, such as adding 'color', 'size', 'material' for products, or 'author bio', 'original link' for articles, etc.These custom parameters allow our content to go beyond fixed fields, enabling it to change according to business needs, greatly enriching the dimensions of information display.
In the AnQiCMS template system,archiveParamsThe tag plays a core role, it is specifically used to extract these custom parameters from the document.We usually use a loop to iterate through all the parameters to display them, but in some cases, if we only want to get the value of a parameter with a specific name and then perform other operations, the traditional loop method is not direct enough. 幸运的是,archiveParamsThe tag provides a more refined control method, allowing us to directly access specific custom parameters as if accessing object properties.
Second,archiveParamsThe core mechanism of the tag and two modes of invocation.
archiveParamsThe tag is used in AnQiCMS templates to obtain all custom parameters of the current document (or specified document). To understand how to call a specific named parameter individually, we first need to understand its two main data acquisition modes:
Default mode (
sorted=true): This isarchiveParamsThe default behavior of the tag. When not specifiedsortedthe parameter or set it totrueit will return an ordered array object. Each element in this array represents a custom parameter, includingName(Parameter name, such as "Product Color") andValue(Parameter value, such as "red"). This pattern is very suitable for looping to display all custom parameters, such as listing all specifications on a product detail page.Unordered Map mode (
sorted=false)This is the main topic we are discussing today. WhenarchiveParamsLabel settingssorted=falseWhen it will no longer return an ordered array, but an unordered Map (which can be understood as a set of key-value pairs). The "key" (key) is exactly what we define in the background content model for custom fields "Field invocationThe name is usually in English lowercase camel case or underscore naming, while the value is an object containing the custom parameter'sNameandValue.In the background content model, when we add a custom field to a certain model (such as "product model"), it is required to fill in the "parameter name" (such as "product color") and the "called field" (such as
productColorThis "call field" is the key identifier we use to directly access specific parameters in unordered Map mode.
Chapter 3: Precision Strike: Calling Specific Named Custom Document Parameters Individually
Now, we focus on how to utilizearchiveParamsThe unordered Map pattern, enabling precise invocation of custom parameters for specific names.
Core strategy: utilizingsorted=falseParameter
When we willarchiveParamslabel'ssortedthe parameter tofalseWhen, its internal returned data structure will become a Map.This means that we can access elements in the Map just like accessing properties of a JavaScript object using dot notation.
Code practice: Implement step by step.
Let's demonstrate through a specific example.
First step: Assume backend configurationTaking the 'Product Model' as an example, assuming that in the AnQiCMS backend content model, we have added the following custom fields for the product:
- Parameter Name: Product ColorField invocation:
productColor - Parameter Name: Material of the productField invocation:
productMaterial - Parameter Name: Suitable scenarioField invocation:
applicationScene
Second step: Template invocation example
Now, in the product detail page template (for exampleproduct/detail.htmlIn this case, if we want to get the values of the specific parameters "Product Color" and "Application Scenario" without traversing 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 namedproductDetailsmap variable. - Then, through
productDetails.productColor.ValueandproductDetails.applicationScene.ValueSuch dot syntax, we directly locate and extract the parameter values of "product color" and "application scenario". {% if ... %}Judging is a good practice that can avoid unnecessary blank lines or errors in template rendering when a custom field is not present in some documents.
witharchiveDetailThe differences and advantages of tags.
You may notice that,archiveDetailTags can also directly obtain 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 obtain a specific field'sValue.
However,archiveParams(sorted