As an experienced website operations expert, I know that how to flexibly use template tags to display content in a high-efficiency content management system like AnQiCMS (AnQiCMS) is the key to operation success. Today, let's delve into this topic in detail.archiveParamsin the tag,sorted=trueandsorted=falseThese two parameters work in which specific scenarios.

Auto CMS offers great convenience to content creators and operators with its simple and efficient architecture.Among them, the custom content model feature is particularly powerful, allowing us to add various additional fields to content types such as articles and products according to business needs, thus achieving personalized content display.archiveParamsThe label, which is the core tool we obtain data from these custom fields and flexibly present in the front-end template.

archiveParams: Unlock personalized data of the document

archiveParamsThe purpose of the tag is very clear: it helps us obtain all the custom parameters set for a specific document (whether an article or a product) in the AnQi CMS backend.These parameters may include product features, author information, publishing platform, estimated reading time, and so on, which greatly enrich the dimensions of the content.

The basic usage of this tag is usually{% archiveParams params with id="1" sorted=true %}.idParameter specifies which document's custom parameter to retrieve (not specified, the document of the current page is used by default). AndsortedParameters, which are the focus of our discussion today, determine how these custom parameters are extracted and used.

sorted=trueWhen the order of fields is critical for general display:

Whensortedparameter settingstrue(This is also)archiveParamsauto default behavior),it will return aarray of objects arranged in the order defined in the background. Each element in this array is an object containingName(field name) andValue(field value)。

Applicable scenarios:

Imagine that you are designing a product detail page template and need to display a "Product Specifications" or "Additional Information" section at the bottom of the page. In this section, you want to list all custom parameters in the order defined by the backend fields, one by one, for example:

  • Processor: Intel Core i7
  • Memory: 16GB DDR4
  • Storage: 512GB SSD
  • Color: Deep Space Gray

In this case, the display order of the field is usually very important, and you may want this block to be able to adapt to the addition of new parameters in the future without modifying the template code.

sorted=trueThe advantages of:

  1. High versatility:The template does not need to know the specific field names, it only needs to traverse the array to display all parameters. This is very convenient for creating universal components (such as product parameter lists, article metadata lists).
  2. Low maintenance cost:Backend addition, deletion, and modification of custom fields, the front-end template usually does not require any changes, because it is dynamically traversed.
  3. Maintain order:The display strictly follows the sorting of the background custom fields, ensuring the logic and consistency of content presentation.

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>

The code will dynamically traverse and display all custom parameters, which is very suitable for building general and scalable 'specifications' or 'additional information' modules.

sorted=false:English localization for precise positioning of specific data, flexible layout

Whensortedparameter settingsfalsewhenarchiveParamsit will return aEnglish localization for unordered Map (or object)In this Map, each custom field's 'field name used in call' (i.e., the English identifier set during backend definition, such asauthor,price,skuauto will be used as the key for the Map, and the corresponding field values and metadata (such asNameandValue) will be its value. Using the (.) syntax, we can directly access these named parameters.

Applicable scenarios:

These scenarios typically require you to accurately obtain a certain已知且特定的自定义字段的值,并将其嵌入到页面布局的特定位置,而不是统一地罗列。

  • 文章页:Show '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
  • [en] SEO Optimization:Get a specific custom field as<meta>Content of the tag.

sorted=falseThe advantages of:

  1. Accurate access:You can directly access specific parameters through the field name, without the need to traverse.
  2. Flexible layout:Can accurately place specific data at any position in the template, seamlessly integrating with the overall design.
  3. Conditional judgment:It is convenient to make conditional judgments on the values of specific fields, such as 'If the price is empty, then show 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 retrieve the author's name, this makes the template code more targeted.

How to choose a strategy that suits you?

Choosesorted=trueOrsorted=falseIt mainly depends on how you want to handle 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 usesorted=true.This applies to creating general information display areas, 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 locations in the template, or need to make conditional judgments based on these parameter values, please usesorted=false.This is suitable for displaying specific information at critical positions on the page (such as next to the title, price area)

In fact, within the same template, you can flexibly combine the use of these two methods according to the needs of different regions. For example, in the main information area of the product detail page,sorted=falseShow core parameters such as price, model, etc., while using the 'More Parameters' section at the footersorted=trueList all the additional specifications. This combination usage can make your CMS website content presentation both general and distinctive.

Summary

Anqi CMS'sarchiveParamsTags and theirsortedParameters, provide great flexibility for website operators to adapt to diverse content display needs. Understand and apply appropriately.sorted=trueandsorted=falseIt can help us manage and optimize website content more efficiently, enhance user experience, and ultimately help achieve the goals of content marketing.


Common Questions (FAQ)

Q1: Can I use the same template at the same time?sorted=trueandsorted=falseDo you want to get custom parameters?

A1:Of course. The template engine design of Anqi CMS allows you to use different areas in the same template file according to different display requirements,sorted=trueandsorted=falseFor example, you can use it at the top of the pagesorted=falseto display several important, named custom fields (such as author, price), and use it in some general "parameter list" areasorted=trueAutomatically display all additional fields. This combination can maximize the flexibility of content presentation.

Q2: If I usesorted=trueHow to traverse parameters and skip some specific custom fields?

A2:When you usesorted=trueObtained after getting the custom parameter array, you canforcombine condition judgment within the loop (iftag) to skip specific fields. Each array element containsitem.Name(field display name) anditem.FieldName(Field call name). You can judge according to these two properties. For example, if you do not want to display the field call nameinternal_notesof the parameters, you can write it like this:

{% 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 does the data type of custom fields manifest in the template? For example, numbers, text, or checkboxes.

A3:InarchiveParamsThe custom parameters obtained in the tag,ValueThe field will be treated as a string type (or a type that can be converted to a string) unless the template engine itself performs special parsing. If you define a checkbox or radio button field in the background,ValueThe string usually includes the options selected by the user (possibly separated by commas). For more complex data types (such as multiple image URLs), you may need to baseValueThe content is 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.usually, anq CMS will try to provide complex data in an easy-to-parse string format.