In AnQiCMS (AnQiCMS), the custom parameter field provides great flexibility and scalability for website content.As a website operator familiar with Anqi CMS, I know how to effectively use these fields and display them on the website front end to meet diverse content display needs.This article will detail how to obtain and display the custom parameter field value of the article through AnQi CMS template tags.

Understanding the custom parameter field of AnQiCMS

AnQi CMS allows users to customize content models according to business needs.This means that in addition to standard fields such as article title, content, and summary, you can add unique custom information for specific content types (such as products, services, cases, etc.)These custom fields can be defined in the backend content model settings, including single-line text, numbers, multi-line text, radio buttons, checkboxes, and dropdown selections, among other types.Once defined and added to the content model in an article or product, these custom parameters become part of the content data and can be called and displayed in the front-end template.

Define custom parameter field

On the AnQi CMS backend, the process of defining custom fields begins with the 'Content Model' management.You can edit existing models (such as article models, product models) or create new content models.In the model editing interface, there is an "content model custom field" area.Here, you can add new fields and specify the "parameter name" (the Chinese name displayed to the editor in the background, such as "article author"), and the "field name" (the actual English field name used in the template, such asauthor), as well as field type, whether it is required, and default value. This "call field" is the key identifier we use to obtain the value of the custom parameter in the template.

UsearchiveParamsLabel to get custom parameters

In the AnQi CMS template,archiveParamsThis label is specifically designed to retrieve custom parameters of articles. This tag allows you to get all custom parameters of the current article or a specified article.

archiveParamsThe basic syntax of tags is:{% archiveParams 变量名称 with id="文档ID" sorted=true %}.

Among them,变量名称Is the name you define for the parameter set you obtained, for exampleparams.idThe parameter is used to specify which custom parameter to obtain for the document; if omittedidIt will default to getting the parameters of the document currently displayed on the page.sortedThe parameters are very critical as they determine the structure of the returned data.

Whensortedis set totrue(This is also the default value),archiveParamsIt will return an ordered array of objects. Each element of the array represents a custom field, includingName(parameter name, that is, the Chinese name set in the background) andValue(Parameter value). This method is suitable for scenarios where you want to iterate and display all custom parameters.

The following is an example code for iterating and displaying all custom parameters.

{# 假设这是在文档详情页,或者通过id="指定文档ID"获取 #}
<div>
    {% archiveParams params with sorted=true %}
    {% for item in params %}
    <div>
        <span>{{item.Name}}:</span>
        <span>{{item.Value}}</span>
    </div>
    {% endfor %}
    {% endarchiveParams %}
</div>

This code block will traverseparamsthe array and display the parameter name and corresponding value for each custom parameter.

Directly retrieve a specific custom parameter

In addition to iterating over all custom parameters, Anqi CMS also provides two more direct ways to retrieve the values of specific custom parameters.

The first method is to usearchiveParamsthe tag and assignsortedthe parameter tofalseIn this mode,archiveParamsIt will return an unordered map object, you can directly access it through the custom field "调用字段" (FieldName)NameandValue.

For example, if you define a field namedauthorThe custom field (called field), you can get its value like this:

{# 获取指定文档ID的参数,并以map形式返回 #}
<div>
    {% archiveParams params with id="1" sorted=false %}
        {# 假设存在名为 "author" 和 "version" 的自定义字段 #}
        <div>作者:{{params.author.Value}}</div>
        <div>版本:{{params.version.Value}}</div>
    {% endarchiveParams %}
</div>

This approach is suitable when you know exactly which custom fields you want to retrieve and want to access their values directly.

The second approach is more concise and is done directly byarchiveDetailTag to get the value of a specific custom field. If your custom field is defined in the background with a clear "call field", you can use that call field asarchiveDetailin the tag as the call field.nameThe value of the parameter, to directly obtain its content.

For example, if you have a field calledauthorcustom parameter, you can display it like this:

{# 直接通过archiveDetail标签获取名为"author"的自定义字段值 #}
<div>文章作者:{% archiveDetail with name="author" %}</div>

This method is very suitable for directly inserting a small amount of specific custom parameters for display on the article detail page, the code is more concise and clear.

Application scenarios and **practice

In website operations, custom parameter fields can greatly enrich your content display.For a product page, you can customize fields such as "specifications", "model", "origin", "warranty period", and so on.For the service page, you can add 'service process', 'service cycle', etc.

When displaying these parameters in the template, it is recommended that you:

  • Back-end Naming ConventionsWhen defining custom fields in the back-end, make sure to use meaningful English field names for the 'invoked fields' to facilitate the understanding and use by front-end template developers.
  • Front-end Style AdaptationPlease note that regardless of the method used to obtain the parameter value, it is important to beautify it with CSS styles to keep it consistent with the overall design style of the website and enhance the user experience.
  • Conditional judgmentIn some cases, custom parameters may not be filled in for all articles. When displayed in the template, it is appropriate to use{% if %}tags for judgment to avoid displaying empty values.

By using the above method, you can flexibly and efficiently display custom parameter fields of articles on the Anqi CMS website, thereby creating a content-rich and powerful website.


Frequently Asked Questions (FAQ)

Q1: I defined a custom parameter field, but it does not display on the article detail page, why is that?A1: Please check the following aspects:

  1. Is the field defined correctly in the background content model?Ensure that the "reference field" is in English and free of spelling errors.
  2. Did you fill in the value of this custom field while editing the article?Only fields with values filled in will be displayed on the front end.
  3. Are the correct template tags and field names used in the template?For example, if you usearchiveParamsEnsureitem.Nameanditem.Valuethe call is correct; if you usearchiveDetailto directly retrieve, make surename="您的调用字段"is correct.
  4. Have the caches been cleared?Try to clear the system cache in the 'Update Cache' function in the background.

Q2:archiveParamsin the labelsorted=trueandsorted=falseWhat are the differences, how should I choose?A2: Whensorted=truethen,archiveParamsReturn an ordered array, suitable for when you want to iterate over all custom parameters and display them one by one, or when you are unsure about the custom parameters.For example, display the complete specification list of a product. Whensorted=falseIt returns an unordered map, you can directly access the value of a specific parameter through the field name (in English).This approach is suitable for the scenario where you already know which specific custom parameters to display and want to directly obtain their values without the need to traverse, the code will be more concise.

Q3: Can the value of a custom field be an image or complex HTML content? If so, how can it be displayed correctly in the template?A3: The custom field types of AnQi CMS include single-line text and multi-line text, the multi-line text editor supports rich text editing, so it can include HTML content and even images. When you get the custom field values containing HTML or image addresses through template tags, you may need to use|safeA filter to ensure that content is rendered in its original HTML form rather than escaped as plain text. For example:<div>产品特性:{{item.Value|safe}}</div>. For image URLs, you can use them directly as<img>label'ssrcProperty value.