How to judge whether a custom parameter exists and display in AnQiCMS via the `archiveParams` tag?
In the powerful content management system of Anqi CMS, custom parameters (also known as content model custom fields) are an important embodiment of its flexibility.It allows the operator to add personalized data to content models such as articles, products, etc. according to specific business needs.But in the front-end template, we often need to determine whether a custom parameter exists or has been assigned a valid value before deciding whether to display it. At this point,archiveParamsThe label has become our indispensable tool.
Understanding the value of custom parameters andarchiveParamsTag
The design philosophy of AnQiCMS emphasizes highly customization, among which the 'flexible content model' is one of the core functions.This means you can define unique fields for different types of content (such as news articles, e-commerce products, service cases), for example, adding 'material', 'size' for products, and 'author bio', 'original link' for articles, etc.These personalized data, which is what we call custom parameters.
In AnQiCMS version iterations, especially starting from v2.1.1, the template tags have been reconstructed,archiveThe series of tags was introduced, making access to content model data more unified and convenient.archiveParamsTags are specifically used to retrieve documents(archive)A tool for custom parameters. It can provide the structured custom fields configured for the current document or specified document to the front-end template.
archiveParams: Intelligent Detector for Custom Parameters
archiveParamsTags can return custom parameters in two main forms: an ordered array or an unordered map.Understanding these two forms is crucial for determining the existence of parameters.
Method one: Traverse the sorted array for judgment (default)sorted=true)
When you use{% archiveParams params %}Or specify explicitlysorted=truethen,archiveParamsIt will return an array containing all custom parameters. Each element of the array is an object that includesName(the display name of the parameter) andValue(the value of the parameter) two properties.
In this mode, to determine if a specific custom parameter exists and display its value, we need to traverse this array, find the corresponding parameter name, and check itsValuewhether it is not empty.
Let us illustrate through a scenario: Suppose you added a custom field to the article model, named "Article Source", and the field it calls isarticleSource. You want to display this source on the article detail page, but the premise is that it must have content.
In the template, you can do it like this:
{# 使用 archiveParams 获取所有自定义参数,默认 sorted=true #}
{% archiveParams extraFields %}
{# 遍历 extraFields 数组 #}
{% for field in extraFields %}
{# 判断当前遍历到的字段的名称是否为“文章来源”,并且其值不为空 #}
{% if field.Name == "文章来源" and field.Value %}
<p><strong>文章来源:</strong> {{ field.Value }}</p>
{% endif %}
{% endfor %}
{% endarchiveParams %}
In this code block:
{% archiveParams extraFields %}Store all the custom parameters of the current document inextraFieldsthe variable.{% for field in extraFields %}Loop throughextraFieldsEach custom parameter in the array.{% if field.Name == "文章来源" and field.Value %}Is the key judgment logic:field.Name == "文章来源"Ensure that we have found the target parameter.field.ValueIt is used to determine whether the parameter has been assigned actual content. In template languages, empty strings, numbers 0, boolean valuesfalseornil(Empty values are evaluated as "false", which is very suitable for judgments of whether there is valid content.)
- If the condition is met,
{{ field.Value }}it will display the parameter content.
Method two: Directly access the unordered map for judgment (specified)sorted=false)
If you know the name of the custom parameter "call field" (i.e., the field name defined in the background content model, such asarticleSource), and you want to access it more directly, then you can usesortedthe parameter tofalse.
{% archiveParams params with sorted=false %}It will return a mapping (map) object, you can access the properties of the parameters by calling field names, for exampleparams.articleSource.Value.
Follow the example of the previous 'article source', if the calling field isarticleSource, you can judge and display it like this:
{# 使用 archiveParams 获取所有自定义参数,指定 sorted=false 返回映射 #}
{% archiveParams extraFields with sorted=false %}
{# 直接通过调用字段名访问,并判断其值是否存在 #}
{% if extraFields.articleSource.Value %}
<p><strong>文章来源:</strong> {{ extraFields.articleSource.Value }}</p>
{% endif %}
{% endarchiveParams %}
This way is more concise, when you know exactly which custom parameter to access, you can skip the step of iterating through the loop. Similarly,extraFields.articleSource.Valuewhether the value is non-empty is the core to determine its existence.
Another direct access approach:archiveDetailTag
exceptarchiveParamsIn addition, if you simply want to obtain the custom parameter value of a specific, known call field, you can also consider usingarchiveDetailLabel. This tag is typically used to access the built-in fields of a document (such asTitle/Content), but it also supports directly accessing the values of custom parameters.
For example, to retrieve and judge whether the 'article source' exists, you can write it like this:
{# 使用 archiveDetail 直接获取“文章来源”字段的值 #}
{% archiveDetail sourceValue with name="articleSource" %}
{# 判断获取到的值是否非空 #}
{% if sourceValue %}
<p><strong>文章来源:</strong> {{ sourceValue }}</p>
{% endif %}
{% endarchiveDetail %}
This method is particularly direct and convenient when you only need to retrieve a few known custom parameters.
Summary
Whether it is througharchiveParamsLabel traversal array pattern, direct mapping access pattern, orarchiveDetailDirectly obtain the tag, AnQiCMS provides a flexible way to handle custom parameters. The core lies in using conditional judgment of template language(iftag), to check the parameters'ValueWhether the attribute contains valid content. This not only makes your website content display more accurately, but also effectively avoids page layout chaos or information errors caused by data missing.Master these skills, and you will be able to use AnQiCMS to build a website with rich features and comprehensive information.
Frequently Asked Questions (FAQ)
Q: Why did I set a custom parameter but it still does not display in the template, even the loop cannot be found?A: Firstly, make sure that your custom field has been correctly created and saved in the backend content model, and that the content for this field has been filled in when the document is published. Secondly, check the usage of your template.
archiveParamsorarchiveDetailtags in the template,idWhether the parameter correctly points to the document ID containing the custom parameter (if not specifiedidit defaults to the document of the current page). Finally, check thenamevalue of the parameter or insorted=falseIs the field name accessed in the mode consistent with the 'called field' defined in the background content model (case-sensitive). If it still cannot be displayed, it may be that the value of the parameter is empty, causingifThe condition failed.**Q:
archiveParamsand