AnQiCMS template inarchiveParamsTags: Easily display all custom document parameters
As an experienced website operations expert, I know that it is crucial to be able to flexibly display and utilize the various properties of documents in a content management system for website personalization and functional expansion. AnQiCMS, as an efficient and customizable content management system, provides rich and powerful template tags, among whicharchiveParamsTags are the core tools that unlock the powerful functionality of custom document parameters.
This article will delve deeper intoarchiveParamsThe use of tags, which helps you display the current document skillfully in AnQiCMS templates, as well as all the custom parameters of specified documents, making your website content display more depth and flexibility.
Why do we need custom parameters?
In AnQiCMS, the design of the content model greatly enhances the flexibility of the system.In addition to basic attributes such as title, content, and category, you can also define various unique "custom parameters" for documents (or product and other content types) according to your business needs.For example, a product introduction may require parameters such as "brand", "model", "color", "stock", and so on; a real estate information may require parameters such as "layout", "area", "price", "renovation situation", and so on.archiveParamsThe use of tags. It builds a bridge between backend data and frontend display, allowing your content to truly realize personalization.
RevelationarchiveParamsLabel: Get the custom parameters of the document
archiveParamsThe tag is used to retrieve all custom parameters configured in the background for the current document or a specified document.Its design aims to allow template developers to easily traverse and display this additional information.
The basic syntax of use is as follows:{% archiveParams params with id="1" sorted=true %}...{% endarchiveParams %}
Among them:
paramsThis is the variable name you define for the custom parameter set you obtain. You can name it according to your preference.customFields/extraDataOnce defined, this variable name must be used within the tag.idThis is an optional parameter. If you do not fill inidThe tag will default to obtaining the custom parameters of the currently browsing document. If you need to obtain the parameters of another specific document, you can set it to the ID of that document, for exampleid="123".sorted: This is a very critical optional parameter, defaulting totrue.- When
sorted=trueat (the default behavior),paramsThe variable will be aan ordered array of objects, each element of the array is a container that includesNameandValueattribute key-value pairs. This pattern is very suitable for passing throughforLoop through all parameters and display them. - When
sorted=falsethen,paramsThe variable will be aUnordered map objectIn this mode, you can directly access specific parameters by the name of the 'call field', for exampleparams.yourCustomFieldName.Value.
- When
Next, we will demonstrate these two modes through specific examplesarchiveParamsThe powerful function of tags.
Display all custom parameters of the current document in AnQiCMS templates.
1. Traverse and display all custom parameters (recommended method)
This is the most commonly used and most direct way, suitable for the scenario where you want to list all the additional information of the document. Due tosortedThe parameter is set to default.true, we usually omit it and useforto iterate in a loop.
Assuming your document (such as a product) has customized parameters such as "brand", "model", and "launch date" in the background. In the template, you can display them like this:
<div class="custom-parameters">
<h3>产品详细参数</h3>
{% archiveParams params %}
{% for item in params %}
<div class="param-item">
<span class="param-name">{{ item.Name }}:</span>
<span class="param-value">{{ item.Value }}</span>
</div>
{% endfor %}
{% endarchiveParams %}
</div>
In this code block:
{% archiveParams params %}Initialize and retrieve all custom parameters of the current document, stored inparamsthe variable.{% for item in params %}:Traverseparamsarray, each loopitemrepresents a custom parameter.{{ item.Name }}:Display the custom parameter name defined in the background (i.e., the Chinese name or display name).{{ item.Value }}:Display the actual value stored in the background for the custom parameter.
In this way, regardless of how many custom parameters the background adds to the document, the template can automatically adapt and display them one by one without modifying the template code.
2. Display specific custom parameters (via the name of the parameter's 'call field')
If you are only concerned with a specific custom parameter in the document, or if you know the name of the "call field" for that parameter, you can usesorted=falsemode. In this mode,paramsIt will act as an associative array (or a map in Go language), you can access it directly through its 'access field' name.
Suppose you have a custom parameter and its 'access field' isproductPriceIts "parameter name" is "product price". You can get and display it like this:
<div class="product-info">
{% archiveParams data with sorted=false %}
<p>产品价格:<strong>{{ data.productPrice.Value }}</strong></p>
<p>产品库存:<span>{{ data.productStock.Value }}</span></p>
{# 如果自定义字段本身是富文本或包含HTML,记得使用 |safe 过滤器防止转义 #}
{% if data.longDescription %}
<div class="product-description">
{{ data.longDescription.Value|safe }}
</div>
{% endif %}
{% endarchiveParams %}
</div>
Here, dataThe variable is now a map, you can access it by:data.productPrice.ValueDirectly access the name ofproductPriceThe value of the custom parameter. This method is very useful when you need to precisely control the display position and style of a specific parameter.
3. Get the custom parameters of other documents
archiveParamslabel'sidThe attribute allows you to easily access custom parameters from non-current documents. This is very practical in scenarios such as building product recommendations, article list summaries, etc.
Assuming you are currently browsing an article but want to display another ID in the sidebar, you can do this:100parameter of the product document, you can do this:
<div class="related-product-info">
<h4>推荐产品型号</h4>
{% archiveParams relatedProduct with id="100" sorted=false %}
{% if relatedProduct.productModel %}
<p>型号:{{ relatedProduct.productModel.Value }}</p>
{% else %}
<p>未找到相关产品型号信息。</p>
{% endif %}
{% endarchiveParams %}
</div>
Byid="100"We specified the parameters to retrieve the document with ID 100.
4. Cross-site invocation (advanced usage)
If you are using the AnQiCMS multi-site management feature and need to call the document custom parameters of other sites, you can usesiteIdParameters. For examplesiteId="2"Retrieve the document parameters of the site with ID 2. However, for most needs to display the document parameters of the current site, this parameter is usually not required.
Practical suggestions
- Define background parameters clearly: When setting the content model in the AnQiCMS backend, set clear 'parameter names' (for display) and 'call fields' (for template calls) for each custom parameter, which will help you use it more efficiently
archiveParams. - Pay attention to data types and security: If the custom parameters you define in the background are the content of a rich text editor, they may contain HTML tags. Be sure to use them when displaying on the front end.
|safeFilter, for example{{ item.Value|safe }}To ensure that the HTML content is rendered correctly rather than being escaped. At the same time, please make sure that your content source is reliable to prevent XSS attacks. - Handle null values: In use
item.Valueordata.fieldName.ValueIt is best to make a judgment to prevent a custom parameter from not being filled in, which may cause the page to display blank or unattractive. You can use{% if item.Value %}or{% if data.fieldName.Value %}Perform conditional judgment.
Summary
archiveParamsTags are an indispensable tool in AnQiCMS template development, providing a flexible way to retrieve and display custom document parameters. Whether it's traversing all parameters to build a detailed information list, pinpointing specific parameters to highlight key information, or accessing data across documents, archiveParamsCan easily cope with. Master itsidandsortedThe parameter usage will take you to a higher level in AnQiCMS content operation and template customization, providing users with a richer and more personalized website experience.
Frequently Asked Questions (FAQ)
Q1: Why did I use in the templatearchiveParamsLabel, but no content is displayed?
A1: This usually has several reasons.First, please check if you have defined custom parameters for the document type in the AnQiCMS backend content model, and actually filled in the values of these parameters when editing the document.sorted=falseAnd try to go throughparams.yourFieldName.ValueAccess, make sureyourFieldNameIs the background-defined 'call field' name, and it must match the case. Finally, confirm yourforDoes the loop syntax correct as well{% endarchiveParams %}Are the tags closed correctly