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 a content management system (CMS) is the key to whether the website can respond quickly to market changes and optimize the structure of content.AnQiCMS (AnQiCMS) offers us great convenience with its powerful custom content model function.However, it is not enough to simply define custom parameters, how to flexibly call and filter these parameters according to actual needs is a practical problem faced by many operators and developers.Today, let's delve deep into how toarchiveParamsIn the loop, accurately filter out the content we want to display based on the name of the custom parameters.
Understand the custom parameter mechanism of AnQiCMS.
Before delving into the filtering strategy, we first need to clarify how the custom parameters operate in the Anqí CMS.One of the core advantages of AnQi CMS is its highly flexible content model.Whether it is articles, products, or any other type of content, we can add various dedicated fields according to business needs, which are what we often call 'custom parameters'.
The definition of these custom parameters is usually carried out in the "Content Management" module on the backend, the specific path is "Content Management" -> "Content Model" -> Select or create a model -> "Custom Field".Here, you will set a 'parameter name' for each custom parameter (this is used in the backend interface and frontend template to identify and display it) and a 'calling field' (this is the unique identifier used internally for storage and access through programming interfaces).
When we fill in these custom fields on the editing page of the document (or product and other content), this data will be stored with the document. In the front-end template, we can then usearchiveParamsLabel to get this additional information.
archiveParamsThe core role of tags
archiveParamsThe tag is a powerful tool in Anqi CMS template used to obtain 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 block,archiveParamsThe tag assigns all custom document parameters as an array toparamsthe variable. Then, we useforLoop through thisparamsEach element of theitemrepresents a custom parameter. By default,archiveParamswill be usedsorted=trueThe way to return an ordered array, eachitemcontains two key properties:Name(which is the parameter name you set in the background) andValue(which is the specific content of the parameter).
However, merely traversing and displaying all parameters is not enough in many cases.For example, you may define multiple parameters such as "author", "publisher", "ISBN", "page number", etc. for an article, but in a certain area of the page, you only want to highlight the information of "author" and "publisher", while other parameters are placed in another more detailed area of the page.At this point, we need to filter in the loop.
In the loop, filter parameters based on name
Resolve inarchiveParamsThe method of filtering specific parameters in the loop is combined with the AnQiCMS template engineforloop andifConditional judgment tags. BecausearchiveParamsEach in the loopitemAll providedNameProperties, we can directly use this property to compare and thus filter out the parameters we want to display.
Imagine a scenario: you have a "book" content model, to which you have added custom parameters such as "author", "publisher", "publish date", "ISBN", and so on.Now, you want to display only 'author' and 'publisher' information in the article summary card.You can implement it like this:
<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:
- We use
{% archiveParams bookDetails with sorted=true %}Get all the custom parameters of the current document and store them inbookDetailsthe variable. {% for detail in bookDetails %}Loop through each custom parameter.- The core lies in
{% if detail.Name == "作者" or detail.Name == "出版社" %}This line. It will check the current parameter'sNameIs the attribute equal to "author" or "publisher"? - Only when the condition is met,
ifThe content inside the tag (<strong>{{ detail.Name }}:</strong> {{ detail.Value }}) will be rendered.
In this way, evenarchiveParamsObtained all the custom parameters of the document, we can also flexibly display the information we need on the front-end page according to the parameter name, which greatly enhances the refinement and maintainability of content display.