How to efficiently concatenate values of different fields in the custom content model (`archiveParams`) using the `add` filter?

Calendar 👁️ 66

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 templatesarchiveParamsStart withsorted=falseWhen accessed in this manner, the code will be clearer.
  • UtilizesetTags: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

Related articles

How to use the `add` filter to dynamically add custom tracking parameters to `tag` links?

In website operation, we often need to track user behavior and evaluate the effectiveness of marketing through different channels.Adding tracking parameters dynamically to website links is an effective method.AnQi CMS is an efficient and flexible content management system that provides a powerful template engine and rich filters, allowing us to easily meet this requirement. Today, let's discuss how to use the `add` filter of Anqi CMS to dynamically add custom tracking parameters to the link of the "Tag".

2025-11-07

Can the `add` filter be used to concatenate array elements processed by the `slice` or `split` filters to form a new string?

When developing templates with AnQi CMS, we often need to flexibly process and display data.This includes string splitting, slicing, and element connection.AnQi CMS provides a rich set of filters (filters) to help us complete these tasks, such as `add`, `slice`, and `split`.Sometimes, we might consider concatenating array elements obtained after processing with `slice` or `split` filters using the `add` filter to form a new string.But is this idea feasible

2025-11-07

How does the `add` filter flexibly combine fixed text with variable content when building dynamic prompt information?

In Anqi CMS template design, building dynamic prompts with real-time interactive features is the key to improving user experience.Whether it is to display product inventory, user welcome messages, or article reading volume, we hope to seamlessly combine fixed text with continuously changing variable content.At this point, the `add` filter becomes a very practical and flexible tool.The `add` filter in the Anqi CMS template is very intuitive: it can add or concatenate two values.This process has high intelligence. If the two values being operated on are of numeric type, it will perform mathematical addition.

2025-11-07

Does the `add` filter support chained calls, such as `{{ var1|add:var2|add:var3 }}` to concatenate multiple variables?

When developing templates in Anq CMS, we often encounter situations where we need to combine, concatenate, or sum multiple variables.Among them, the `add` filter is a very practical tool that allows us to perform addition operations on numbers or concatenate strings.However, some users may be curious whether the `add` filter supports a chain-like call like `{{ var1|add:var2|add:var3 }}` to concatenate multiple variables at once?Familiarize yourself with the template syntax of AnQi CMS after deep study

2025-11-07

How to combine the `add` filter with the `stampToDate` function to concatenate a formatted date and time string?

When managing content in AnQi CMS, we often need to display dates and times in a specific format.The system provides very convenient template tags and filters to handle these requirements.Today, let's talk about how to combine the `add` filter with the `stampToDate` function to concatenate a formatted date-time string, making our content display more flexible and diverse.### Get to know the `stampToDate` function: Format timestamps First, let's review the `stampToDate` function

2025-11-07

How to use the `add` filter to dynamically generate the complete path segment in the breadcrumb navigation (`breadcrumb`)?

In website operation, breadcrumb navigation is one of the key elements to improve user experience and website SEO performance.It clearly shows the user's position on the website and provides a convenient way to return to the previous level page.AnQi CMS provides a powerful and flexible template tag system, where the `breadcrumb` tag can help us easily implement breadcrumb navigation.But if we need to dynamically adjust the path text or links in the breadcrumb navigation, the `add` filter of Anqi CMS can play a unique role.`breadcrumb`

2025-11-07

Does the `add` filter cause garbled or incorrect output when concatenating Chinese and English mixed strings?

When using AnQiCMS for template development, we often need to concatenate different text content, such as dynamically generated titles, descriptions, etc.At this time, the `add` filter has become a powerful tool in our hands.However, when dealing with mixed Chinese and English strings, many friends may worry: Will such concatenation produce garbled characters or cause program errors?Today, let's discuss this issue in detail.

2025-11-07

In the AnQiCMS template, can a simple counter dynamic display be realized through the `add` filter?

AnQiCMS is an enterprise-level content management system developed based on the Go language, providing strong support for content operators with its efficient and flexible features.In daily content operation and template creation, we often encounter the need to process and dynamically display data, such as adding numbers to list items and calculating totals.Among them, the template filter is an important tool for realizing such needs.Today, let's discuss the `add` filter in the AnQiCMS template to see if it can help us achieve a simple dynamic counter display.###

2025-11-07