In Anqi CMS, when managing content, we often encounter the need to combine multiple field values from a custom content model into a text that is more expressive or conforms to a specific display format.For example, you may need to concatenate the product's 'brand' and 'model' into a complete product name, or combine the contact's area code and phone number.This is provided by AnQiCMSaddFilter combinationarchiveParamsTags, can help us efficiently implement these operations.
Flexible content model witharchiveParamsapplication
One of the core strengths of AnQiCMS is its flexible content model.It allows us to define dedicated fields for different types of content (such as articles, products, events, etc.) rather than being limited to fixed titles and text.For example, for a 'product' model, we can define 'Brand', 'Model', 'SKU (Product Code)', 'Color', and many other custom fields.
When accessing these custom fields in the template,archiveParamsTags are our helpful assistants. They can retrieve all custom parameters of the current document or a specified document. There are two ways to use them, which can be flexibly chosen according to your specific needs:
One is when you know the specific field name, through settingsorted=falseto access in the form of key-value pairs (map). This way, you can access object properties directly throughparams.FieldName.ValueAccess a specific field, for exampleparams.Brand.ValueThis way the code is more concise and straightforward when the field name is clear.
Another is the default case or when you need to iterate over all custom fields, setsorted=trueIt will return an ordered array object. Each array element containsName(field display name) andValue(Field value). You can display these fields in a loop, but this is slightly less efficient when directly concatenating specific fields.
In order to efficiently concatenate the values of different fields, we usually choosearchiveParams params with sorted=falsebecause it allows us to directly point to and retrieve the desired field values.
addthe cleverness of the filter
Once we pass througharchiveParamsRetrieve the value of a custom field, and then it's about how to concatenate them. In the AnQiCMS template,addThe filter is particularly useful at this moment. It's like in programming languages,+Operators that can perform numeric addition and string concatenation.It is also worth mentioning that it can intelligently handle different types of data: if you try to add numbers to numbers, it will perform mathematical operations;If you concatenate a string with other data types, it will try to convert non-string types to strings before concatenating.If the conversion fails, it will elegantly ignore the parts that cannot be concatenated without interrupting the rendering of the entire template, which greatly enhances the robustness of the template.
addThe basic syntax of the filter is{{ obj|add:obj2 }}of whichobjIt is the original value,obj2The value to be added. This operation can be chained to concatenate multiple values and strings continuously.
Practice: Efficiently concatenate field values
Let's go through several specific scenarios to see how toarchiveParamsandaddcombine filters to efficiently concatenate field values.
Scenario one: Build product display titles
Assuming your custom product model includes the 'Brand' and 'Model' fields, you want to display them on the page as 'Brand - Model'.
First, get these custom fields in the template:
{% archiveParams productInfo with sorted=false %}
Then, you can concatenate them like this:
<h2 class="product-title">
{{ productInfo.Brand.Value | add: " - " | add: productInfo.Model.Value }}
</h2>
This code will get first:BrandThe value, then appending a static string” - “, then appendingModelThe value, finally forming a complete title, such as 'AnQiCMS - Advanced Edition'.
Scenario two: Concatenating a prefixed unique identifier.
If your product has a unique code composed of "Product SKU (SKU)" and "Color Code (ColorCode)" and you want to display it in the form of "Product Code: SKU-ColorCode".
{% archiveParams productInfo with sorted=false %}
<p class="product-code">
{{ "产品编码:" | add: productInfo.SKU.Value | add: "-" | add: productInfo.ColorCode.Value }}
</p>
Here, we first concatenate a static string "Product Code:", then concatenate the SKU value, the connector "-", and the ColorCode value.
Scenario three: Handling optional field concatenation
In practical applications, some custom fields may not be required.If a field is empty, we do not want it to leave an extra connector in the concatenated result.At this point, we can combineifFlexible handling with logical judgment tags
Assuming you want to concatenate "CPU model" and "memory capacity", but one or both of these fields may be empty.We hope to display them only when they have values, and separated by slashes.
{% archiveParams computerSpecs with sorted=false %}
<p class="computer-summary">
{% set cpu = computerSpecs.CPUType.Value %}
{% set ram = computerSpecs.RAMSize.Value %}
{% if cpu and ram %}
{{ cpu | add: " / " | add: ram }}
{% elif cpu %}
{{ cpu }}
{% elif ram %}
{{ ram }}
{% else %}
暂无配置信息
{% endif %}
</p>
In this example, we first assign the field value to a temporary variablecpuandramIt improved the readability of the code. Then, throughif-elif-elseThe structure, judge whether two fields exist, and concatenate or display them separately according to the situation. In this way, even if a field is missing, the output result is still elegant, without extra slashes.
Some suggestions
- Keep field names clear:When defining custom fields in the background, use descriptive and easily understandable English names so that they can be used in templates
archiveParamsStart withsorted=falseWhen accessed in this manner, the code will be clearer. - Utilize
setTags:When your concatenation logic becomes complex, you can consider using{% set variable = value %}The label stores intermediate results in temporary variables, builds the final string step by step, which greatly enhances the readability and maintainability of the template. - Thorough testing:Especially when involving conditional judgment and concatenation of various data types, be sure to test your template under different data states to ensure that the output meets expectations, and avoid blank connectors or format errors.
By flexibly using AnQiCMS'sarchiveParamsTags andaddFilter, we can easily handle various custom content field concatenation requirements, making the website content display more dynamic and accurate.This not only improves the user experience, but also provides more possibilities for website SEO optimization, after all, clear and structured information is always easier for search engines to favor.
Frequently Asked Questions (FAQ)
Q1:addCan the filter perform numerical operations in addition to string concatenation?A1: Yes,addThe filter is very intelligent. When you pass it two numbers, it performs mathematical addition. For example,{{ 5 | add: 2 }}It will display7When you mix numbers and strings, it will try to convert the number to a string and then concatenate.
Q2: If the custom field I want to concatenate may be empty, how can I avoid outputting extra concatenation symbols?A2: This is a very common requirement in actual development. You can combine it with the AnQiCMS template inifLogic judgment labels to solve. Before concatenation, check if the value of the related fields exists, and only concatenate when there is a value in the field, and add a connector if necessary.The scenario of 'processing the concatenation of optional fields' provides a detailed example.
Q3:archiveParamsorder to obtain custom fields using tagssorted=trueandsorted=falseWhat is the difference? Which one should I choose?A3: sorted=true(默认值)It will return an ordered array containing custom field objects, each withNameandValueproperties. This method is suitable for iterating over all custom fields. Andsorted=falseReturns a key-value pair (map), you can directly access the value of a specific field by the field name (such asparams.YourFieldName.Value) for efficient