In AnQi CMS, when managing website content, we often encounter the need to display specific information in addition to standard fields such as title and content.For example, when operating an e-commerce website, in addition to the name and description of the product, you may also need to display "material", "origin", "warranty period", and so on;Or you are running a news blog, articles may need to display 'author source', 'publishing institution', and so on.Flexible content model design of Anqi CMS is exactly to meet the demand of diverse content display.
Today, let's delve into how to obtain and elegantly display custom parameters in the Anqi CMS template for content models such as articles, products, etc., making your website content richer and more structured.
Why do we need custom parameters for the content model?
AnQi CMS provides various preset content types such as 'Article Model' and 'Product Model', but the detailed needs of content for each industry and each website are very different. Custom parameters allow you:
- Enrich the dimensions of content:Break through the limitations of traditional content, add any additional information you consider important to articles and products.
- Improve user experience:Present information in a structured manner so that visitors can easily find the key data they are interested in, such as the core selling points of a product or the unique background of an article.
- Optimize content management:Categorize different information, which makes background entry and management clearer and more efficient.
- Enhance SEO effects:Structured data is easier for search engines to understand and crawl, which helps improve the visibility and ranking of the website.
In short, custom parameters are the foundation for personalizing and professionalizing your website content.
How to set custom parameters in the background?
In AnQi CMS, the process of setting custom parameters is very intuitive. You can follow the following steps:
- Enter content model management:Log in to the Anqi CMS backend, find the "Content Management" on the left navigation bar, and then click "Content Model".
- Select or create a model: You can choose to edit an existing 'article model' or 'product model', or create a content model as needed.
- Add custom field:On the model editing page, you will see the "Content Model Custom Field" area. Click "Add Field" to define new parameters for the current model.
- Parameter name:This is the Chinese name displayed on the backend, for your understanding (for example, 'article author').
- Call field:This is the actual English variable name used in the template, please ensure its uniqueness and easy recognition (for example
author) - Field type:Select the data type you need to store (single-line text, number, multi-line text, single choice, multiple choice, drop-down selection).This will directly affect the input form on the backend and the display on the frontend.
- Mandatory, default value: Set according to business requirements.
- Save settings:After adding custom fields, be sure to click Save.
After setting up the content model, when you add or edit articles or products under "Content Management", you will see the custom fields just defined in the "Other Parameters" section of the editing page, where you can enter corresponding data for each piece of content.
Retrieve and display custom parameters in the template
In Anqi CMS templates, we mainly use two ways to obtain and display these custom parameters: directly through field names, and by looping through all parameters. The Anqi CMS template engine is similar to Django syntax, using double curly braces{{变量}}Output, conditions and loops using{% 标签 %}the structure.
1. Directly get specific custom parameters through field names
If you know the name of the custom parameter "call field" and only need to get the value of a single parameter, you can usearchiveDetailThe tag is usually used on the details page of articles or products, and can obtain detailed information about the current document, including custom parameters.
Assuming you have added a custom parameter "article source" to the article model, its "call field" issource. In the article detail page template, you can retrieve and display it like this:
{# 假设当前是文章详情页,archiveDetail会自动获取当前文档的详情 #}
<div>
<span>文章标题:</span>{{ archive.Title }}
</div>
<div>
<span>发布时间:</span>{{ stampToDate(archive.CreatedTime, "2006-01-02") }}
</div>
<div>
{# 直接通过自定义参数的“调用字段”名称来获取 #}
<span>文章来源:</span>{% archiveDetail with name="source" %}
</div>
{# 如果您将获取到的值赋给一个变量,可以这样: #}
{% archiveDetail articleSource with name="source" %}
<div>
<span>文章来源(变量):</span>{{ articleSource }}
</div>
Please note,nameThe value must be the 'trigger field' name that you set in the background. This method is concise and clear, suitable for situations where you know exactly which custom parameters to display.
2. Loop through all custom parameters
Sometimes, you may want to dynamically display all custom parameters, or you are unsure of the specific custom parameters of each content model. At this time,archiveParamsThe label comes into play. It can retrieve all custom parameters of a specified document and allows you to display them through a loop.
{# 获取当前文档的所有自定义参数,并将其赋给变量params #}
{% archiveParams params %}
<div>
<h3>其他参数:</h3>
<ul>
{% for item in params %}
<li>
<span>{{ item.Name }}:</span> {# item.Name 是参数名(例如“文章作者”) #}
<span>{{ item.Value }}</span> {# item.Value 是参数的值 #}
</li>
{% endfor %}
</ul>
</div>
{% endarchiveParams %}
In the above example,paramsA variable is an array of objects containing all custom parameters, eachitemincludesName(parameter name, that is, the Chinese name set in the background) andValue(Parameter value).
Handle custom parameters of a specific type:
If your custom parameters areMulti-line textorRich text(For example, a detailed "product feature introduction". The field type is multi-line text, and the background allows HTML input. To ensure that the HTML content is parsed correctly and not escaped, you need to use it in the output when)|safeFilter. If this field supports Markdown, it needs to be used first|renderThen use the filter to convert it to HTML|safe:
{# 假设自定义字段"产品特点介绍"的调用字段是"features", 且支持Markdown #}
{% archiveDetail productFeatures with name="features" %}
<div class="product-features">
{{ productFeatures|render|safe }}
</div>
If your custom parameter is aimage group(For example, "product actual photos", multiple image addresses input in the background),`