Manage and display content in AnQiCMS, custom fields play a crucial role, allowing our website to flexibly carry various unique information, not limited to general attributes such as title and content.Whether it is adding detailed technical parameters for product detail pages or supplementing author profiles and external reference links for articles, custom fields can provide strong support.Understanding how to call and display these custom fields in the article detail page will greatly enhance the richness and customization of the website content.
One of the design philosophies of AnQiCMS is to provideFlexible Content ModelThis is the basis on which custom fields can take effect.Through custom content models, you can define various content types according to business needs, and add dedicated fields for each type to meet personalized content display requirements.
Define and manage custom fields: the foundation of content models
In AnQiCMS backend, the management of custom fields is concentrated under the "Content Model" in the "Content Management" module.System default provides "Article Model" and "Product Model", you can also create your own content model.Enter any content model, and you can find the "Content Model Custom Fields" area here, where you can add new fields as needed.
Each custom field needs to specify a 'parameter name' (used for display and identification in the background) and a 'call field' (the actual name referenced in the template). Field types are diverse, including:
- Single Line Text:适用于简短的文字描述,如作者名、产品型号。
- Number:确保输入的内容是纯数字,例如价格、库存。
- Multi-line text:适合较长的文本内容,如产品特性描述、作者履历。
- Single selection/Multiple selection/Drop-down selection:提供预设选项供选择,适用于标准化数据,如产品颜色、尺寸。
Properly setting field types and calling field names is the premise for accurately calling data in templates later.
Calling custom field content in the article detail page
Once the custom field is defined and content is filled for a specific article, the next step is to display it in the template.AnQiCMS provides two main ways to call these custom fields: directly calling specific fields and iterating through all fields.
1. Directly call a single custom field
If you know the "Call Field" name of the custom field and the field is a single value (such as single-line text, number, or multi-line text), you can use it directly.archiveDetailTag to get its value. For example, if you create a single-line text field namedauthora single-line text field:
<div>作者:{% archiveDetail with name="author" %}</div>
Or, you can assign the field value to a variable and then perform subsequent operations, which is very useful when you need to process the value:
{% archiveDetail articleAuthor with name="author" %}
<div>作者名称:{{ articleAuthor }}</div>
Through this method, you can accurately display custom information on the article detail page at the specified location.
2. Loop through all custom fields
When you want to dynamically display all custom fields of an article, or are unsure of the specific names of each field,archiveParamsThe tag will be very useful. It will retrieve all custom fields of the current article and provide them as an array (or object) to the template.
{% archiveParams params %}
<div>
{% for item in params %}
<div>
<span>{{ item.Name }}:</span>
<span>{{ item.Value }}</span>
</div>
{% endfor %}
</div>
{% endarchiveParams %}
In this code,paramsis an array containing all custom fields, eachitemall have,Name(parameter name, which is the Chinese name displayed in the background) andValue(field value) property. This method is particularly suitable for product parameter lists or dynamic display of additional information.
3. Handle specific types of custom fields
Some custom field types require special handling when displayed, such as image galleries or multi-select fields. Suppose you define a field namedarcimagesThe group field, used to upload multiple images:
{% archiveDetail arcimages with name="arcimages" %}
<ul class="article-gallery">
{% for imgUrl in arcimages %}
<li><img src="{{ imgUrl }}" alt="文章图片" /></li>
{% endfor %}
</ul>
Here,arcimagesIt will return an array of image URLs, you can go throughforand display each image.
If the custom field stores rich text content, which may contain HTML tags, in order to prevent these tags from being escaped into plain text, you need to use|safeFilter:
{% archiveDetail articleContent with name="自定义富文本字段名" %}
<div>{{ articleContent|safe }}</div>
If the custom field contains Markdown formatted content and you want it to be parsed as HTML, you can use|renderFilter:
{% archiveDetail articleMarkdown with name="自定义Markdown字段名" %}
<div>{{ articleMarkdown|render|safe }}</div>
Practical Tips
- Debugging toolIn the development process, if the structure or value of a custom field is uncertain, you can use
|dumpa filter to print the details of the variables, for example{{ params|dump }}This will display the type and content of the variable, helping you quickly locate the problem. - Conditional judgmentIn displaying custom fields, it is best to add conditional judgments to prevent unnecessary blank spaces or errors on the page when the field is empty. For example
{% if item.Value %}or{% if arcimages %}. - Naming conventions:Select clear and meaningful 'call field' names for custom fields, which will make the template code easier to read and maintain.
- Filter applied:flexibly use the various filters provided by AnQiCMS (such as
|thumbto get thumbnails,|truncatecharsUsed for text extraction and so on, which can better beautify and process the display effect of custom fields.
By using the above method, you can fully utilize the custom field feature of AnQiCMS to build a powerful, content-rich, and highly personalized article detail page, meeting various complex display needs.
Common Questions (FAQ)
1. Why is there no content displayed when I call the custom field in the template?This usually has several reasons:
- Field name is incorrect: Please check the template
name="自定义字段名称"The part is whether it is completely consistent with the "Calling Field" name set in your "Content Model" on the backend, including case sensitivity. - Unsaved content of the articleConfirm whether you have entered content and successfully saved for this custom field while editing the article.
- The field type does not matchFor example, if the custom field is a multi-select type, you try to use
{% archiveDetail with name="字段名" %}Directly retrieving a single value may result in not getting the expected result. For multi-select or group images, etc., it is usually necessary to traverse its contents.
2. How to use different custom fields for different categories of articles?In AnQiCMS, custom fields are bound to the 'content model' rather than 'categories'.This means that all articles belonging to the same content model (for example, "article model") will share the custom fields defined under that model.If you need to use a completely different set of custom fields for articles in different categories, you can consider creating different "content models" and associating different categories with different content models.For example, create a 'News Model' and a 'Tutorial Model', which can have their own independent custom fields.
3. My custom field content is HTML code, but it is escaped when displayed. How can I solve this?If your custom field stores HTML code (for example, through a rich text editor), AnQiCMS will default to escaping it when outputting directly in the template for safety. In order for these HTML codes to be parsed and displayed normally by the browser, you need to use|safefilter. For example:{{ archiveContent|safe }}Please note, using|safeMeans you trust that the content is safe and will not bring security risks such as XSS.