How to judge whether a custom parameter exists and display in AnQiCMS via the `archiveParams` tag?

Calendar 👁️ 62

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:

  1. {% archiveParams extraFields %}Store all the custom parameters of the current document inextraFieldsthe variable.
  2. {% for field in extraFields %}Loop throughextraFieldsEach custom parameter in the array.
  3. {% 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.)
  4. 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)

  1. 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.

  2. **Q:archiveParamsand

Related articles

The `archiveParams` tag returns custom parameters, is the data structure a fixed array or a variable Map?

Hello, all website operators and AnQi CMS enthusiasts! Today, we will delve into a practical and flexible tag in the Anqi CMS template development——`archiveParams`.Many times, when using custom parameters, there is a question: What is the data structure of the custom parameters returned by the `archiveParams` tag, whether it is a fixed array or a variable Map?In order to better utilize the strong content model customization capabilities of Anqi CMS, it is crucial to understand this point.This article will explain in detail

2025-11-06

How to implement cross-site calls for the `siteId` parameter of the `archiveParams` tag in the AnQiCMS multi-site environment?

## Mastering AnQi CMS Multi-Site: Cross-Site Call Practice of `archiveParams` Tag SiteId Parameter As an experienced website operations expert, I fully understand that efficient content management and flexible content scheduling are the keys to business success in increasingly complex network environments.AnQiCMS (AnQiCMS) provides powerful tools for operators with its excellent multi-site management capabilities.Today, let's delve into a very practical feature in a multi-site environment

2025-11-06

How to call the `archiveParams` tag individually in AnQiCMS template to get custom document parameters with a specific name?

In the rich feature treasure trove of AnQiCMS, the content model and custom document parameters are undoubtedly the foundation of its flexibility and powerful extensibility.As an expert in website operations, I am well aware of how to efficiently and accurately call these custom parameters in templates, which is crucial for achieving personalized content display and optimizing operational efficiency.Today, let's delve deeply into a powerful and often overlooked technique in the AnQiCMS template: how to call the `archiveParams` tag individually to obtain custom document parameters with a specific name.Introduction

2025-11-06

How to use the `archiveParams` tag to dynamically display the product specification parameter list on the product detail page?

In the era where content is king, an efficient and flexible content management system is crucial for a company's online operations.AnQiCMS (AnQiCMS) is an advanced enterprise-level content management system developed based on the Go programming language, with its powerful content model and template tag system, providing us with infinite possibilities for refined content operation.Today, let's delve into a very practical scenario: how to cleverly use the AnQiCMS `archiveParams` tag to dynamically display a rich variety of product specification parameter lists on the product detail page

2025-11-06

How to safely render `archiveParams` tag if the document parameters contain HTML content?

AnQiCMS (AnQiCMS) is a powerful content management system developed based on the Go language, with flexible content models and customizable document parameter functions, providing great freedom for enterprises and content operators.This means we can create unique fields for articles, products, and other content to meet personalized display needs.

2025-11-06

How to filter the items to display in the `archiveParams` loop based on the custom parameter name?

As an experienced website operations expert, I know that the flexibility of the Content Management System (CMS) is the key to the website's ability to quickly respond to market changes and optimize content structure.AnQiCMS (AnQiCMS) with its powerful custom content model feature, provides us with great convenience.However, defining custom parameters is not enough. How to flexibly call and filter these parameters according to actual needs is a practical problem faced by many operators and developers.

2025-11-06

What is the main purpose of the Tag label function in Anqi CMS?

As an experienced website operations expert, I know that in the increasingly fierce online competition, content must not only be of high quality but also easy to find and organize.AnQiCMS (AnQiCMS) provides us with a comprehensive and efficient content management solution with its powerful functions.Today, let's delve deeply into one of the seemingly simple yet potentially powerful features - Tag label, and see what role it plays in our content operation strategy.

2025-11-06

How to add Tag tags for articles or products in the Anqi CMS backend?

As an experienced website operations expert, I know that the core value of a content management system (CMS) lies in its ability to efficiently and flexibly support content operation strategies.AnQiCMS (AnQiCMS) offers an excellent solution in content publishing and management with its powerful feature set.Today, let's delve deeply into a seemingly simple but crucial feature for content organization and search engine optimization (SEO): how to add Tag tags for articles or products in the AnQiCMS backend.

2025-11-06