The AnQi CMS endows content management with exceptional flexibility, and the custom parameter function is the key to meeting the needs of personalized content display.How to accurately present the unique parameters set for articles, products, or category content models on the website front-end template is a concern for many users.This article will elaborate on this process, helping you fully utilize the powerful customization capabilities of Anqi CMS.
Learn the mysteries of custom parameters
First, ensure that custom parameters can be displayed in the frontend template, the core lies in the backend content model definition and data entry.In the Anqi CMS backend management interface, you need to go to the "Content Management" section, click on the "Content Model".Whether it is the built-in article model, product model, or a custom model created by yourself, you can add 'Content Model Custom Field' in its settings.
When adding custom fields, there are several key pieces of information to keep in mind: 'Parameter name' is the name of this field displayed to you in the background, convenient for management;The 'invoking field' is truly important part, it determines the name used to access the parameter in the template, and this name is usually recommended to use letters, and keep it unique.In addition, the 'field type' determines the input form of the parameter, such as single-line text, multi-line text, numbers, single selection, multiple selection, or drop-down selection, which also affects how you process the data after obtaining it in the front-end 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 a "article model" and set its "field call" toauthorSo when you publish articles, you can fill in author information for each article.
Let parameters shine in front-end templates.
Once you have properly configured the custom parameters and entered the content in the background, the next step is to present it on the website front end.The AnQi CMS template engine provides a simple and powerful way to read these custom data.
1. Directly call a single custom field
If you clearly know the name of a custom field's 'call field' and the field stores a single value (such as the author of an article, product model), you can directly use the double curly bracket syntax to call it. The most common way is to usearchiveDetailThe tag, which is specifically used to retrieve detailed data of the document, including its custom parameters.
For example, if you want to display the settings you made before on the article detail page.authorField, you can write the template code like this:
<div>文章作者:{% archiveDetail with name="author" %}</div>
Or, if the current page context is already a document (archive) itself, you can also access it directly through object properties:
<div>文章作者:{{ archive.author }}</div>
Herename="author"orarchive.authorAll point to the "call field" name set in the background content modelauthorthe parameter.
2. Loop through all custom fields.
Sometimes, you may want to dynamically display all the custom parameters of an item in an area without listing them one by one in the template. At this point,archiveParamsThe tag comes in handy. It can retrieve all custom parameters of the current document (or a document specified by ID) and return them as an array, where you can iterate over this array to display the name and value of each parameter.
This is an example of a template code that iterates 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 you set in the background (for example, "Article Author") whileitem.ValueIt will 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 flexibly handled after acquisition:
Multi-line text or rich text editor content:If your custom field is multiline text or a rich text editor content (such as a Markdown editor) that may contain HTML tags. In order for this content to be rendered correctly instead of displaying the raw 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 will first convert it to HTML.<div>文章摘要:{{ archive.abstract|render|safe }}</div>Multiple image upload, multiple selection, drop-down selection, etc:When the custom field type allows multiple selections (such as image groups, multiple choices), Anqi CMS usually returns 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 for uploading 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 of specific field:
archiveParamsTags also support throughsorted=falseThe parameter is obtained in the form of a Map (key-value pairs), if you need to access a specific field accurately,NameorValuethis method will be more direct.{% archiveParams params with sorted=false %} <div>作者姓名:{{ params.author.Value }}</div> <div>联系电话:{{ params.phone_number.Value }}</div> {% endarchiveParams %}Here
params.author.Valuedirectly obtained the field calledauthorthe value.
By using the above method, you can flexibly and accurately present the custom parameters set in the Anqi CMS backend in the website front-end template, bringing great convenience and extensibility to your website content management.
Frequently Asked Questions (FAQ)
1. I have set custom parameters in the background, but they do not show up in the front-end template. What could be the reason?The most common reason is that the 'call field' name has been entered incorrectly in the template. Please carefully check the custom field 'call field' name in the background content model to ensure that the case is matched perfectly. In addition, also check if you have used the correct tags (for example, in the document detail page, you should use')archiveDetailorarchiveParams), as well as whether the data has been entered into the custom field of this content.
2. How to display rich text content (such as Markdown or HTML) as raw code on the front end instead of formatted styles?This is because the template engine defaults to escaping output content to prevent potential security issues. If your custom fields contain HTML or Markdown content, you need to add it when calling the template.|safeThe filter tells the template engine that this is safe content and does not need to be escaped. If the content is Markdown format and needs to be rendered on the front end, it may also be necessary|rendera filter such as{{ archive.my_rich_text_field|render|safe }}.
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 take advantage ofdefaulta filter such as{{ archive.my_custom_field|default:"未填写" }}, so it will display 'Not filled' when the field is empty.