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 respond quickly to market changes and optimize the content structure.Auto CMS (AutoCMS) provides us with great convenience with its powerful custom content model function.However, simply 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.archiveParamsIn the loop, accurately filter out the content we want to display based on the custom parameter name.

Understand the custom parameter mechanism of AnQiCMS

Before delving into the filtering strategy, we must first clarify how custom parameters work in the security CMS.One of the core strengths of Anqi CMS is its highly flexible content model.Whether it is an article, product, or any other type of content, we can add various dedicated fields according to business needs, which are what we commonly refer to as 'custom parameters'.

When we fill in these custom fields on the editing page of the document (or product content, etc.), this data will be stored with the document. On the front-end template, we can accessarchiveParamsLabel to retrieve this additional information.

archiveParamsThe core function of the tag

archiveParamsTags are powerful tools in AnQi CMS templates used to retrieve all custom parameters of the current document or a specified document. Usually, we use it like this:

{% archiveParams params %}
    {% for item in params %}
        {# 这里的 item 包含了自定义参数的 Name(参数名)和 Value(参数值) #}
        <p>{{ item.Name }}:{{ item.Value }}</p>
    {% endfor %}
{% endarchiveParams %}

In this code,archiveParamsThe tag assigns all custom document parameters as an arrayparamsThen, we loop through each parameter.forLoop through thisparamsto it. Eachitemrepresents a custom parameter. By default,archiveParamsit will besorted=trueThe way to return an ordered array, eachitemcontains two key attributes:Name(that is the 'parameter name' you set in the background)Value(that is the specific content of the parameter).

In the loop, filter parameters according to the name

Resolve inarchiveParamsThe method of filtering specific parameters in the loop is combined with the AnQiCMS template engineforloop andifConditional judgment tags. SincearchiveParamsEach in the loopitemAll providedNameProperties, we can directly use this property for comparison, thereby filtering out the parameters we want to display.

Imagine a scenario: you have a "bookNow, you want to display only 'Author' and 'Publisher' in the article summary card.

<div class="book-info-card">
    <h3>书籍信息概览</h3>
    {% archiveParams bookDetails with sorted=true %}
        {% for detail in bookDetails %}
            {# 根据参数名(item.Name)进行条件判断 #}
            {% if detail.Name == "作者" or detail.Name == "出版社" %}
                <p><strong>{{ detail.Name }}:</strong> {{ detail.Value }}</p>
            {% endif %}
        {% endfor %}
    {% endarchiveParams %}
</div>

In this code block:

  1. We use{% archiveParams bookDetails with sorted=true %}Get all custom parameters of the current document and store them inbookDetailsthe variable.
  2. {% for detail in bookDetails %}Loop through each custom parameter.
  3. The core is in{% if detail.Name == "作者" or detail.Name == "出版社" %}this line. It will check the current parameter'sNameThe property is equal to 'author' or 'publisher'.
  4. Only when the condition is met,ifthe content inside the tag (<strong>{{ detail.Name }}:</strong> {{ detail.Value }}) will be rendered.

Through this method, even thougharchiveParamsRetrieved all custom parameters of the document, and we can also flexibly display the information we need on the front-end page according to the parameter name. This greatly enhances the refinement and maintainability of content display.