An enterprise CMS with excellent flexibility and customizability provides powerful content management capabilities for website operators.In daily website operations, we often encounter situations where standard fields (such as title, content, and publish time) cannot fully meet specific content display needs.At this moment, custom document fields become our helpful assistants, such as adding 'author' information to an article or supplementing 'price' attributes for a product.How can we flexibly call these custom fields and display them on the website page in Anqi CMS?
Today, as an experienced website operations expert, I will guide you to deeply understand the definition, calling, and display strategies of custom document fields in Anqi CMS, ensuring that you can transform technical details into practical operations that are easy to understand.
一、Why is it necessary to customize document fields?
One of the core strengths of Anqi CMS is its 'flexible content model' design.In many content management systems, the fields of content are fixed, which greatly limits the expression of content and the extensibility of business.For example, a simple blog post may only need a title, content, and publication date, but if you are going to post product information, you may also need fields such as price, inventory, SKU, brand, and a series of exclusive fields.If all this information is crammed into the "content" field, it's not only a mess to manage but also difficult to structure and present beautifully on the frontend.
The appearance of custom document fields is to break through this restriction.It allows you to add unique, personalized data fields for different content models (such as "article modelSo, each type of content can have the most fitting data structure for its own characteristics, greatly enhancing the fineness of content management and the flexibility of front-end display.
二、Define custom fields in the Anqi CMS backend
To call and display custom fields, we first need to define them correctly in the backend. This process is actually very intuitive:
- Enter the content model management:Login to the Anqi CMS backend, navigate to the "Content ManagementThis list contains all the existing content models in the system, such as the default 'Article Model' and 'Product Model'.
- Select or create a model:You can select an existing model to edit or create a new content model as needed.
- Add custom fields:After entering the model editing page, you will see a section named "Content Model Custom Fields". Click "Add Field" to start defining your exclusive fields.
- Parameter name:This is the name displayed to the administrator on the back-end interface, such as 'Article Author', 'Product Price'.
- Field call:This is the most critical part!It is an English string, which is the 'key name' you use when calling this field in the front-end template.
author/priceThis 'call field' is the 'secret weapon' we will refer to in the template later. - Field Type:Select an appropriate type based on your data characteristics, such as 'Single-line text' (for author name), 'Number' (for price, inventory), 'Multi-line text' (for a brief description of the product details), 'Single selection', 'Multiple selection', or 'Drop-down selection', etc.
- Is it required & default value:Set according to business logic. If a default value is set, the preset default value will be automatically displayed on the front end when you edit the document without filling in this field.
Complete the field definition and save the model.Now, when you go to 'Content Management' to add or edit documents under this model, you will see the custom fields you have defined in the 'Other Parameters' area, where you can enter corresponding data for each document.
In the template, call the custom field and display
Define the fields and enter the data, and then the core step is to display it in the front-end template.The template engine of AnQi CMS (similar to Django syntax) provides powerful tags to help us achieve this.
1. Call a single specific field
When you are on the document detail page (for example, displaying the full content of a blog post), you may need to display specific custom fields of the article, such as the author.
Use
archiveDetailTags:For the document detail page, the most direct method is to usearchiveDetaillabel combined withnameParameter.nameThe value of the parameter is exactly the "field called" you defined in the background.{# 假设你定义了一个“调用字段”为 author 的自定义字段 #} <div>文章作者:{% archiveDetail with name="author" %}</div> {# 假设你定义了一个“调用字段”为 price 的自定义字段 #} <div>商品价格:¥{% archiveDetail with name="price" %}</div>Here are the
archiveDetailLabels will intelligently obtain the document information of the current page. If you want to specify the document fields of a certain ID, you can also do so throughidparameters, for example{% archiveDetail with name="author" id="10" %}.in the document list loop:If you are in
archiveLista loop of tags (such as displaying a list of articles), if you want to display custom fields of list items, you can directly access them through the properties of the loop variable:{% archiveList archives with type="list" limit="10" %} {% for item in archives %} <li> <a href="{{item.Link}}"> <h5>{{item.Title}}</h5> {# 直接访问 item 对象的自定义字段 #} <p>作者:{{item.author}}</p> <p>价格:¥{{item.price}}</p> </a> </li> {% endfor %} {% endarchiveList %}In this example,
itemrepresented the current document object in the loop, you can use it directlyitem.调用字段名称to retrieve the value of its custom field. This method is very suitable for briefly displaying the custom information of each item on a list page.
2. Traverse all custom fields
Sometimes, you may want to display all custom fields of a document within a block, such as the 'Product Parameters' area on a product detail page.archiveParamsThe label comes in handy, it can help you traverse and display all the additional parameters of a document.
Use
archiveParamsTags:This label will retrieve all custom fields of the current document (or the document with a specified ID) and organize them into a reusable list.<div class="product-parameters"> <h3>产品参数</h3> {% archiveParams params %} {% for item in params %} <p> <span>{{item.Name}}:</span> {# item.Name 是你在后台定义的“参数名” #} <span>{{item.Value}}</span> {# item.Value 是该字段的具体值 #} </p> {% endfor %} {% endarchiveParams %} </div>Here,
paramsis a collection of all custom fields, eachitemincludesName(field display name) andValue(field value).archiveParamsTags also supportsorted=true(default value, sorted by background)sorted=false(unordered Map object, can be sorted by)params.调用字段.ValueDirectly fetch the value) in two modes, to meet the needs of different scenarios.
3. Combine different field types with special processing
For different types of custom fields, some additional processing may be required when displayed on the front end:
Multi-line text (rich text):If your custom field type is multi-line text and may contain HTML content (such as, through a rich text editor), in order to ensure that these HTML codes are rendered correctly rather than escaped to plain text, you need to use
|safeFilter:<div>产品特色:{{archive.feature_description|safe}}</div>Image type:If your custom field is an image (for example, product details images uploaded through custom fields), its
ValueIt will be an image URL. If you want to display multiple images, the field is usually configured to support multi-image upload at this time.item.ValueIt may be an image URL array.”`twig {# 假设你定义了一个“调用字段”为 arcimages,它存储了一个图片URL数组 #}
{% archiveDetail arcimages with name="arcimages" %} {% for img_url in arcimages %} <img