On the document detail page, how to call and display the custom model field content of the article?

The flexible content model of AnQiCMS is a powerful tool that allows us to add custom fields for different types of content (such as articles, products, events, etc.) according to the actual needs of the website.This means that our content is no longer limited to fixed titles, summaries, and content, but can carry more diverse and structured information.How do we call and display the content of these custom model fields on the document detail page?

Next, let's explore the various flexible ways provided by Anqi CMS together.

Understand the creation and role of custom model fields.

Before delving into template calls, it is necessary to briefly review the process of creating custom model fields. In the Anqi CMS backend, you can accessContent ManagementContent modelCome manage and create content models. Whether it is the default system models such as "Article Model" and "Product Model", or the models you add yourself, you can add custom fields to them.

When adding fields, there are several key points to note:

  1. Parameter name (display name): This is the field name you see when editing documents in the background, such as "Article Author", "Product Model".
  2. Field call (English name)This is used when calling the field content in the templateUnique identifierIt must be alphabetic. For example, if you set the call field for "Article Author" toauthorThen you will use in the templateauthorGet its value.
  3. Field type: AnQi CMS supports various field types such as single-line text, numbers, multi-line text, single choice, multiple choice, and drop-down selection, etc.Different field types may require different handling when displayed on the front end.

When you are editing a document in the background, if the category associated with the document is linked to a content model, then all the custom fields defined under this content model will be displayed in the "Other Parameters" section for you to fill in the corresponding content.Ensure that these fields are filled in and saved, as this is a prerequisite for the front-end to function successfully.

Call the custom model field content in the document detail page

Once the content of the custom model field is entered in the background, we can then access the corresponding template file on the document detail page (usuallytemplate/您的模板目录/模型表名/detail.htmlordetail-{文档ID}.html),Through the following ways to call and display them.

1. The most direct way: througharchiveObject property calling

This is the most commonly used and simplest way, suitable for most custom fields. In the template of the document detail page, Anqi CMS will automatically wrap all the data of the current document in a namedarchiveIn the object. This object includes the default fields of the document (such asTitle/Content/Description), also includes all the custom model fields you have defined.

You only need to use dot syntax (.It can directly access the content of custom fields. For example, if you have a field namedauthorof the called field:

<p>作者:{{ archive.author }}</p>

If your custom field type is a rich text editor (multi-line text and allows HTML), to ensure that the HTML content can be correctly parsed and displayed in style, you need to use|safeFilter:

<div class="product-introduction">{{ archive.introduction|safe }}</div>

This method is simple and clear, highly recommended as the first choice.

2. UsearchiveDetailLabel calls accurately

archiveDetailTags are specifically used to retrieve document details, they can not only retrieve built-in document fields but also be used to retrieve content of custom model fields. This method is used in some cases where finer control is needed or only specific field values are needed without introducing the entirearchiveIn the case of an object it will be more useful.

Suppose you have a custom call field.product_dimensions(Product size):

<p>产品尺寸:{% archiveDetail with name="product_dimensions" %}</p>

You can also assign the obtained value to a variable and then process it:

{% archiveDetail dimensions_value with name="product_dimensions" %}
<p>产品的具体尺寸是:{{ dimensions_value }}</p>

For some special fields, such as custom album fields (for examplegallery_images), it stores a list of image URLs. You can obtain thearchiveDetailtags and then loop through them to display:

{% archiveDetail gallery with name="gallery_images" %}
<div class="product-gallery">
    {% for img_url in gallery %}
        <img src="{{ img_url }}" alt="产品图" />
    {% endfor %}
</div>

3. Use.archiveParamsLabel traversal or access as needed

If you want to dynamically iterate through all custom fields and their corresponding names and values in a template, or need to directly access a custom field in the form of a key-value pair,archiveParamsTags will be your helpful assistant.

By default,archiveParamsTags will return a sorted array object, each element containingNameand (parameter name)Value(parameter value). You can loop through and display all custom fields like this:

<div class="custom-parameters">
    {% archiveParams params %}
    {% for item in params %}
        <p>{{ item.Name }}:{{ item.Value }}</p>
    {% endfor %}
    {% endarchiveParams %}
</div>

If your custom field is rich text, don't forget to{{ item.Value }}added afterwards|safefilter.

In addition, you can also setsorted=falseto get an unordered onemapAn object (similar to a dictionary), so you can directly retrieve the value of a custom field by its English name (i.e., 'call field') without looping:

{% archiveParams custom_fields with sorted=false %}
    <p>作者邮箱:{{ custom_fields.author_email.Value }}</p>
    <p>是否推荐:{{ custom_fields.is_featured.Value }}</p>
{% endarchiveParams %}

This method is very convenient when you know which specific custom field you want to retrieve and need itsValueproperty (orNameproperty).

Summary and **practice**

Anqi CMS provides a flexible content model and a powerful template tag system, making the invocation of custom model fields intuitive and efficient. In practice:

  • For most scenarios, go directly through{{ archive.your_custom_field_name }}Property calling is the simplest choice.
  • If the custom field is rich text content, be sure to use|safeFilter, for example{{ archive.rich_text_field|safe }}To prevent HTML from being escaped and not displayed correctly.
  • archiveDetailTags are suitable for situations where additional processing or assignment needs to be done for individual fields.
  • archiveParamsThe label is very suitable for scenarios where it is necessary to dynamically display all custom fields, or quickly access specific values in the form of key-value pairs.