In the Auto CMS, we often encounter the need to display specific information in addition to standard fields such as title and content.For example, if you are running an e-commerce website, in addition to the name and description of the product, you may also need to display "materialThe flexible content model design of Anqi CMS is precisely to meet this diverse content display demand.

Today, let's delve into how to retrieve and elegantly display the custom parameters in the article, product, and other content models within the Anqi CMS template, making your website content richer and more structured.

Why is it necessary to have custom parameters for content model?

The "article model" and "product model" and other preset content types are provided by AnQi CMS, but the detailed content requirements of each industry and each website are very different. Custom parameters allow you:

  1. Enrich content dimensions:Break through the limitations of traditional content, adding any additional information you consider important to articles and products.
  2. Enhance user experience:Display information in a structured manner so that visitors can quickly find the key data they are interested in, such as the core selling points of products and the unique background of articles.
  3. Optimize content management:Categorize different information, and the background input and management will be clearer and more efficient.
  4. Enhance SEO effect:Structured data is easier for search engines to understand and crawl, which helps to 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 up custom parameters in the background?

In the Auto CMS, the process of setting custom parameters is very intuitive. You can follow the following steps:

  1. Enter the content model management:Log in to the AnQi CMS backend, find the "Content Management
  2. Select or create a model:You can choose to edit an existing "article model" or "product model", or create a content model as needed.
  3. Add custom fields:On the model editing page, you will see the "Content Model Custom Fields" 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").
    • Field call:This is the actual English variable name used in the template, please ensure it is unique and easy to identify (for example)author).
    • Field Type:Select the data type you need to store (single-line text, number, multi-line text, single choice, multiple choice, dropdown).This will directly affect the input form of the backend and the display way of the frontend.
    • Is it required, default value:Set according to business requirements.
  4. Save settings:After adding a custom field, be sure to click Save.

Set the content model, when you add or edit articles, products under "Content Management", you will see the custom fields just defined in the "Other Parameters" section of the editing page, and you can enter the corresponding data for each piece of content.

Get and display custom parameters in the template

In AnQi CMS templates, we mainly obtain and display these custom parameters in two ways: directly accessing through field names, and by iterating over all parameters. The AnQi CMS template engine is similar to Django syntax, using double curly braces{{变量}}Output, conditions and loops usage{% 标签 %}structure.

1. Directly access specific custom parameters by field name

If you know the name of the 'call field' of the custom parameter and only need to get the value of a single parameter, you can usearchiveDetailTags. This tag is usually used on the detail pages of articles or products, and can obtain detailed information about the current document, including custom parameters.

Suppose you have added a custom parameter for 'article source' to the article model, its 'calling field' issource. You can retrieve and display it like this in the article detail page template:

{# 假设当前是文章详情页,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 of the attribute must be the 'Call field' name that you set in the background. This method is concise and clear, suitable for cases where you know exactly which custom parameters to display.

2. English translation of auto is English

Sometimes, you may want to dynamically display all custom parameters, or you are unsure about what specific custom parameters each content model has. At this time,archiveParamsThe label comes in handy. It can retrieve all custom parameters of a specified document and allows you to display them through looping.

{# 获取当前文档的所有自定义参数,并将其赋给变量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,paramsThe variable is an array of objects containing all custom parameters, eachitemcontainsName(parameter name, that is, the Chinese name set in the background) andValue(parameter value).

Handling custom parameters of specific types:

If your custom parameter isMulti-line textorRich text(For example, a detailed "Product Features Introduction", field type is multiline text, and backend allows HTML input), To ensure that the HTML content is parsed correctly rather than escaped, you need to use|safeFilter. If this field supports Markdown, you need to use it first.|renderConvert it to HTML with the filter, then use it.|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", input multiple image addresses in the background),