In AnQiCMS, managing and displaying content, 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 to add detailed technical parameters for product detail pages or to supplement author profiles and external reference links for articles, custom fields can provide strong support.Understand how to call and display these custom fields on the article detail page, which 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 for the role of custom fields. By customizing the content model, you can define various content types according to business needs and add exclusive fields for each type to meet the personalized display needs of content.
Define and manage custom fields: the foundation of the content model
In the AnQiCMS backend, the management of custom fields is concentrated in the 'Content Model' under the 'Content Management' module.The system provides default "article model" and "product model", you can also create your own content model.Enter any content model and you will find the "Content Model Custom Field" area, 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 'calling field' (the actual name referenced in the template). The field types are diverse, including:
- Single-line text: Suitable for brief text descriptions, such as author names, product models.
- Number: Ensure the input content is purely numeric, such as prices, inventory.
- Multi-line text: Suitable for longer text content, such as product features descriptions, author biographies.
- Single choice/Multiple selections/Drop-down selection: Provides preset options for selection, suitable for standardized data such as product colors, sizes.
Correctly setting the field type and calling the field name is the premise for accurately calling data in the template later.
Call the custom field content in the article detail page.
Once the custom field is defined and content is filled in 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 name of the custom field's "call field" and the field is a single value (such as single-line text, number, or multi-line text), you can use it directlyarchiveDetailLabel to get its value. For example, if you create a namedauthorsingle 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>
In this way, 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 is very useful. It retrieves all the custom fields of the current article and provides 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 block,paramsis an array that contains all custom fields, eachitemhasName(parameter name, that is, the Chinese name displayed in the background) andValue(Field value) attribute. This method is especially suitable for product parameter lists or dynamic display of additional information.
3. Handle custom fields of specific types.
Some custom field types require special handling when displayed, such as album or multiple choice fields. Assume you have defined an album field namedarcimagesused for uploading 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 URL, you canforloop through and display each image.
If the custom field stores rich text content, which may include HTML tags, to avoid these tags being escaped as plain text, you need to use|safeFilter:
{% archiveDetail articleContent with name="自定义富文本字段名" %}
<div>{{ articleContent|safe }}</div>
If a 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>
Little tricks in practice
- Debugging tool: During development, if you are unsure about the structure or value of custom fields, you can use
|dumpa filter to print variable details, for example{{ params|dump }}This will display the variable type and content, helping you quickly locate the problem. - Conditional judgmentIn displaying custom fields, it is best to include conditional judgments to prevent unnecessary blank spaces or errors from appearing on the page when fields are empty. For example
{% if item.Value %}or{% if arcimages %}. - Naming convention: Choose a clear and meaningful "call field" name for custom fields, which will make the template code easier to read and maintain.
- Filter applicationUse flexible applications of the various filters provided by AnQiCMS (such as
|thumbUsed to obtain thumbnails,|truncatecharsUsed to extract text and so on), it can better beautify and process the display effect of custom fields.
By using the above method, you can fully utilize the custom field function of AnQiCMS to build a powerful, content-rich, and highly personalized article detail page of the website, meeting various complex display requirements.
Frequently Asked Questions (FAQ)
1. Why did I add a custom field but nothing is displayed when called in the template?This usually has several reasons:
- Field name is incorrect: Please check the template in
name="自定义字段名称"The part is whether it is consistent with the 'Call Field' name set in the background 'Content Model', including case sensitivity. - The content of the article has not been saved.Confirm whether you have filled in the content for this custom field and successfully saved it while editing the article.
- The field type does not match.For example, if you try to use
{% archiveDetail with name="字段名" %}Directly fetching a single value may result in not getting the expected result. For multi-select or group image fields, it is usually necessary to iterate through their content.
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 (such as "article model") will share the custom fields defined under the model.If you need to use completely different custom field sets for articles of 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.
My custom field content is HTML code, but it gets escaped when displayed. How to solve this?If your custom field stores HTML code (for example, through a rich text editor), when outputting directly in the template, for safety, AnQiCMS will default to escaping it. In order for these HTML codes to be parsed and displayed correctly by the browser, you need to use|safea filter. For example:{{ archiveContent|safe }}Please note that using|safemeans you trust that the content is safe and will not pose XSS or other security risks.