In Anqi CMS, flexibly displaying customized article parameters is the key to enhancing the personalization and richness of website content.In order to better describe product features, or to clearly mark the author and source in the article, custom parameters can provide strong support.The Anqi CMS provides a concise and efficient way to define and call these parameters, allowing you to easily display them in website templates.
Next, we will discuss step by step how to implement the loop display of custom parameters in the Anqi CMS template.
Step 1: Understand and create custom parameters
In 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, the article model can have parameters such as 'author' and 'source', while the product model can have parameters such as 'size' and 'color'.
To create these custom parameters, you need to set them in the Anqi CMS backend:
- Log in to the backend and navigate toContent Management>Content model.
- Select the content model you want to add custom parameters to (such as "article model"), click the "Edit" button.
- On the content model editing page, find the 'Content Model Custom Field' section.
- Here, you can add new fields. Each field needs to set a 'parameter name' (for display in the background), a '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 selection, 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 namedsourceThe parameter named "Article Source", a custom field of type "Single Line Text".
Step 2: Display the custom parameter on the article detail page.
After you have created and filled in the custom parameters for the article in the background, the next step is to call them in the front-end template. On the article detail page({模型table}/detail.htmlor{模型table}/detail-{文档ID}.html), AnQi CMS providesarchiveDetailTag to get the detailed information of the current article, which also includes custom parameters.
If you know the specific name of the custom parameter (for example,authororsource),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 includes the current article object (usually on the article detail page and namedarchivevariable), you can also access it directly using dot notation:
<div>作者:{{archive.author}}</div>
<div>来源:{{archive.source}}</div>
This method is suitable when you know exactly which custom parameters you need to display.
Step 3: Loop through all the custom parameters of the article.
In some cases, you may want to dynamically display all the custom parameters of the article, or you are not sure what parameters there will be, and just want to list all the non-empty parameters that have been set. At this time,archiveParamsThe label comes into play. This label is specifically used to obtain all custom parameter lists of a specified article and supports looping.
archiveParamsThe usage method 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 code above:
{% archiveParams params %}Assigns the collection of all custom parameters of the current article toparamsVariable.{% for item in params %}Then it will traverse thisparamscollection, each time loop,itemThe variable represents a custom parameter.{{item.Name}}The value is the parameter name set in the background (for example, "author", "source of article").{{item.Value}}The value is the specific value of the parameter (for example, "Zhang San", "Xinhua News Agency").- If the value of a custom parameter may contain HTML content (such as using a multi-line text field), to ensure that HTML is parsed correctly and not displayed as plain text, you can
{{item.Value}}added afterwards|safea filter such as{{item.Value|safe}}.
If you want to get the custom parameters of 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
On the article list page (for example, category list page, search results page, etc.), you will usually usearchiveListLabel to loop through multiple articles. If you want to display custom parameters in each article item, you can putarchiveParamsNested in a tagarchiveListinside 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 toarchiveParamsso that the custom parameters of the article can be retrieved and displayed.
Summary
The Anqi CMS custom parameter function combined with powerful template tags provides you with great flexibility, allowing you to accurately display additional information of articles at any location on the website according to actual needs. By making reasonable use ofarchiveDetailandarchiveParamsTags, whether it is a fixed parameter or a dynamic parameter list, can easily handle, making your website content more attractive and professional.
Frequently Asked Questions (FAQ)
1. I have set custom parameters in the background, but why can't I access them through the front-end template?{{archive.customFieldName}}This usually happens when?This usually happenscustomFieldName(The spelling of the 'call field' you set in the background is incorrect, or the current template context contains)archiveThe variable does not contain the custom parameter data. Please check the spelling of the called field and make sure you are on the article detail page orarchiveListinside a loop andarchiveThe variable indeed points to the correct article object. UsearchiveParamsTagging for iteration is a more robust method, as it explicitly returns all custom parameters that have been set.
How to display a specific custom parameter without looping through all parameters?If you want to display a known specific custom parameter, you can use it directly.{% 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 do I display these HTML contents correctly 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 parse these HTML correctly rather than displaying them as plain text, you need to use|safea filter. For example:<span>{{item.Value|safe}}</span>Please note that using|safeThe filter means you trust the content of the parameter to be safe, and it will not introduce malicious scripts.