The Auto CMS has given content management great flexibility, and the custom parameter feature is the key to meeting the needs of personalized content display.How can you accurately display the unique parameters you set for content models such as articles, products, or categories in the website front-end template?This article will elaborate on this process in detail, helping you fully utilize the powerful customization capabilities of the Anqi CMS.
Understand the mysteries of custom parameters
Firstly, it is important to ensure that custom parameters can be displayed in the front-end template, which is core to the model definition and data entry on the back-end.In the background management interface of AnQi CMS, you need to go to the "Content Management" section, click on "Content Model".Whether it is a system-built article model, product model, or a custom model created by you, you can add 'Content Model Custom Field' in its settings.
When adding custom fields, there are several key information points to be aware of: "Parameter name" is the name of this field displayed to you in the background for easy management; while "Call field" is the truly important part, which determines the name used to access the parameter in the template. This name is usually recommended to use English letters and to be unique.Additionally, the "field type" determines the input format of the parameter, such as single-line text, multi-line text, numbers, single selection, multiple selection, or dropdown selection, etc., which also affects how you handle the data after obtaining it in the frontend template.It is equally important to set appropriate "default values" for these fields, which can provide alternative content when you do not manually fill in the parameters.
For example, if you add a custom field named "article author" to an "article model" and set its "field call" toauthorSo when you publish articles, you can fill in author information for each article.
Let the parameters shine in the front-end template.
Once you have properly configured the custom parameters in the background and entered the content, the next step is to present it on the website front end.The template engine of AnQi CMS provides a simple yet powerful way to read these custom data.
1. Directly call a single custom field
If you clearly know the "name of the called field" of a custom field and the field stores a single value (such as the author of the article, the model of the product), you can directly use the double curly bracket syntax to call it. The most common way is to usearchiveDetailLabel, it is used specifically to retrieve detailed data of the document, including its custom parameters.
For example, if you want to display theauthorField, it can be written like this template code:
<div>文章作者:{% archiveDetail with name="author" %}</div>
Or, if the current page context is already a document (archive) itself, it can also be accessed directly through object properties:
<div>文章作者:{{ archive.author }}</div>
Here are thename="author"orarchive.authorAll point to the 'Call field' named that you set in the background content modelauthorThe parameter.
2. Loop through all custom fields
Sometimes, you might want to dynamically display all custom parameters of a content item in a region without listing them individually in the template. At this point,archiveParamsLabels come in handy.It can retrieve all custom parameters of the current document (or the document with a specified ID) and return them as an array. You can iterate over this array to display the name and value of each parameter.
The following is an example template code that traverses and displays all custom parameters:
{% archiveParams params %}
<div>
{% for item in params %}
<div>
<span>{{ item.Name }}:</span>
<span>{{ item.Value }}</span>
</div>
{% endfor %}
</div>
{% endarchiveParams %}
In this example,item.NameIt will display the "parameter name" (such as "Article Author") that you set in the background,item.ValueThen display the specific content of the parameter.
3. Flexibly handle complex data types
The types of custom fields are diverse, and they also need to be handled flexibly after retrieval:
English text or rich text editor content:If your custom field is multi-line text or rich text editor (such as Markdown editor) content, which may contain HTML tags. In order for this content to be rendered correctly instead of displaying the original code, you need to use
|safeFilter. If the Markdown editor is enabled and the content is in Markdown format, you may also need|renderThe filter first converts it to HTML.<div>文章摘要:{{ archive.abstract|render|safe }}</div>Multiple image upload, multiple selection, dropdown selection, etc: When the custom field type allows multiple values to be selected (such as image groups, multiple selections), the safe CMS will usually return it as an array or slice. You need to use
forLoop to iterate through these values.For example, if you have a variable named
product_imagesCustom field used to upload multiple product images:{% archiveDetail productImages with name="product_images" %} <ul class="product-gallery"> {% for img in productImages %} <li><img src="{{ img }}" alt="产品图片" /></li> {% endfor %} </ul> {% endarchiveDetail %}Attribute access to a specific field:
archiveParamsTags also support throughsorted=falseThe parameters are obtained in the form of Map (key-value pairs) to get custom fields, if you need to access a specific field exactly,NameorValuethis method will be more direct.{% archiveParams params with sorted=false %} <div>作者姓名:{{ params.author.Value }}</div> <div>联系电话:{{ params.phone_number.Value }}</div> {% endarchiveParams %}Here are the
params.author.Valueit directly gets the field calledauthorthe value.
By using the above method, you can flexibly and accurately present the customized parameters set in the Anqi CMS backend in the website front-end template, bringing great convenience and extensibility to your website content management.
Common Questions (FAQ)
1. I have set custom parameters in the background, but they do not display in the front-end template either. What could be the reason?The most common reason is that the name of the "field name" is entered incorrectly in the template. Please carefully check the "field name" of the custom field in the background content model, ensuring that the case is completely matched. In addition, also check if you have used the correct tag (for example, on the document detail page, you should use)archiveDetailorarchiveParams),as well as whether the data has been entered into the custom field for this content.
2.The rich text content (such as Markdown or HTML) in the custom field is displayed as raw code on the front end instead of formatted styles. How should we solve this?This is because the template engine defaults to escaping output content to prevent potential security issues. If your custom field contains HTML or Markdown content, you need to add it when calling the template.|safeA filter to tell the template engine that this is safe content and does not need to be escaped. If the content is in Markdown format and needs to be rendered on the front end, it may also|renderFilter, such as{{ archive.my_rich_text_field|render|safe }}.
3. How to determine if a custom field exists in a front-end template and display a default placeholder when there is no content?You can useifCondition judgment to check if the field contains content. For example,{% if archive.my_custom_field %}{{ archive.my_custom_field }}{% else %}暂无内容{% endif %}. For fields with default values, you can also usedefaultFilter, such as{{ archive.my_custom_field|default:"未填写" }}, so that "Not filled" is displayed when the field is empty.