The flexible content model of AnQiCMS is a powerful tool that allows us to add dedicated fields for different types of content (such as articles, products, events, etc.) based on 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 several flexible methods provided by AnQi CMS.
Understand the creation and function 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 management→Content ModelManage and create content models. Whether it's the default 'Article Model' and 'Product Model' or the models you have added yourself, you can add custom fields to them.
When adding fields, there are several key points to note:
- Parameter name (display name)'} ]This is the field name you see when editing documents in the background, such as “Article Author”, “Product Model”.
- Field call (English name)This is used when calling the content of this field in the template.Unique identifierIt must be alphabetic. For example, if you set the call field of 'Article Author' to
authorThen, in the template, you will useauthorto get its value. - field typeThe [en] version is: [Support multiple field types such as single-line text, numbers, multi-line text, single selection, multiple selection, and dropdown 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 of the document is associated with a content model, then all the custom fields defined under that content model will be displayed in the 'Other Parameters' section for you to fill in the corresponding content.Make sure these fields are filled in and saved, which is a prerequisite for the front-end to be successfully called.
to call the custom model field content on the document detail page
Once the content of the custom model field has been entered in the background, we can then view the corresponding template file on the document detail page (usually)template/您的模板目录/模型表名/detail.htmlordetail-{文档ID}.html),Through the following ways to call and display them.
1. The most direct way: througharchiveobject property invocation
This is the most commonly used and simplest method, 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 namedarchivethe object. This object includes the default fields (such asTitle/Content/Descriptionetc.), as well as all the custom model fields you have defined.
You only need to use dot notation (.)can directly access the content of custom fields. For example, if you have a field namedauthor:
<p>作者:{{ archive.author }}</p>
If your custom field type is a rich text editor (multiline text and allows HTML), in order to ensure that the HTML content can be correctly parsed and displayed with 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. UsearchiveDetailAccurate tag call
archiveDetailTags are specifically used to retrieve detailed data of documents. They can not only obtain built-in fields of documents but also be used to retrieve the content of custom model fields. This method is suitable for certain scenarios where more fine-grained control is needed or only specific field values are required without introducing the entirearchiveIt can be quite useful in object cases.
Suppose you have a custom field nameproduct_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 group image fields (for examplegallery_images), it stores a list of image URLs. You can get the tags first and then display them in a loop:archiveDetail.
{% archiveDetail gallery with name="gallery_images" %}
<div class="product-gallery">
{% for img_url in gallery %}
<img src="{{ img_url }}" alt="产品图" />
{% endfor %}
</div>
3. UtilizearchiveParamsLabel traversal or on-demand access
If you wish to dynamically iterate over all custom fields and their corresponding names and values in a template, or need to access a specific custom field directly in the form of key-value pairs,archiveParamsLabels will be your helpful assistant.
By default,archiveParamsLabels will return a sorted array of objects, each containingName(parameter name) andValue[en] The parameter value is shown here. You can loop through 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 add{{ item.Value }}after|safeFilter.
In addition, you can also set throughsorted=falseGet an unorderedmapobject (similar to a dictionary), so you can directly get its value by calling the English name of the custom field (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 exactly which custom field you need to retrieve and you need itsValueproperty (orNameproperty).
Summary and **Practice
Anqi CMS makes the invocation of custom model fields intuitive and efficient through its flexible content model and powerful template tag system. In practice:
- For most scenarios, directly using
{{ archive.your_custom_field_name }}attribute calls is the simplest choice. - If the custom field is rich text content, please make sure to use
|safeFilter, for example{{ archive.rich_text_field|safe }}To prevent HTML from being escaped and not displayed properly. archiveDetailThe tag is suitable for situations where additional processing or assignment is needed for a single field.archiveParamsTags are very suitable for scenarios that require dynamically displaying all custom fields, or quickly accessing specific values by key-value pairs based on the field names called.