In AnQi CMS, the flexibility of content is one of our core advantages in website operation and content management.In addition to standardized fields such as titles and main content, we often need to add specific custom information for different types of content, such as a product's 'brand', 'model', 'origin', or an article's 'author', 'source', etc.These custom fields greatly enrich the dimensions of content display, but how to accurately and flexibly present these meticulously entered background data on the front-end page is an important consideration during template development.

This provides by AnQi CMSarchiveParams

Understand the custom field witharchiveParamsbridging role

In the Anqi CMS backend, we can define a series of unique custom fields for different content types (such as the 'Article' model or the 'Product' model).When you add or edit a document, these custom fields will appear as "other parameters" on the editing interface for you to fill in additional information.

archiveParamsTags act as an intelligent bridge, capable of understanding the background parameters you define for the document and securely extracting them for use in front-end templates. Whether you want to list all parameters or focus on a specific one,archiveParamsCan help you easily achieve it.

archiveParamsBasic usage of tags

archiveParamsThe basic structure of the label is.{% archiveParams 变量名称 with 参数... %}{% endarchiveParams %}. We usually use it on the document detail page (such as article detail page or product detail page) because it defaults to retrieving the custom parameters of the current page document.

It supports several key parameters to control the way data is retrieved:

  • idThis parameter is used to specify the custom parameter for the document to be retrieved. It can usually be omitted when used on the document detail page.idIt will automatically retrieve the ID of the current document. But if you want to get the parameters of other documents, you can specify them explicitly byid="文档ID".
  • sortedThis is a very practical parameter, which determines the structure of the data you get.
    • Whensorted=true(This is also the default value) when,archiveParamsIt will return an array of objects arranged according to the background settings. Each element of this array is a containingName(field name, that is, the Chinese name displayed in the background) andValue(The field value, i.e., the specific data you enter) object.
    • Whensorted=falseIt returns an unordered map object when it happens. In this mode, you can directly access the data through the "field name called" (the English name defined in the content model), for exampleparams.your_field_name.Value.
  • siteIdIf you use the multi-site management function of Anq CMS and need to call data from other sites, you cansiteIdspecify it. Generally, we do not need to fill it in.

Two main display methods

MasteredarchiveParamsBy setting the parameters, we can choose different ways to display custom fields according to our needs.

1. Traverse all custom fields (suitable forsorted=trueWhen you do not know the specific field names, or when you are not sure about the custom fields of a document, or when you want to display all defined parameters uniformly, use the default one.

When you are not sure about the specific field names, or when you do not know the custom fields of a document, or when you want to show all defined parameters uniformly, use the default one.sorted=truePattern (or not specifiedsortedparameters), byforLoop traversal is a very convenient method.

Assuming you define custom fields such as "Brand", "Model", "Color", etc. in the background for products, you can display them like this in the product detail page template:

<div class="product-params">
    <h4>产品参数</h4>
    {% archiveParams params %}
        {% for item in params %}
        <p>
            <span>{{ item.Name }}:</span>
            <span>{{ item.Value }}</span>
        </p>
        {% endfor %}
    {% endarchiveParams %}
</div>

In this code block,paramsis an array that contains all custom fields. Each loop,item.Namewill display Chinese names such as "Brand", "Model", and so on,item.ValueIt will display the specific content you fill in the background, such as 'AnQi Technology', 'AQ-CMS-001', 'black', etc.This method is very suitable for dynamically listing all relevant information in areas such as 'Product Parameters' or 'Article Properties'.

2. Directly access specific custom fields (suitable forsorted=falseorarchiveDetailtags)

If you know the name of the 'display field' for a custom field (for example, you define a field to store the article author, whose 'display field' isauthor),and if you want to display only this field, accessing it directly will be more concise and efficient.

Method one: usearchiveParamswithsorted=falsetosortedthe parameter tofalseCan let you directly access values by calling the field name in a way similar to a dictionary (map):

{% archiveParams params with sorted=false %}
    <p>作者:<span>{{ params.author.Value }}</span></p>
    <p>出版社:<span>{{ params.publisher.Value }}</span></p>
{% endarchiveParams %}

HereauthorandpublisherIt is the 'callback field' name set for these custom fields in the content model.

Method two: througharchiveDetailDirectly obtain the tagThis is the most commonly used and simplest way on the document detail page. Anqi CMS provides a more direct approach, you do not need to usearchiveParamsthe loop structure of tags, you can directly go througharchiveDetailLabel to get the value of a single custom field.

For example, on the article detail page, if you have a custom field called "call field" isauthorYou can use it directly:

<p>作者:<span>{% archiveDetail with name="author" %}</span></p>

Even if the current template context already includes the document object (usually on a document detail page, there will be a name calledarchiveThe global variable), you can also access the custom field directly via dot notation:

<p>作者:<span>{{ archive.author }}</span></p>
<p>产品型号:<span>{{ archive.product_model }}</span></p>

This method is particularly suitable for those custom fields that you use frequently and have fixed names, making template code clearer and more readable.

Considerations and tips in practice

  • Field naming and invocation:When defining custom fields in the background, it is recommended to use concise, meaningful English lowercase letters or camel case naming for 'invoked fields', so that they can be easily called in templates (for exampleproduct_modelor `product`