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

Calendar 👁️ 66

In AnQi CMS template design, building dynamic prompt information with real-time interactive sense is the key to improving user experience.Whether it is to display product inventory, user greeting messages, or article reading volume, we hope to seamlessly combine fixed text with continuously changing variable content. At this point,addThe filter becomes a very practical and flexible tool.

addThe filter's role in the Anqi CMS template is very intuitive: it can add or concatenate two values.This process has a high degree of intelligence, if the two values operated on are both numeric types, it will perform mathematical addition.And if one or two of the values are strings, it will concatenate them.This flexibility allows us to not worry about data type mismatch issues, we can directly integrate different types of data together, which is very convenient.

Flexible concatenation of scenarios and examples.

UnderstoodaddThe basic principle of the filter, let's see how it plays a role in the actual template construction.

  1. Simple numerical additionWhen we need to perform a simple addition of two numbers in a template,addthe filter can easily complete it.

    {% set num1 = 5 %}
    {% set num2 = 2 %}
    结果:{{ num1|add:num2 }}
    {# 显示结果:7 #}
    

    It also supports floating-point numbers, or automatically converts string numbers to numbers for calculation:

    {% set price = 10.50 %}
    {% set tax = 2.30 %}
    总金额:{{ price|add:tax }}
    {# 显示结果:12.8 #}
    
  2. Concatenation of plain text contentIn addition to numbers,addThe filter can also handle the concatenation of plain text content well.

    {% set brand = "安企" %}
    {% set product = "CMS" %}
    产品名称:{{ brand|add:product }}
    {# 显示结果:安企CMS #}
    
  3. Combine fixed text with dynamic variablesThis isaddThe most common application scenario of the filter, especially suitable for building dynamic prompt information.We can easily concatenate a descriptive text with data variables obtained from the backend.For example, display copyright information at the footer of the website, combining fixed text with the website name and the current year from system variables:

    <p>版权所有 © {{ now "2006"|add:' '|add:system.SiteName }}</p>
    {# 假设当前年份是2023,网站名称是“安企CMS”,显示结果:版权所有 © 2023 安企CMS #}
    

    On the article detail page, display the number of reads of the article:

    <p>本文已被阅读 {{ archive.Views|add:' 次' }}</p>
    {# 假设文章阅读量是1234,显示结果:本文已被阅读 1234 次 #}
    

    If you need to display detailed contact information on the contact us page, you can also make use ofaddFilter:

    <p>客服热线:{{ contact.Cellphone|add:'(周一至周五 9:00-18:00)' }}</p>
    {# 假设联系电话是13800138000,显示结果:客服热线:13800138000(周一至周五 9:00-18:00) #}
    
  4. Handle cases where the variable is empty or the type is incompatible addThe filter also performs robustly when handling variables that may be empty. If one of the operands is a null value (nothingornilIt tries to keep non-empty parts or ignore empty parts to avoid template errors.

    {% set value = 100 %}
    {% set empty_var = nothing %}
    结果:{{ value|add:empty_var }}
    {# 显示结果:100 (将empty_var忽略)#}
    
    {% set text = "产品编号:" %}
    {% set id = nothing %}
    产品信息:{{ text|add:id }}
    {# 显示结果:产品编号: (如果id为空,会直接拼接空字符串) #}
    

    This error-tolerant mechanism makes developers feel more at ease when dealing with variables that may not exist.

Use suggestions and precautions

  • Readability first:ThoughaddFilters support chained calls (such as{{ var1|add:var2|add:var3 }}), but when concatenating long texts or multiple variables, excessive chaining may reduce the readability of the template. At this point, it may be considered to usesetLabel the intermediate results first, or pre-process complex strings at the controller level in the Go language.
  • Context-aware:Ensure the variable you are using is available in the current template context. The Anqi CMS template tags (such asarchiveDetail/system/contactetc.) will provide the corresponding context data.
  • Combined with other filters: addThe filter can be used with other filters, such asdefault/truncatecharsUse them in combination to achieve more fine-grained control, such as providing default values for variables that may be empty, or limiting the length of the concatenated string.

ByaddThe filter makes the dynamic content generation of Anqi CMS templates extremely simple and efficient, helping us to seamlessly integrate fixed information with variable business data in a natural and smooth way, thereby jointly constructing user-friendly and informative page experiences.


Frequently Asked Questions (FAQ)

Q1:addFilters and using directly{{ var1 }}{{ var2 }}What is the difference? A1:directly{{ var1 }}{{ var2 }}It will directly output the values of two variables one by one, always performing string concatenation. AndaddThe filter is smarter: if both operands are numbers, it will perform mathematical addition; if one or both are strings, it will perform string concatenation. This meansaddWhen processing numbers, you can perform calculations, not just simple string concatenation.

Q2: IfaddFor example, one variable in the filter is nullnothingornil) what is the impact? A2:WhenaddThe filter usually shows strong tolerance when encountering null values.If the other operand is a number, the empty value is ignored, and only the number itself is retained;If the other operand is a string, null values are usually treated as empty strings, and the final result is a concatenation of strings without null content.This helps to avoid errors when the template is missing data.

Q3:addCan the filter perform complex mathematical operations? For example, multiplication or division? A3: addThe filter focuses only on addition and concatenation operations. If you need to perform more complex mathematical calculations such as multiplication, division, or advanced arithmetic logic, Anqi CMS provides specializedcalcLabel (please refer to)tag-calc.mdThe document supports a wider range of mathematical expressions.addThe design philosophy is to keep it simple, focusing on its core addition/joining function.

Related articles

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 use the `add` filter to dynamically add 'Published on:' before the blog post's publish time?

When operating a blog, we often hope that the publication time of the articles can be presented in a more intuitive and brand style compliant manner.For example, adding 'Published on:' before the date can make it clear to the reader that this is a published article.AnQiCMS (AnQiCMS) with its flexible template engine makes such customization very simple.Today, let's discuss how to cleverly use the built-in `add` filter of AnQiCMS to dynamically add the phrase 'Published on:' before the publication time of blog posts.

2025-11-07

What is the final concatenation result when one operand of the `add` filter is a number and the other is a string that cannot be converted to a number?

AnQi CMS is an enterprise-level content management system developed based on the Go language, dedicated to providing users with efficient and customizable content management solutions.In daily content operation and template development, we often use various template tags and filters to process data, where the `add` filter is a very practical feature that can help us conveniently perform numerical addition or string concatenation.However, when operands of mixed types are involved, the logic of their behavior becomes particularly important.

2025-11-07

How to use the `add` filter in a loop to implement alternating row colors or dynamically concatenate different CSS class names?

In modern web design, the visual presentation of list content has an important impact on user experience.Whether it is an article list, product display, or comment section, by giving list items different styles, such as alternating row colors or applying different class names based on specific conditions, it can significantly improve the readability and aesthetics of the page.AnQiCMS is a powerful template engine that provides flexible tools to meet these dynamic style requirements, where the `for` loop's `cycle` tag and `add` filter are our reliable assistants.###

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 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

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

In AnQi CMS, we often encounter the need to combine multiple field values of 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 "brand" and "model" of the product into a complete product name, or connect the "area code" and "phone number" of the contact.At this time, the `add` filter provided by AnQiCMS combined with the `archiveParams` tag can help us efficiently perform these operations.###

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