AnQiCMS (AnQiCMS) is an efficient and flexible content management system that provides great freedom in content display.Among them, the custom parameter feature allows website operators to add unique attributes to content models such as articles, products, and others according to their actual business needs, such as product specifications, author information, source channels, etc.archiveParamsLabels, especially their core parameterssortedingenious application.
The flexibility of custom parametersarchiveParamsbridging role
In the AnQiCMS backend, when you create a content model (such as "article" or "productThese custom parameters greatly enrich the expression of content, meeting the personalized needs of different industries.
We need to call and display these custom parameters carefully set in the background on the front-end pagearchiveParamsThe tag acts as a bridge, securely and efficiently passing structured data from the backend to your template for rendering and display.
sortedParameter: the key to controlling the output order
archiveParamsThe core charm of tags lies in itssortedThe parameter determines how the custom parameters you obtain from the background are presented, thereby affecting the way you access and iterate over these parameters in the template. It supportstrueandfalseTwo values, by defaulttrue.
WhensortedWithtrueWhen (the default behavior): the beauty of traversing an ordered array
When you usesorted=true(or not explicitly specifiedsortedbecause the parameter,trueis the default value whenarchiveParamsthe tag will treat the custom parameters of the specified document asan ordered array of objectsand return it to the template. Each element of this array is a container that includesNameandValueThe field object, respectively corresponding to the Chinese name of the custom parameter and its corresponding value.
The advantage of this pattern is that it will output strictly according to the order of the custom fields defined in the background "content model". This means that if you want the order of the custom parameters displayed on the front-end to be consistent with the order set in the background, or if you need to traverse all custom parameters dynamically without concerning about the specific field names, thensorted=trueis your best choice. In the template, you usually combineforloop to traverse these parameters, thus achieving flexible and dynamic display.
For example, if you want to list all the custom properties of the article one by one, you can write the template code like this:
{% archiveParams params %}
<div>
{% for item in params %}
<div>
<span>{{item.Name}}:</span>
<span>{{item.Value}}</span>
</div>
{% endfor %}
</div>
This code will traverseparamsEach custom parameter in the array, and its name (item.Name) and value (item.ValuePrint it out, perfectly following the order defined by the background.
WhensortedWithfalseAt: precise positioning of unordered mapping
withsorted=trueRelatively, when you are going tosortedthe parameter tofalsethen,archiveParamsThe tag will return aUnordered mapping (map) objectIn this mode, you can directly access it by using the 'call field' of custom parametersNameandValue.
sorted=falseThe main advantage lies in its directness and efficiency.When you know exactly which specific custom parameters to display and do not want to perform additional loops, this method is very convenient.It is like a dictionary, you can directly access the corresponding value through the key (i.e., the custom field's 'access field'), without concerning about their arrangement in the background.
For example, if your article model has a field namedauthorThe custom parameter of the (field called), you can directly obtain its value in the following way:
{% archiveParams params with sorted=false %}
<div>作者名称:{{params.author.Value}}</div>
<div>作者标签名:{{params.author.Name}}</div>
Or, if you only want to get the value of a custom field and output it directly, you can also omit the variable name and output it directly inarchiveDetailthe tag throughnameParameter specification, to some extent, also utilizes similarsorted=falsethe direct access thought:
<div>文章作者:{% archiveDetail with name="author" %}</div>
Although this direct inarchiveDetailis usednameThe way of using parameters is more concise, butarchiveParams params with sorted=falseProvided aparamsobject that allows you to easily access multiple specified fields within a context, rather than callingarchiveDetail.
how to choose an appropriatesortedvalue?
SelectsortedThe value of the parameter, mainly depends on your content display requirements:
Use
sorted=true(Default):- When you want to display all custom parameters dynamically in an area and maintain their order defined in the background.
- When you are unsure about which custom parameters will be present, or when the number and type of custom parameters change frequently, using a loop to traverse them can better adapt to this change.
- For example, display the list of all technical parameters of the product, or all the metadata of the article.
Use
sorted=false:- When you need to accurately retrieve and display one or more known specific custom parameters.
- When performance is a key consideration, and you only want to access a few specific fields while avoiding unnecessary iteration.
- For example, directly display the 'author' and 'publish date' below the page title, or show the 'price' at the top of the product details page.
In summary,archiveParamslabel'ssortedThis parameter provides you with the powerful ability to flexibly control the output method of custom parameters.Understand and use it well, it will make your AnQiCMS website content management and front-end display more efficient, accurate, and flexible.
Frequently Asked Questions (FAQ)
Ask: When custom parameters are defined in the background "Content Model", their order affects the front end
archiveParamsDoes the tag output? Answer:Yes, ifarchiveParamsThe tag uses the defaultsorted=truepattern, then pass through the templateforWhen iterating over these parameters, their output order will strictly follow the sequence you define for these custom fields in the AnQiCMS backend "Content Model". If you changesortedis set tofalseSince it is an unordered mapping, the order of access is no longer meaningful, you need to directly obtain it through the 'field calling field'.Ask: I want to display a specific custom parameter, such as a parameter named "author", using
sorted=trueOrsorted=falsemore efficiently? Answer:In this case, usingsorted=falsewill be more direct and efficient. Becausesorted=falseIt returns a map object, you can directly access it through its 'call field', for example{{params.author.Value}}, without having to traverse the entire parameter list. If you usesorted=trueYou then need to iterate over all parameters and judge inside the loopitem.Namewhether it is "author", which is relatively inefficient.Question: If I use
sorted=falseHow to access a non-existent custom parameter, for example{{params.nonExistentField.Value}}Will the page report an error? Answer:In most cases, AnQiCMS's template engine will not directly throw a fatal error causing the page to crash. It will simply{{params.nonExistentField.Value}}Parsed as an empty string or a null value (depending on the implementation), no content will be displayed on the page.This makes the template have better error tolerance when facing missing fields.{% if params.nonExistentField %}Avoid outputting empty tags or unnecessary structure.