Does the `moduleDetail` tag support direct retrieval of custom field values defined in the content model, and if so, how do you call it?
As an experienced website operations expert, I am well aware of the importance of a flexible content model and convenient template calls for a modern CMS.AnQiCMS (AnQiCMS) performs well in this regard, its powerful template tag system makes content presentation both flexible and efficient.Today, let's delve deeply into a question that everyone cares about: moduleDetailDoes the tag support directly retrieving the value of the custom field defined in the content model? If so, how can it be called?
First, let us clarify the division of responsibilities among different tags in Anqi CMS. Anqi CMS divides the data in the content management system into several levels:
- Data at the system level: Such as website name, Logo, contact information, etc., these are through
system/contacttags. - Data at the content model levelThis refers to the metadata of the content model itself, for example, the name, alias, and the table name corresponding to it in the database, etc. This information is exactly
moduleDetailThe tag is responsible for fetching. - Document (content) level dataThis refers to a specific document published through a certain content model (such as a news article, a product detail), including the document title, content, publishing time, as well asIn this content model, an additional custom field is defined for the document.
Understanding this hierarchy, we can better understandmoduleDetailThe function of tags. According to the Anqi CMS document description,moduleDetailLabels (for example intag-/anqiapi-category/3501.html) are mainly used to obtain the details of the content model itself, such as:
- Model ID(
Id) - Model title(
Title) - Model name(
Name) - Model Link(
Link) - Model table name(
TableName) - Model Introduction(
Description)
This information is about the content modelitselfThe attribute, rather than the custom fields owned by the specific document under the model. In other words,moduleDetailIt is used to describe what the 'article model' is like, rather than what specific field values an article has.
Then, when we want to obtain the 'content model' ofSpecific documentWhat tag should be used when defining a custom field value? The answer is:archiveDetailandarchiveParams.
The strength of AnQi CMS lies in its 'flexible content model' feature (such asAnQiCMS 项目优势.mdMentioned), allowing users to customize the content structure according to business needs, such as adding an "author introduction" field to the "article model" or adding "product size", "color", and other fields to the "product model". These custom fields are attached toSpecific document content.
How to obtain the value of the custom field defined in the document?
AnQi CMS provides two main ways to call the document (archive) to obtain the value of the custom field:
Direct pass.
archiveDetailLabel calls a single custom fieldIf you know the name of the custom field's 'call field' (defined in the backend content model management, for exampleauthor_info), and you can directly access the value of a specific custom field in the documentarchiveDetailLabel. For example, you define a field namedauthor_infoYour custom field, you can call it like this on the article detail page:<div>作者简介:{% archiveDetail with name="author_info" %}</div>If the current page context already has
archiveVariable (usually available on the document detail page), you can also access it more succinctly through a variable:<div>作者简介:{{ archive.author_info }}</div>This method is very suitable when you know exactly which custom field to display.
By
archiveParamsLabel traversal or retrieve all custom fields by nameIf you need to get all the custom fields of the current document, or your custom field is a complex type (such as a multi-image group, a multi-choice box),archiveParamsTags provide more powerful features. For example,tag-/anqiapi-archive/146.htmlas shown in the documentation,archiveParamsTags will return a collection containing all custom fields. You can control the returned data structure through thesortedparameters:sorted=true(default): Return an ordered array, you can go throughforLoop through all custom fields. This is very useful when you need to dynamically render all parameters and their values in a template.
Here{% archiveParams params %} <div> {% for item in params %} <p><strong>{{ item.Name }}:</strong>{{ item.Value }}</p> {% endfor %} </div> {% endarchiveParams %}item.NameIs the field display name (such as "author biography"),item.ValueIs the specific value of the field.sorted=false: Returns an unordered map object, you can directly access its value by using the 'field name' of the custom field, just like accessing the properties of an object.
It is particularly noteworthy that when your custom field is of the 'gallery' type, it may store an array of image URLs. At this point, you need to first go through{% archiveParams params with sorted=false %} <div> <p>作者简介:{{ params.author_info.Value }}</p> <p>出版日期:{{ params.publish_date.Value }}</p> </div> {% endarchiveParams %}archiveDetailorarchiveParamsGet this array and then use itforLoop to traverse and display the image:
{# 假设你的组图自定义字段名为 'product_images' #} {% archiveDetail product_images with name="product_images" %} <div class="product-gallery"> {% for img_url in product_images %} <img src="{{ img_url }}" alt="产品图片"> {% endfor %} </div>If your custom field contains rich text content (such as a 'detailed description' field that includes HTML tags), be sure to include it in the output.
|safeA filter to ensure that HTML content can be parsed correctly rather than being escaped:{% archiveDetail with name="long_description" %}{{ long_description|safe }}
Summary
moduleDetailTags and getting custom field values are two different levels of concepts.moduleDetailThe focus is on the structure and metadata of the content model itself, whereas the values of the custom fields defined within the content model need to be accessed througharchiveDetailorarchiveParamsthese tags because the values of these fields are associated withThe specific document (archive)Closely related. Correct understanding and application of these tags can enable you to achieve high customization and flexible content display in Anqi CMS.
Frequently Asked Questions (FAQ)
Q1: I defined a custom field called "Product Specifications" in the content model, should I usemoduleDetailOrarchiveDetailto display it on the product detail page?
A1:you should usearchiveDetailLabel. Because "Product specifications" is fora specificproduct documentation, its value may vary from product to product.moduleDetailThe label can only retrieve the attributes of the "product model" concept itself (such as the name "product model"), and cannot retrieve the "product specification" value of a specific product document. You can call it like this:{% archiveDetail with name="产品规格的调用字段名" %}.
Q2: If I have a content model with dozens of custom fields, how can I list them all without writing them one by onearchiveDetailWhat about the tags
A2:This isarchiveParamsThe place where tags can be used. You can use{% archiveParams params %}tags to get all the custom fields of the current document, it will return an array containing information about these fields. Then you can use aforLoop through this array to dynamically display the names and values of each field without manually listing each field. For example:
{% archiveParams params %}
{% for field in params %}
<p>{{ field.Name }}:{{ field.Value }}</p>
{% endfor %}
Q3: My custom field is 'Product Group Image', which stores multiple images. I usedarchiveDetailgot the value, but it only displays a string of text and not the images. What's the matter?
A3:When you define a custom field of the "Group Image" type in the content model, Anqicms will store these image URLs in the form of an array.Directly outputting the array will display its text representation. To correctly display the images, you need to pass the array of obtained image URLs through anforLoop to iterate through and then generate one for each URL<img>Label. For example:
“`twig
{% archiveDetail product_gallery_images with name=“product_images_field_name” %}
{% for image_url in product_gallery_images %}
<img src="{{ image_url }}" alt="产品图片">