When using Anqi CMS to manage your website content, you may find that relying solely on the default fields such as title, content, and abstract cannot fully meet your personalized display needs.For example, you want to display "author" and "sourceThe AutoCMS provides powerful custom parameter functions, allowing you to easily add, manage, and display these personalized information for different types of documents (such as articles, products).
This article will introduce in detail how to obtain and display custom parameters of specified documents in the Anqi CMS, such as author, source, etc., to help you create a more rich and customized content display.
English in the background to configure custom parameters and enter content
To retrieve and display custom parameters, you first need to configure them in the AnQi CMS backend. This process is divided into two steps: defining the parameters, and then entering the values of these parameters for the documents.
1. Define custom fields of content models
Custom parameters are associated with the 'Content Model'. Each content model (such as 'Article Model', 'Product Model') can have its own unique custom fields.
- Enter the content model management:Log in to the AQ CMS backend, navigate to“Content Management”the menu, and then click“Content Model”.
- Select or create a model:Here, you can choose to edit an existing content model (such as "Article Model") to add new fields, or create a completely new content model.
- Add custom fields:In the selected content model editing page, find“Content Model Custom Fields”the area, click "Add Field". You will see the following key settings:
- Parameter name:This is the Chinese name displayed on the backend interface for operators, such as "Article Author
- Field call:This is the unique identifier used in the front-end template to call this parameter, it must use English letters, and it is recommended to use camel case or lowercase underscore naming, for example
author/source/materialThis field name is crucial, it will be used when the frontend calls it. - Field Type:Choose the appropriate field based on the data type you need to store, such as 'Single-line text' (suitable for author, source), 'Multi-line text' (suitable for longer introductions), 'Numbers', 'Single choice', 'Multiple choice', or 'Drop-down selection', etc.
- Mandatory:Based on business requirements, decide whether this field is required.
- Default value:If this field has common values, you can set a default value.
- Save configuration:Complete the field settings and click the Save button to make the changes take effect.
2. Enter the custom parameter value for the document entry.
Define custom fields for content models, and you can fill in specific content for these fields when publishing or editing the corresponding model documents.
- Enter the document editing interface:Navigate to:“Content Management”clickPublish DocumentOr edit an existing document.
- Enter custom parameters:Below the document editing page, there will be aOther ParametersThe folding area.Expand this area, and you will see the custom fields just defined for this content model, as well as the corresponding input boxes or selection items.Here, you can fill in information such as 'Author of the Article' and 'Source of Content' for the current document.
- Save Document:After filling in, save the document. The values of these custom parameters will be stored with the document.
二、In the front-end template, retrieve and display custom parameters
Normally, you need to add the following code under your template directory{模型table}/detail.html[for example]article/detail.htmlorproduct/detail.html)file.
1. Directly call the specified custom parameter (recommended for simple scenarios)
The most direct method is to use for specific fields like 'author' and 'source' that are displayed only once on the pagearchiveDetailLabel.
Assuming you have defined 'Author' (field calledauthor') and 'Source' (field calledsource) two custom fields in the content model.
{# 在文档详情页,您可以直接使用 archive 对象来调用自定义字段 #}
<p>作者:{{ archive.author }}</p>
<p>来源:{{ archive.source }}</p>
{# 或者,使用 archiveDetail 标签,这在当前页面不是文档详情页,但您知道文档ID时非常有用 #}
<p>作者:{% archiveDetail with name="author" %}</p>
<p>来源:{% archiveDetail with name="source" %}</p>
{# 如果自定义参数的内容是富文本或 Markdown 格式,需要加上 |safe 过滤器,Markdown 内容还需 |render 过滤器 #}
<div class="introduction">
产品简介:{{ archive.introduction|render|safe }}
</div>
Description:
- In the document detail page, the system usually automatically provides a variable named
archivewhich contains all the information of the current document, including custom parameters. You can access{{ archive.调用字段名 }}of the form directly access. {% archiveDetail with name="调用字段名" %}This method is more general, whether the current page is directly a document detail page or not, as long as the system can recognize the current document context or you specify a document ID, it can retrieve the data.|render|safeIf your custom field type is a rich text editor, and the content may contain HTML tags or Markdown syntax, in order for this content to be displayed correctly rather than as plain text, you need to add|safeFilter. If the Markdown editor is enabled at the same time, it is necessary to enable it first|renderthen|safe.
2. Loop through all custom parameters (for dynamic display)
If you want to dynamically display all custom parameters of a document in a region, for example, to uniformly display all specification parameters on a product detail page, thenarchiveParamsThe tag will be very convenient. It will return a list of custom parameters, which you can iterate through and display their names and values.
<div class="custom-parameters">
<h3>详细参数</h3>
{% archiveParams params %}
{% for item in params %}
<p><strong>{{ item.Name }}:</strong> {{ item.Value|safe }}</p>
{% endfor %}
{% endarchiveParams %}
</div>
Description:
{% archiveParams params %}Will retrieve all custom parameters defined in the current document and assign them toparamsa variable.paramsIs an array object, each element of which containsName(parameter name, such as “article author”) andValue(parameter value).- Pass
{% for item in params %}Loop, you can sequentially retrieve each parameter's name and value for display. - Similarly,
|safeThe filter is also very important here to prevent HTML content from being parsed in parameter values.
Three, How to Use and Points for Attention
- Consistency of Field Names Used:Ensure that the name used to define 'Called Field' in the background is exactly the same as the one you use in the template (including the case).
- Data type matches:When defining field types in the background, please select according to the actual content. For example, if the custom field is a multi-select type, then on the front end
item.ValueMay be a comma-separated string, you may need to perform additional processing (such as using|split:","a filter to convert it to an array and then iterate over it). - Template Caching:After modifying the template file, if the page does not update immediately, please try clearing the system cache or browser cache of the AnQi CMS.
- [en] Style beautification:The above code is just for functional implementation, in actual application, you still need to use CSS to beautify these custom parameters so that they match the overall style of the website.
By using the above method, you can fully utilize the custom parameter function of the Anqi CMS, adding more dimensions and personalized display to your website content, thereby enhancing user experience and content value.
Common Questions and Answers (FAQ)
Q1: I have set custom parameters in the background, but why can't I see it in the 'Other Parameters' section of the document editing page?A1: Please check if you have setThe content model of the current documentAdded custom parameters.For example, if you are editing an article, the custom parameters must be defined under the 'article model'.Also, make sure to click the save button after defining the custom field, and you may need to refresh the page for it to display.
**Q2: I am calling a custom parameter in the template, and it displays plain text without parsing HTML tags or Markdown content