In AnQi CMS, flexibly displaying custom parameters of articles is the key to enhancing the personalization and information richness of website content.Whether it is to better describe product features or to clearly mark the author and source in the article, custom parameters can provide strong support.Auto CMS provides a simple and efficient way to define and call these parameters, allowing you to easily loop through them in web templates.
Next, we will discuss step by step how to implement the loop display of custom parameters in the article of the security CMS template.
Step 1: Understand and create custom parameters
In the AnQi CMS, custom parameters are defined based on the 'content model'.This means you can set exclusive parameters for different types of content (such as articles, products, etc.).For example, an article model may have parameters such as "author", "source", while a product model may have parameters such as "size", "color".
To create these custom parameters, you need to set them up in the security CMS backend:
- Log in to the backend, navigate toContent management>Content Model.
- Select the content model you want to add custom parameters to (such as "Article Model
- On the content model editing page, find the 'Content Model Custom Fields' section.
- Here, you can add new fields.Each field needs to set the 'parameter name' (used for backend display), 'calling field' (this is the unique identifier you use in the template, recommended to use lowercase English letters), 'field type' (such as single-line text, multi-line text, single choice, etc.), whether it is required, and the default value.
For example, if you want to add a "source" parameter to an article, you can create a call field namedsource,Parameter name is "Article Source", field type is "Single Line Text" custom field.
Step 2: Display custom parameters on the article detail page.
When you have already created and filled in custom parameters for the article in the background, the next step is to call them in the front-end template. In the article detail page({模型table}/detail.htmlor{模型table}/detail-{文档ID}.html),Safe CMS providesarchiveDetailLabel to obtain the detailed information of the current article, which also includes custom parameters.
If you know the specific name of the custom parameter (for exampleauthororsourceIt can be called directly in the following way:
{# 假设您在后台创建了调用字段为 author 和 source 的自定义参数 #}
<div>作者:{% archiveDetail with name="author" %}</div>
<div>来源:{% archiveDetail with name="source" %}</div>
Or, if your template context already contains the current article object (usually on the article detail page, there is a name calledarchiveThe variable),can also be accessed directly using the dot syntax:
<div>作者:{{archive.author}}</div>
<div>来源:{{archive.source}}</div>
This method is suitable when you know exactly which custom parameters you need to display.
Third step: Loop through all custom parameters of the article
In some cases, you may want to dynamically display all custom parameters of the article, or you are not sure which parameters there will be, and just want to list all non-empty parameters that have been set. At this time,archiveParamsLabels come into play. This label is specifically used to obtain all custom parameter lists of a specified article and supports iteration.
archiveParamsThe usage of the label is as follows:
{% archiveParams params %}
{% for item in params %}
<div>
<span>{{item.Name}}:</span>
<span>{{item.Value}}</span>
</div>
{% endfor %}
{% endarchiveParams %}
In the above code:
{% archiveParams params %}Will assign the set of all custom parameters of the current article.paramsa variable.{% for item in params %}It will iterate over this.paramsSet, every time a loop,itemThe variable represents a custom parameter.{{item.Name}}The parameter name (e.g., "author{{item.Value}}The specific value of the parameter (e.g., "Zhang San- If the value of the custom parameter may contain HTML content (for example, using a multi-line text field), to ensure that HTML is parsed correctly and not displayed as plain text, you can
{{item.Value}}after|safeFilter, such as{{item.Value|safe}}.
If you want to get custom parameters for a specified article ID instead of the parameters of the article on the current page, you can use it like thisarchiveParams:
{% archiveParams params with id="123" %} {# 这里的123替换为具体的文章ID #}
{% for item in params %}
<div>
<span>{{item.Name}}:</span>
<span>{{item.Value}}</span>
</div>
{% endfor %}
{% endarchiveParams %}
Step 4: Display custom parameters on the article list page
In the article list page (such as category list page, search results page, etc.), you will usually usearchiveListLabel to display multiple articles. If you want to display custom parameters in each article list item, you canarchiveParamsthe template)archiveListin the loop.
{% archiveList articles with type="page" limit="10" %}
{% for article_item in articles %}
<div class="article-item">
<h3><a href="{{article_item.Link}}">{{article_item.Title}}</a></h3>
<p>发布时间:{{stampToDate(article_item.CreatedTime, "2006-01-02")}}</p>
{# 在这里调用当前文章的自定义参数 #}
{% archiveParams custom_fields with id=article_item.Id %}
{% for field in custom_fields %}
<div>
<span>{{field.Name}}:</span>
<span>{{field.Value}}</span>
</div>
{% endfor %}
{% endarchiveParams %}
<p>{{article_item.Description}}</p>
</div>
{% endfor %}
{% endarchiveList %}
In this example,archiveListIn the looparticle_itemrepresent each article, we useid=article_item.IdPass the current article ID toarchiveParamsto retrieve and display the custom parameters of the article.
Summary
The custom parameter feature of Anqi CMS combined with powerful template tags provides you with great flexibility. It can accurately display additional information of articles at any position on the website according to your actual needs.archiveDetailandarchiveParamsLabels, whether fixed parameters or dynamic parameter lists, can be easily handled, making your website content more attractive and professional.
Common Questions and Answers (FAQ)
1. I have set custom parameters in the background, but why can't I access it through the front-end template?{{archive.customFieldName}}Access is not possible?This usually happens incustomFieldName(i.e. the "Field Invoked" set in the background) is spelled incorrectly, or the current template context isarchiveThe variable does not contain the custom parameter data. Please check the spelling of the called field and ensure you are on the article detail page.archiveListinside the loop, andarchiveThe variable indeed points to the correct article object. UsingarchiveParamsLooping through tags is a more robust method, as it explicitly returns all custom parameters that have been set.
2. How to display a specific custom parameter without looping through all parameters?If you only want to display a known specific custom parameter, you can directly use{% archiveDetail with name="您的调用字段名" %}Label. For example, to display “Author”, you can use{% archiveDetail with name="author" %}. This way avoids unnecessary loops, making the code more concise and clear.
3. My custom parameter is a multi-line text field that contains HTML code, how can I correctly display these HTML contents in the template?If the value of your custom parameter may contain HTML code (for example, a brief introduction with bold text and links), in order for the browser to correctly parse these HTML and not display them as plain text, you need to use|safefilter. For example:<span>{{item.Value|safe}}</span>Please note, using|safeThe filter means that you trust the content of this parameter to be safe and not to introduce malicious scripts.