In AnQiCMS, custom parameter fields provide great flexibility and scalability for website content.As a website operator familiar with the AnQi CMS, I am well aware of how to effectively utilize 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 values of the article through the template tags of the security CMS.
Understanding the custom parameter field of AnQiCMS
The 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 abstract, you can add unique custom information for specific content types (such as products, services, cases, etc.).These custom fields can be defined in the content model settings on the backend, including various types such as single-line text, numbers, multi-line text, radio buttons, checkboxes, and dropdown selections.Once defined and added to the content model within 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
In the Anqi CMS backend, the process of defining custom fields starts with the 'Content Model' management.You can edit an existing model (such as an article model, product model) or create a new content model.In the model editing interface, there is an area called "Content Model Custom Field".author), and field type, whether it is required and default value. This "call field" is the key identifier we use to get the value of this custom parameter in the template.
UsearchiveParamsLabel acquisition of custom parameters
In the templates of AnQi CMS,archiveParamsThis tag is specifically designed to retrieve custom parameters of articles. This tag allows you to retrieve all custom parameters of the current article or a specified article.
archiveParamsThe basic syntax of the tag is{% archiveParams 变量名称 with id="文档ID" sorted=true %}.
Among them,变量名称The name you define for the parameter set you have obtained, for exampleparams.idThe parameter is used to specify which document's custom parameter to obtain; 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 returned data structure.
Whensortedsettrueat the moment (which is also the default value),archiveParamsIt will return an ordered array of objects. Each element in the array represents a custom field, containingName(parameter name, that is, the Chinese name set in the background) andValue(Parameter value). This method is suitable for the scenario where you want to traverse and display all custom parameters.
The following is an example code that traverses and displays 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 segment will traverseparamsthe array and display the parameter name and corresponding value for each custom parameter.
Directly obtain a specific custom parameter
In addition to iterating over all custom parameters, the Anqi CMS also provides two more direct methods to obtain the values of specific custom parameters.
The first method is to usearchiveParamstags andsortedparameter settingsfalseIn this mode,archiveParamsa unordered map object will be returned, you can directly access itsNameandValue.
For example, if you define a single-line text custom field namedauthorThe custom field (call 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 you want to access their values directly.
A more concise way is to directly go througharchiveDetailLabel to get the value of a specific custom field. If your custom field has a clear "call field" when defined in the background, you can use that call field asarchiveDetailthe call field as in the tagnameThe value of the parameter, to directly obtain its content.
For example, if you have a field callauthora custom parameter, you can display it directly 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 in the article detail page, with more concise and clear code.
Actual application scenarios and **practice
In actual website operation, custom parameter fields can greatly enrich your content display.For product pages, you can customize fields such as 'specifications', 'model', 'origin', 'warranty period', and so on.For the service page, you can add "service process
When displaying these parameters in the template, it is recommended that you:
- Back-end Naming ConventionsWhen defining custom fields in the back-end, ensure that the 'Called Field' uses a meaningful English name, which is convenient for front-end template developers to understand and use.
- Front-end Style Adaptation:No matter which way you obtain the parameter value, please pay attention to beautify it with CSS styles to keep it consistent with the overall design style of the website, thus enhancing the user experience.
- Conditional judgmentIn some cases, custom parameters may not be filled out for all articles. When displayed in the template, appropriate use can be made.
{% if %}Use tags for judgment to avoid displaying empty values.
Through 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.
Common Questions and Answers (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:
- Is the field correctly defined in the background content model?Make sure the 'field called' is in English and free of spelling errors.
- Did you fill in the value of this custom field when editing the article?Only fields with values filled in will be displayed on the front end.
- Are the correct template tags and field names used in the template?For example, if you use
archiveParams,ensureitem.Nameanditem.Valuethe call is correct; if usingarchiveDetailDirectly obtain, ensurename="您的调用字段"Correctness - Have the caches been cleared?In the background "Update Cache" feature, try to clear the system cache.
Q2:archiveParamsthe tag insorted=trueandsorted=falseWhat is the difference, and how should I choose?A2: Englishsorted=truewhenarchiveParamsReturns an ordered array, suitable for iterating over all custom parameters and displaying them one by one, or when unsure about the available custom parameters.For example, display the full specification list of a product.sorted=falseWhen it is executed, it returns an unordered map. You can directly access the value of a specific parameter by the 'field name' (in English).This approach is suitable for the scenario where you already know which specific custom parameters you want to display, and you 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 even images. When you get these custom field values containing HTML or image addresses through template tags, you may need to use|safeFilter to ensure 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 directly use them as<img>Tagssrcuse it as the attribute value.