In the powerful content management system of AnQi CMS, custom parameters (also known as custom fields of content model) are an important manifestation of its flexibility.It allows operators to add personalized data to content models such as articles, products, etc., in addition to standard fields according to specific business needs.But in the front-end template, we often need to judge whether a custom parameter exists or has been assigned a valid value, and then decide whether to display it.archiveParamsLabels have become an indispensable tool for us.

Understanding the value of custom parameters andarchiveParamstags

The design philosophy of AnQiCMS emphasizes highly customization, among which 'flexible content model' is one of the core features.This means you can define unique fields for different types of content (such as news articles, e-commerce products, service cases), for example, add 'material', 'size' for products, and 'author bio', 'original link' for articles, etc.These personalized data are what we call custom parameters.

In the version iteration of AnQiCMS, especially starting from v2.1.1, the template tags have been refactored,archiveThe tag series is introduced, which makes accessing content model data more unified and convenient.archiveParamsThe tag is specifically used to retrieve documents (archiveAn tool for custom parameters. It can provide all custom fields configured for the current document or specified document in a structured form to the front-end template.

archiveParamsEnglish: Custom parameter intelligent sensor

archiveParams

English: Method one: Traverse the sorted array for judgment (default)sorted=true)

When you use{% archiveParams params %}English: Or explicitly specify this waysorted=truewhenarchiveParamsIt 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 whether 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 have added a custom field to the article model, named "article source", with the field name being calledarticleSource. You hope to display this source on the article detail page, but 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 custom parameters of the current document inextraFieldsthe variable.
  2. {% for field in extraFields %}to iterateextraFieldsEach custom parameter in the array.
  3. {% if field.Name == "文章来源" and field.Value %}Is the key judgment logic:
    • field.Name == "文章来源"Ensure we have found the target parameter.
    • field.Valuethen it is used to determine whether the parameter has been assigned actual content. In template language, an empty string, the number 0, a boolean valuefalseornilValues that are empty will be evaluated as 'false', which is very suitable for determining 'whether there is valid content'.
  4. If the condition is met,{{ field.Value }}the parameter content will be displayed.

方法二:直接访问无序映射进行判断(指定 Englishsorted=false)

如果你明确知道自定义参数的“调用字段”名称(即在后台内容模型中定义的字段名,如 EnglisharticleSource),and hope to access more directly, then you cansortedparameter settingsfalse.

{% archiveParams params with sorted=false %}will return a map object, you can access the properties of the parameters by calling field names directly, for exampleparams.articleSource.Value.

Continue the example of the previous "article source", if the called field isarticleSourceYou 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 method is more concise, and you can omit the step of looping through when you exactly know which custom parameter you want to access. Similarly,extraFields.articleSource.Valuewhether the value is non-empty is the core of determining its existence.

Another direct access approach:archiveDetailtags

ExceptarchiveParamsIn addition, if you just want to obtain the custom parameter value of a specific, known call field, you can also consider usingarchiveDetailTag. This tag is usually used to get the built-in fields of the document (such asTitle/Content), but it also supports directly getting the values of custom parameters.

For example, to get 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 get a few known custom parameters.

Summary

Whether it is througharchiveParamsThe traversal array pattern, direct mapping access mode, orarchiveDetailLabel direct access, AnQiCMS provides flexible ways to handle custom parameters. The core lies in using conditional judgment of template language (ifTag), checking parameters ofValueThe attribute contains valid content.This can not only make your website content display more accurately, but also effectively avoid page layout chaos or information errors caused by missing data.Master these skills, and you will be able to use the AnQiCMS building function more freely to create a website with rich features and comprehensive information.


Common Questions (FAQ)

  1. Q: Why can't I display the custom parameter I set in the template, even if it doesn't find the loop?A: First, make sure that your custom field is correctly created and saved in the backend content model, and that the content is indeed filled in for this field when the document is published. Second, check the usage of this field in your template.archiveParamsorarchiveDetailthen,idIs the parameter correctly pointing to the document ID containing the custom parameter (if not specified)id, then it defaults to the document of the current page). Finally, check the tag innamethe value of the parameter orsorted=falseThe field name accessed in the mode is completely consistent with the "invoked field" defined in the background content model (case-sensitive). If it still cannot be displayed, it may be that the value of this parameter is empty,ifThe condition failed.

  2. **Q: archiveParamsand