In the `archiveParams` tag, what scenarios are `sorted=true` and `sorted=false` applicable to?
As an experienced website operation expert, I am well aware that how to flexibly use template tags to display content in a high-efficiency content management system like AnQiCMS is the key to operational success. Today, let's delve into it in detail.archiveParamsTagged,sorted=trueandsorted=falseThese parameters can play a role in specific scenarios.
The Anqi CMS offers great convenience to content creators and operators with its simple and efficient architecture.Among them, the custom content model function is particularly powerful, allowing us to add various additional fields to content types such as articles, products, etc. to achieve personalized content display. AndarchiveParamsThe tag is the core tool from which we obtain data from these custom fields and flexibly present it in the front-end template.
archiveParams: Unlock personalized data of the document
archiveParamsThe purpose of the label is very clear: it helps us obtain all the custom parameters set for a specific document in the Anqie CMS backend (whether it is an article or a product).These parameters may include product features, author information, publishing platform, estimated reading time, etc., which greatly enrich the dimensions of the content.
The basic usage of this tag is usually{% archiveParams params with id="1" sorted=true %}. Among them,idThe parameter specifies which document's custom parameter to retrieve (if not specified, it defaults to the document on the current page). AndsortedParameters, which are the focus of our discussion today, determine how these custom parameters are extracted and used in what structure.
sorted=trueWhen the order of fields is crucial for general display:
Whensortedthe parameter totrueat this time (which is alsoarchiveParamsThe default behavior will return aThe array objects are arranged in order defined by the backgroundEach element of this array is a container that includesName(field name) andValue(field value) object.
Application scenarios:
Imagine that you are designing a product detail page template, and you need to display a "Product Specifications" or "Additional Information" block at the bottom of the page. In this block, you want to list all custom parameters in order according to the fields defined in the backend, for example:
- Processor: Intel Core i7
- Memory: 16GB DDR4
- Storage: 512GB SSD
- Color: Dark Grey
In this case, the order of the fields is usually important, and you may want this block to be able to adapt to new parameters added in the future without modifying the template code.
sorted=trueAdvantages:
- Strong in versatility:The template does not need to know the specific field names, it only needs to iterate through the array to display all parameters. This is very convenient for creating universal components such as product parameter lists and article metadata lists.
- Low maintenance cost:The backend can add, delete, and modify custom fields, and the front-end template usually does not require any changes because it is dynamically traversed.
- Maintain order:Sort strictly according to the custom field sorting on the backend, ensuring the logical and consistent presentation of content.
Code example:
{# 假设我们正在一个文档详情页,需要显示所有自定义参数 #}
<h3>产品规格:</h3>
<ul>
{% archiveParams productSpecs with sorted=true %}
{% for item in productSpecs %}
<li>
<strong>{{ item.Name }}:</strong>
<span>{{ item.Value }}</span>
</li>
{% endfor %}
{% endarchiveParams %}
</ul>
This code will dynamically traverse and display all custom parameters, which is very suitable for building universal and expandable specifications or additional information modules.
sorted=false: Accurately locate specific data, flexible layout
Whensortedthe parameter tofalsethen,archiveParamsWill return oneAn unordered Map (or object). In this Map, each custom field's 'display field name' (which is the English identifier set during back-end definition, such asauthor,price,skuetc.") will be used as the key of the Map, while the corresponding field values and metadata (such asNameandValue) will be its value. We can directly access these named parameters through the dot (.) syntax.
Application scenarios:
Suppose you need to display the author's name prominently below the article title, or highlight the price and stock status at a specific location on a product page. These scenarios usually require you to accurately obtain a certainknown and specificThe value of the custom field, and embed it into the specific position of the page layout, rather than listing uniformly.
- Article page:Display "Author: [Custom field 'author']" at the top of the article
- Product page: Display 'Price: [Custom field 'price']' and 'Stock: [Custom field 'stock']' next to the purchase button
- SEO optimization:Access a specific custom field as
<meta>the content of the tag.
sorted=falseAdvantages:
- Accurate access:You can directly access a specific parameter through the field name 'call field name', without traversing.
- Flexible layout:Can accurately place specific data at any position in the template, seamlessly integrating with the overall design.
- Conditional judgment:Convenient for conditional judgment of specific field values, such as 'If the price is empty, display Negotiable.'
Code example:
{# 假设我们需要在文章标题下方显示作者和阅读时间估算 #}
<div class="article-header">
<h1>{{ archive.Title }}</h1>
{% archiveParams postMeta with sorted=false %}
<p class="meta-info">
{% if postMeta.author.Value %}
作者:{{ postMeta.author.Value }}
{% endif %}
{% if postMeta.read_time.Value %}
· 阅读时长:{{ postMeta.read_time.Value }} 分钟
{% endif %}
</p>
{% endarchiveParams %}
</div>
{# 访问特定字段的Name和Value #}
<p>
字段名:{{ postMeta.author.Name }},字段值:{{ postMeta.author.Value }}
</p>
Here, we directly go throughpostMeta.author.ValueTo obtain the author name, which makes the template code more targeted.
How to choose a strategy suitable for you?
Selectsorted=trueOrsorted=falseIt mainly depends on how you want to process and display these custom data in the template:
- When you need to dynamically display all custom parameters and pay attention to their arrangement in the background, please use
sorted=true.This is suitable for creating a general information display area, such as product specifications tables, detailed parameter lists of articles, etc. - When you need to accurately obtain one to several known custom parameters, and embed them into specific positions in the template, or need to make conditional judgments based on the values of these parameters, please use
sorted=false.This is suitable for displaying specific information at key positions on the page (such as next to the title or price area)
In fact, within the same template, you can flexibly combine these two methods according to the needs of different regions. For example, in the main information area of the product details page, you can usesorted=falseTo display prices, models, and other core parameters, while in the "More Parameters" section at the footer, it usessorted=trueList all the additional specifications. This combination application can make your security CMS website present content both universally and uniquely.
Summary
Of Security CMSarchiveParamstags and theirsortedParameters provide website operators with great flexibility to adapt to diverse content display needs. Understand and apply appropriatelysorted=trueandsorted=falseCan help us manage and optimize website content more efficiently, enhance user experience, and ultimately help achieve content marketing goals.
Frequently Asked Questions (FAQ)
Q1: Can I use the same template simultaneously?sorted=trueandsorted=falseGet custom parameters?
A1:Of course. The Anqí CMS template engine design allows you to use different template engines in different areas of the same template file according to different display needssorted=trueandsorted=falseFor example, you can use it at the top of the pagesorted=falseto display several important, named custom fields (such as author, price), while using a general 'parameter list' area on the pagesorted=trueDynamically display all additional fields. This combination can maximize the flexibility of content presentation.
Q2: If I usesorted=trueTraverse the parameters, but how to skip some specific custom fields?
A2:When you usesorted=trueAfter obtaining the custom parameter array, you canforcombine condition judgment within the loop (iftag) to skip specific fields. Each array element includesitem.Name(field display name) anditem.FieldName(Field name used). You can judge based on these two properties. For example, if you do not want to display the field name used asinternal_notesyou can write the parameter as:
{% archiveParams productSpecs with sorted=true %}
{% for item in productSpecs %}
{% if item.FieldName != 'internal_notes' %}
<li><strong>{{ item.Name }}:</strong><span>{{ item.Value }}</span></li>
{% endif %}
{% endfor %}
{% endarchiveParams %}
Q3: How do custom field data types manifest in the template? For example, numbers, text, or checkboxes.
A3:InarchiveParamsThe custom parameters obtained from the tag,ValueThe field will be treated as a string type (or any type that can be converted to a string) unless the template engine itself performs special parsing. If you have defined checkboxes or radio buttons in the background,ValueIt usually contains a string of options selected by the user (possibly separated by commas). For more complex data types (such as multiple image URLs), you may need to according toValueThe content is to be further parsed or processed in the template. For example, ifValueIs a JSON string containing multiple image URLs, you may need to use a filter or custom logic to convert it to an array before iterating over it.Generally, AnQi CMS strives to provide complex data in a string format that is easy to parse.