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 publish time, you can also add various custom fields according to business needs 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 form of the website content, making it more in line with the actual application scenario.

So, after you have toilfully defined and filled in these custom fields in the background, how can you elegantly display them on the website front-end page? AnQiCMS provides a special template tag for this—archiveParamsIt is the bridge that connects backend custom data with frontend display.

Understand custom fields: backend settings and content entry.

Further inarchiveParamsBefore using tags, 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" in the "Content Model", and add custom fields to the model. For example, you can add a field named "Article Source" (the field name is called thesourceThe "single line text" type field.

Next, when you publish documents under "Content Management", select the category you have defined with custom fields, and there will usually be a "Other Parameters" collapsible area 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 corresponding custom field data for each document, such as entering the article source as '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 where specific document custom fields need to be displayed).This tag is very flexible in use, 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,变量名称Is the temporary variable name you set for the custom field data, for exampleparamsorcustomFields.参数It is used to control the way data is obtained.

Method one: Traverse all custom fields (default and recommended)

When you want to display all custom fields of a document on a page, or are unsure of which fields to display dynamically, you can usearchiveParamsThe default usage. It will return an array object sorted by background, 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 tag will retrieve all custom field data of the current document and assign it toparamsVariable.
  • {% for item in params %}Loop throughparamsarray.
  • {{ item.Name }}It will output the parameter name set in the background for custom fields (such as "Article Source").
  • {{ item.Value }}It will output the custom field data entered in the document (for example, "Zhihu").

This method is especially suitable for product detail pages, where you can loop through all product specifications (such as brand, model, size, color, etc.), without needing to preset the calling code for each field, which greatly enhances 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 'call field' and you only need to get its value, not traverse all fields, then how many more concise ways are there.

The most direct way: througharchiveObject access

In the document detail page, you can usually access through directlyarchiveObject (AnQiCMS will automatically assign the detail data of the current document to)archiveUsing a variable to access custom fields. You just need to know the "call 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.

ByarchiveParamscooperatesorted=falseGet specified field

If you need to retrieve multiple specific fields and also want to obtain their 'parameter names', or if the current page is not a direct document detail page, you cannot use it directlyarchiveAn object, then you canarchiveParamslabel'ssortedthe parameter tofalseThus,archiveParamsIt will return an object with the key namedmapAn object, you can access the 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(such as "author") andValuetwo properties.authorandsourceIt is the name of the 'call field' set for the custom field in the background content model.

Get the custom field by specifying the document ID.

archiveParamsThe tag also supports passing throughidTo specify the custom field data of a specific document to be retrieved instead of the default data of the current page, which is very useful in certain scenarios where cross-document data retrieval is needed.

{# 获取 ID 为 10 的文档的自定义字段 #}
{% archiveParams otherDocParams with id="10" %}
    {% for item in otherDocParams %}
        <p><strong>{{ item.Name }}:</strong> {{ item.Value }}</p>
    {% endfor %}
{% endarchiveParams %}

Summary

archiveParamsThe tag is a very practical feature in AnQiCMS template creation, which is配合内容模型中的自定义字段, allowing you to flexibly manage and display various types of content data. Whether it is dynamically traversing all fields or accurately locating specific fields, archiveParamsAll can provide concise and efficient solutions, helping you build a rich and clear website page.

Frequently Asked Questions (FAQ)

1. Why do I define custom fields in the background?archiveParamsWhen the tag is used, the front-end page does not display any data?