When using the Anqi CMS for website content management, we often need to display more personalized data beyond the conventional information such as title, summary, thumbnail, etc.This additional information, such as the author of the article, the model of the product, the release location, etc., is achieved through the powerful custom field function of the AnQi CMS.archive/listHow can we elegantly extract and utilize these custom fields when getting the document list through the interface? The answer is hidden in the returned data.extrain the field.
Inarchive/listThe interface returns the data of each document (or article) and you will find a field namedextraThe field. This field is like a treasure box, containing all the custom field information you carefully set for the document.extra字段本身是一个对象(或称作关联数组),它的键名(key)就是您在安企CMS后台内容模型中为自定义字段设置的“表单字段调用名”,而每个键名对应的值则又是一个包含 Englishname/valueanddefaultAn object with three properties.
Let us understand through a specific example. Suppose you have added a custom field named "author" for the "article" content model in the Anqi CMS backend, and its internal call is namedauthorWhen you accessarchive/listthe article list through the interface, if an article has filled in author information, then you will see a structure like this in itsextrafield:
"extra": {
"author": {
"name": "作者",
"value": "AnqiCMS编辑部",
"default": ""
},
"publish_city": {
"name": "发布城市",
"value": "上海",
"default": "北京"
}
}
Here,authorandpublish_cityIt is the call name of the custom field. They each contain:
name: The Chinese display name of the field, for easy understanding.value: The actual content filled in for this custom field.defaultIfvalueIf empty, this default value will be used.
It is very intuitive to get the values of these custom fields. You just need to go through the "name used to call the field" (i.e.,extraField key name ()to access it, and then retrieve the properties from it. For example, if you have already obtained a single document object in your front-end code,valuethe properties.document, then the way to get the author's name isdocument.extra.author.value, the way to get the published city isdocument.extra.publish_city.value.
This feature greatly enhances the flexibility and scalability of content. By customizing fields, you can add exclusive attributes that fully meet the business needs of different content models (such as articles, products, cases, etc.).extraThe field is the key to passing these exclusive properties from the backend to the frontend.This means you can design highly customized display pages based on the content model, without modifying the core code to adapt to various content structures.
In practical development, there are several points to pay special attention to. First, be sure to checkextraDoes the field itself and its internal custom fields exist. Some documents may not have filled in a certain custom field, or the field is not defined at all in the content model, in which case accessing directlydocument.extra.someField.valueIt may cause the program to crash. A robust approach is to first judgeextrawhether it exists, then judge whether the specific custom field exists, and finally try to get itvalue。For example, you can use conditional statementsif (document.extra && document.extra.author) { /* 使用 document.extra.author.value */ }to ensure the robustness of the code. Next, whenvaluethe field is empty, you can consider usingdefaultthe field as alternative display content to improve user experience.
Additionally, if you not only need to get the value of a custom field, but also want to know its type (such as text, number, or image), or whether it is a filterable field, etc., metadata information, then you can further utilize the information provided by the Anqi CMS.module/detailInterface. This interface can return the detailed definitions of all custom fields of the specified content model, including theirfield_name/name/type/is_filterThe property, which is very useful for front-end scenarios that require dynamic form rendering or different processing based on field type.
In short,archive/listReturned by the interface.extraThe field provides a unified and flexible mechanism for handling custom field information in the security CMS.Understand and make good use of this mechanism, which will make your website content display more depth and personalization, thereby better meeting user needs.
Common Questions (FAQ)
1.extraWhy are some fields sometimes empty (null)?When you have not set any custom fields for the document type in the content model of the AnQi CMS backend, or although fields have been set, all custom fields in the document instance are not filled in with any values (including default values), thenarchive/listReturned by the interface.extraField may be displayed asnull. When handling in the frontend, it is recommended to checkextrawhether the field isnull, to avoid errors caused by trying to access the properties of an empty object.
2. How to know the 'call name' of a custom fieldfield_name)?The 'label name' of the custom field is the unique identifier you set when creating or editing the field in the Anqi CMS backend content model.If you forget the call name of a field, you can go to the corresponding content model management interface in the background to view it.module/detailinterface (pass the corresponding model ID) to get all the detailed definitions of custom fields under the model, includingfield_nameproperties.
3.extraField withindefaultWhat is the purpose of the value?
defaultThe value is in the custom fieldvalueAn alternative value provided when empty. For example, if you set a "author" field and specify the default value "anonymous", then when a document does not explicitly fill in the author, extra.author.valueMay be empty, butextra.author.defaultwill still be "Anonymous". This can be used as a fallback solution when displaying content to ensure that even if there is no specific entry, a friendly prompt or preset content is displayed.