What is the core function of the `archiveParams` tag in AnQiCMS template?
In the Anqi CMS template world, efficiently and flexibly displaying content is the key to website operation success.We know that a good content management system not only needs to manage standardized information but also needs to adapt to the ever-changing business needs and provide personalized content display.Today, let's delve deeply into a crucial tag in the AnQiCMS templatearchiveParamsIt is the hero behind the scenes that enables highly customized content.
In AnQiCMS templatearchiveParamsWhat is the core role of tags?
archiveParamsThe tag plays the role of a bridge in the AnQiCMS template, it safely and flexibly presents the custom additional parameters for articles (or any document type, such as products) from the backend to the frontend. In short, when you define personalized information such as 'color', 'size', 'author', 'origin', 'product specifications' in the content model of the backend, in addition to standard fields such as title, content, and categories, for articles, products, or other 'documents',archiveParamsThat is the core tool you use to extract and display these custom data on the page.
Imagine if AnQiCMS only allowed displaying fixed titles and content, then when making e-commerce product pages, real estate information pages, or job position detail pages, it would not be able to display those key, differentiated attributes.archiveParamsThe appearance has completely broken through this limitation, endowing the content model with infinite scalability. It allows you to create unique and rich attribute sets for each type of document based on actual business needs, and through this tag, accurately call and layout these information in the front-end template.
The source of these custom parameters is usually the AnQiCMS backendContent modeldefined inCustom fieldFor example, you can add a custom field named "Product Weight" of type "Number" to the "Product Model". When editing a product, you can fill in its weight.archiveParamsThe task is to extract the value of this "product weight" from the background database and provide it for your template.
Deep understandingarchiveParams: Parameters and Usage
archiveParamsThe basic usage of tags is{% archiveParams 变量名称 with 参数... %}...{% endarchiveParams %}It accepts several key parameters to control the way data is retrieved and organized:
idParameter- Purpose: Used to specify custom parameters for the document to be retrieved.
- Usage:
id="1". - Instructions: Generally, when you use on the document detail page
archiveParamsthere is no need to specify manuallyidIt will default to getting the document ID of the current page. But if you need to display custom parameters of a specific document on other pages (such as a module on the homepage), you canidSpecify the parameter explicitly.
sortedParameter- Function: Determines the data structure of the returned data isOrdered arrayOrUnordered map (Map).
- Usage:
sorted=trueorsorted=false. - Description:
- When
sorted=trueWhen set to the default value,archiveParamsIt will return an ordered array containing custom parameters. Each array element is an object that includesName(Parameter name, such as "color") andValue(Parameter value, such as "red") two properties. This method is very suitable for scenarios where you want to iterate and display all custom parameters, especially when you are not sure about the custom parameters or they may change dynamically. - When
sorted=falsethen,archiveParamsIt returns an unordered mapping (similar to a dictionary), where the key is the 'calling field' of the custom field (defined in the background content model, usually in lowercase English), and the value is the content that includesNameandValueThe object. This method has the advantage that if you know the name of a custom parameter call field, you can directly access its value by field name, without traversing, which is more efficient.
- When
siteIdParameter- The function: Specify which site's document data to retrieve in a multi-site management scenario.
- Usage:
siteId="1". - Instructions: For single-site users, this parameter is usually not required. If you manage multiple AnQiCMS sites and want to call the document parameters of another site in a template of one site,
siteIdIt becomes very useful.
Actual application scenarios and code examples
Let us feel through specific code snippets.archiveParamsThe powerful function.
Scenario one: Traverse and display all custom parameters (such as all specifications of the product)This is applicable to the product details page, where you want to list all the features or specifications of the product.
{# 假设我们正在一个文档详情页,并且后台为该文档定义了“颜色”、“尺寸”、“材质”等自定义字段 #}
<div>
<h3>产品详细参数:</h3>
{% archiveParams params with sorted=true %} {# 使用默认的 sorted=true 返回有序数组 #}
<ul>
{% for item in params %} {# 遍历数组中的每一个自定义参数 #}
<li>
<strong>{{ item.Name }}:</strong> {{ item.Value }}
</li>
{% endfor %}
</ul>
{% endarchiveParams %}
</div>
This code will intelligently traverse all custom fields, regardless of how many you add in the background, the front end can automatically adapt and display them, greatly reducing the cost of template maintenance.
Scenario two: Directly access specific custom parameters (such as obtaining the product's 'author' information')If you know exactly which specific custom field value you need to display, such as the "author" of an article or the "inventory" of a product.
{# 方法一:通过 archiveParams with sorted=false 获取映射并直接访问 #}
{% archiveParams customFields with sorted=false %}
{% if customFields.author %} {# 假设自定义字段的“调用字段”是“author” #}
<p><strong>作者:</strong> {{ customFields.author.Value }}</p>
{% endif %}
{% if customFields.stock %} {# 假设自定义字段的“调用字段”是“stock” #}
<p><strong>库存:</strong> {{ customFields.stock.Value }}</p>
{% endif %}
{% endarchiveParams %}
{# 方法二:更简洁的方式,直接使用 archiveDetail 标签(推荐已知字段时使用) #}
{# 当您只需要获取一个或少数几个明确知道名称的自定义字段时,archiveDetail 更加直观和简洁 #}
<p><strong>文章作者:</strong> {% archiveDetail with name="author" %}</p>
<p><strong>商品库存:</strong> {% archiveDetail with name="stock" %}</p>
Here we saw.archiveDetailThe tag can directly retrieve custom fields, it is usually recommended for known and specific fields, because it is more concise in syntax.archiveParamsofsorted=falsemode andarchiveDetailOn obtaining a single known custom field, the function is similar, butarchiveParamsThe advantage lies in being able to provide theNameandValueobject, whilearchiveDetailreturn directlyValue.
Scenario three: Handling custom image groups (such as product multi-image display)Assuming you have defined a custom field "Product Image Group" (call field) in the backgroundproduct_images), it allows uploading multiple images.
{% archiveParams params with sorted=false %}
{% if params.product_images and params.product_images.Value %} {# 检查是否存在产品图片组及是否有值 #}
<h3>产品图片展示:</h3>
<div class="product-gallery">
{% set imageUrls = params.product_images.Value %} {# 获取图片URL的字符串,通常为JSON格式或逗号分隔 #}
{% set imageArray = imageUrls | list %} {# 假设后台存储的是JSON格式的字符串数组,这里使用list过滤器将其转换为数组 #}
{% for imgUrl in imageArray %}
<img src="{{ imgUrl }}" alt="产品图片">
{% endfor %}
</div>
{% endif %}
{% endarchiveParams %}
This example shows.archiveParamsFlexibility in handling complex custom data (such as image arrays), usually requires combining with AnQiCMS filters (such aslist) to parse and display.
archiveParamsRelationship with other tags
archiveParamsIt is not isolated, it is closely connected to the AnQiCMS ecosystem:
- Relationship with the Content Model (Content Model):
archiveParamsEverything displayed is sourced from the backendContent modeldefined inCustom field. Without the extension of the content model,archiveParamsthere is no flour to cook. - with
archiveDetailLabel relationships:As mentioned before,archiveDetailcan be directly accessed throughname="自定义字段名"The way to get the value of a single custom field. When you are only interested in a specific custom field,archiveDetailit is usually more concise. AndarchiveParamsIt is more suitable for scenarios where you need to dynamically traverse all custom fields, or need to access fields.NameandValueFor more precise control over objects. - with
archiveFiltersLabel relationships:archiveFiltersUsed to generate filtering conditions on the list page, the underlying data also comes from the 'filterable parameters' configured in the content model.archiveParamsIt is aimed at individual documents, displaying their specific parameter values.
Summary
archiveParamsThe tag is an indispensable tool for the AnQiCMS content operation experts.It gives AnQiCMS great content customization flexibility, allowing you to easily display the rich custom parameters defined in the background content model in a user-friendly way on the front end.To enhance the information density of the product detail page or to achieve highly personalized display of specific document types, proficiency in masteringarchiveParamsThe use of, can make your AnQiCMS website content more attractive and more in line with business needs.
Frequently Asked Questions (FAQ)
- When should I use
archiveParamsWhen to usearchiveDetailto get custom fields?- Use
archiveParams:when you need to iterate and display a documentallormostWhen customizing fields (especially when you are unsure about the fields, or the fields may change dynamically), or
- Use