As an experienced website operations expert, I am well aware of the importance of a flexible content model and convenient template invocation for a modern CMS.The AnQiCMS (AnQiCMS) performs well in this aspect, with its powerful template tag system making content presentation both flexible and efficient.moduleDetailDoes the label support directly obtaining the value of the custom fields defined in the content model? If so, how can it be called?
Firstly, let's clarify the division of responsibilities for different tags in the Anqi CMS. The Anqi CMS categorizes data in a content management system into several levels:
- System-level data:Such as website name, Logo, contact information, etc., these are called through
system/contactby the tag. - data at the content model levelThis refers to the metadata of the content model itself, such as the name, alias, and the corresponding table name in the database it corresponds to. This information is exactly
moduleDetailresponsible for acquiring by the tag. - Document (content) level dataThis refers to a specific document published through a content model (such as a news article, a product detail), including the document title, content, and publication time,In this content model, custom fields are defined for this document..
Understanding this hierarchy, we can better understand.moduleDetailThe function of tags. According to the document description of Anqi CMS,moduleDetailLabel (for example intag-/anqiapi-category/3501.html) is 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 modelitselfproperties, rather than the custom fields owned by specific documents under the model. In other words,moduleDetailIt is used to describe 'what the article model looks like', not 'what specific fields a particular article has.'
Then, when we want to get the content model thatSpecific documentWhat tag should be used when defining a custom field value? The answer is:archiveDetailandarchiveParamsLabel.
The strength of the Anqi CMS lies in its "flexible content model" feature (such asAnQiCMS 项目优势.mdMentioned in the article), allows users to customize 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 get the value of the custom field defined in the document?
Anqi CMS provides two main ways to call the document (archive) to get the value of the custom field:
Proceed directly
archiveDetailLabel call single custom fieldIf you know the "Call field" name of the custom field (defined in the backend content model management, for exampleauthor_info),and only need to get the value of a specific custom field in the document, you can directly usearchiveDetailFor example, you have defined an article model with a nameauthor_infoThe 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
archiveVariables (usually present by default on the document detail page), you can also access them more succinctly through variables:<div>作者简介:{{ archive.author_info }}</div>This method is very suitable when you know exactly which custom field you want to display.
Pass
archiveParamsLabel traversal or retrieval of all custom fields by nameIf you need to get all custom fields of the current document, or your custom field is a complex type (such as a multi-image group, multiple choice box),archiveParamsThe tag provides more powerful functions. Astag-/anqiapi-archive/146.htmlthe document shows,archiveParamsthe tag will return a collection containing all custom fields. You can control the returned data structure through thesortedparameters:sorted=true(Default)Returns an ordered array, you can iterate over all custom fields. This is very useful when you need to dynamically render all parameters and their values in a template.forLoop 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 display name of the field (such as “Author bio”),item.ValueIs the specific value of the field.sorted=falseReturns an unordered map object, you can access its value directly through the 'field name' of the custom field, just like accessing the properties of an object.
Especially worth mentioning is that when your custom field is of the "group image" 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 %}archiveDetailorarchiveParamsObtain this array and then useforLoop 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 (for example, a 'detailed description' field containing HTML tags), please make sure to include it in the output.
|safeFilter, to ensure that HTML content is parsed correctly rather than escaped:{% archiveDetail with name="long_description" %}{{ long_description|safe }}
Summary
moduleDetailTag and retrieving custom field values are two different concepts.moduleDetailFocuses on the structure and metadata of the content model itself, while the values of the custom fields defined in the content model need to be accessed througharchiveDetailorarchiveParamsThese tags to obtain, as the values of these fields are related toThe specific document (archive)Closely related. Correct understanding and application of these tags can enable you to achieve a high degree of customization and flexible content display in the Anqi CMS.
Common Questions (FAQ)
Q1: I defined a custom field called 'Product Specifications' in the content model, should I use it in the product detail page?moduleDetailOrarchiveDetailto display it?
A1:you should usearchiveDetailLabel. Because "Product specifications" is fora specificproduct document, its value may vary from product to product.moduleDetailThe label can only obtain the attributes of the "product model" concept itself (such as the name "product model{% archiveDetail with name="产品规格的调用字段名" %}.
Q2: How can I list all the custom fields in a content model that has dozens of them, without writing them one by one?archiveDetailtags?
A2:This is exactlyarchiveParamsThe place for tags. You can use{% archiveParams params %}tags to get all the custom fields of the current document, it will return an array containing this information. Then you can through aforLoop through this array, dynamically display the name and value 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 havearchiveDetailgot the value, but it only displays a string of text, not the image. What's wrong?
A3:When you define a custom field of the "Group Image" type in the content model, the safe CMS will store these image URLs in the form of an array.Directly outputting an array will display its text representation.forLoop to iterate, 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="产品图片">