In AnQiCMS, the flexibility of website content is one of its highlights, which is attributed to its powerful custom field function of the content model.In addition to standard fields such as article title, content, and publication time, you can also add various custom fields according to business requirements for different content models (such as articles, products), such as 'author', 'product model', 'floor area', or 'service features'.These custom fields greatly enrich the expression of the website content, making it more closely fit the actual application scenarios.
Then, after you have diligently defined and filled in these custom fields in the background, how can you elegantly display them on the website's front-end page? AnQiCMS provides a special template tag for this.archiveParamsIt is the bridge that connects backend custom data with frontend display.
Understanding custom fields: settings on the backend and content entry
Going deeperarchiveParamsBefore using the label, let's quickly review the lifecycle of custom fields in the AnQiCMS backend.
Firstly, you need to select or create a model under "Content Management" -> "Content Model", and add custom fields to the model. For example, you can add a field named "Article Source" (field name calledsourceThe "single line text" type field.
Then, when you publish documents under "Content Management", select the category you have defined with custom fields, there will usually be a collapsible area for "Other Parameters" at the bottom of the page.After expanding, you will see all the custom fields you have defined for the model.Here, you can fill in the data for custom fields of each document, such as filling in the article source with "Zhihu" or "Original".
archiveParamsLabel: Get custom field data
archiveParamsThe core function of the label is to help you obtain these additional custom field data on the document detail page (or other pages that need to display specific document custom fields).The usage of this tag is very flexible, you can choose different parameters according to your specific needs to obtain data.
Its basic syntax structure is usually like this:{% archiveParams 变量名称 with 参数="值" %}
Among them,变量名称This is a temporary variable name you set for the custom field data you have obtained, for example,paramsorcustomFields.参数This is used to control the way data is obtained.
方法一:Loop through all custom fields (default and recommended)
When you want to display all custom fields of a document on a page, or are unsure about the fields, you can dynamically display them.archiveParamsIt returns an array object sorted in the background, with each object containing the Chinese name of the field and its value.
{# 假设当前页面是文档详情页,我们将获取到的自定义字段命名为 params #}
<div>
<h3>文档附加信息:</h3>
<ul>
{% archiveParams params %}
{% for item in params %}
<li><strong>{{ item.Name }}:</strong> {{ item.Value }}</li>
{% endfor %}
{% endarchiveParams %}
</ul>
</div>
In this code block:
{% archiveParams params %}The label will retrieve all custom field data from the current document and assign it toparamsa variable.{% for item in params %}to iterateparamsarray.{{ item.Name }}It will output the 'parameter name' set in the background for custom fields (for example, 'Article Source').{{ item.Value }}It will output the data filled in the custom field in the document (for example, "Zhihu").
This method is especially suitable for product detail pages, where you can cyclically display all product specifications (such as brand, model, size, color, etc.), without the need to preset the calling code for each field, greatly enhancing the reusability of the template.
Method two: Directly retrieve the value of a specific custom field
If you know the name of a custom field's 'called field' and only need to get its value, rather than traversing all fields, there are several more concise ways.
The most direct way: througharchiveObject access
In the document detail page, you can usually access it directly througharchivethe object (AnQiCMS will automatically assign the detailed data of the current document to)archiveUsing a variable ()to access custom fields. You just need to know the 'called field' name defined in the content model.
{# 假设您在内容模型中定义了一个调用字段为 'author' 的自定义字段 #}
<p><strong>作者:</strong> {{ archive.author }}</p>
{# 假设您在内容模型中定义了一个调用字段为 'productModel' 的自定义字段 #}
<p><strong>产品型号:</strong> {{ archive.productModel }}</p>
This method is the most concise, provided that you ensurearchiveThe variable is available, and the "call field" name of the custom field is accurate.
PassarchiveParamswithsorted=falseGet the specified field
If you need to retrieve multiple specific fields and also want to get their 'parameter name', or if the current page is not a direct document detail page, you cannot use it directlyarchiveobject, then you canarchiveParamsTagssortedparameter settingsfalselike this,archiveParamswill return an object with the key namemapobject, you can access field data directly by key name.
{# 设置 sorted=false 后,我们将获取到的自定义字段命名为 customFields #}
{% archiveParams customFields with sorted=false %}
<p><strong>{{ customFields.author.Name }}:</strong> {{ customFields.author.Value }}</p>
<p><strong>{{ customFields.source.Name }}:</strong> {{ customFields.source.Value }}</p>
{# 假设 customFields.author 和 customFields.source 存在 #}
{% endarchiveParams %}
in this way,customFields.authoris an object that containsName(parameter name, such as “author”) andValue(field value) two properties.authorandsourceThis is the name of the 'Field Invocation' you set for the custom field in the backend content model.
Specify document ID to get custom field
archiveParamsthe label also supportsidSpecify parameters to retrieve custom field data for a specific document instead of fetching the document on the current page by default. This is very useful in scenarios where data needs to be retrieved across multiple documents.
{# 获取 ID 为 10 的文档的自定义字段 #}
{% archiveParams otherDocParams with id="10" %}
{% for item in otherDocParams %}
<p><strong>{{ item.Name }}:</strong> {{ item.Value }}</p>
{% endfor %}
{% endarchiveParams %}
Summary
archiveParamsLabels are a very practical feature in AnQiCMS template creation, which, when combined with custom fields in the content model, allows you to flexibly manage and display various types of content data. Whether it is dynamically traversing all fields or accurately locating specific fields,archiveParamsCan provide concise and efficient solutions to help you build feature-rich and clear website pages.
Common Questions and Answers (FAQ)
1. Why do I define custom fields in the backgroundarchiveParamsIs there no data displayed on the front-end page when labeling?