How do you handle the case when one of the variables is `nothing` or `nil` while using the `add` filter, and what will be the output result?

Calendar 👁️ 71

In AnQiCMS template development, we often need to perform simple addition operations or string concatenation,addThe filter is born for this. It is very convenient to use and can intelligently handle the addition of numbers and concatenation of strings.If you give it two numbers, it will perform mathematical addition;If you give it two strings, it will concatenate them.Even when numbers and strings are mixed, AnQiCMS will try to perform a reasonable conversion and output the result.

For example, adding numbers:

{{ 5|add:2 }}
{# 显示结果: 7 #}

Or, perform string concatenation:

{{ "安企"|add:"CMS" }}
{# 显示结果: 安企CMS #}

Then, when one of the variables is:nothingornil(In the context of a template, it usually represents that the variable has not been assigned or is empty)addThe behavior of the filter is particularly critical. Many users may worry that this could lead to template rendering errors or unexpected results.

AnQiCMS'addThe filter is processingnothingornilWhen encountering a situation, it will adopt a very practical strategy:It will intelligently ignore thatnothingornilIt will only process the valid operands.

This means that if you try to add a number with anothingWhen added, the result will be the valid number itself, not an error or zero. For example:

{% set my_optional_number = nothing %}
{{ 10|add:my_optional_number }}
{# 显示结果: 10 #}

Similarly, ifnilis the first operand, the result will be the same:

{{ nil|add:20 }}
{# 显示结果: 20 #}

For string concatenation, the behavior is similar:

{{ "开始"|add:nil }}
{# 显示结果: 开始 #}

AnQiCMS'addThe filter tries to convert the operands internally. When encounteringnothingornilWhen, it fails to convert it into a valid type that can be used for addition or concatenation (such as a number or a non-empty string), it will ignore the invalid operand and only process the valid operands. This design ensures that the template can render as normally as possible even when the data is incomplete, avoiding single

Related articles

In AnQiCMS template, why does `{{ 5|add:"CMS" }}` output `5CMS`? What are the rules for mixing numbers and strings in concatenation?

In AnQiCMS template development, we sometimes encounter some seemingly simple expressions that hide clever logic.Among the phenomenon that `{{ 5|add:"CMS" }}` finally outputs `5CMS`, it often makes friends who are new to it feel curious.This involves the unique rule of mixed number and string splicing in AnQiCMS template engine.Today, let's delve deeper into this interesting mechanism.### The "filter" in AnQiCMS template and the magic of `add` In AnQiCMS

2025-11-07

How does the `add` filter seamlessly concatenate the values of two string variables?

In AnQiCMS template development, we often need to combine different data fragments, whether it is the calculation of numbers or the integration of text information.Among them, the `add` filter is a very practical and flexible tool that can help us easily perform the "addition" operation of two variable values, achieving seamless splicing effects.The design philosophy of the `add` filter is to take into account both numeric calculations and string concatenation.When you apply it to two numeric variables, it performs standard mathematical addition to calculate their sum.

2025-11-07

How can the `add` filter be used to perform exact addition of numbers in the Anqi CMS template?

In Anqi CMS template development, we often need to perform calculations on numbers, such as counting the total price of a product or dynamically adjusting the display quantity.The AnQi CMS template engine provides a rich set of filters to handle these scenarios, where the `add` filter is a powerful tool for adding numbers or concatenating strings.It handles the data calculation needs in templates with its intelligent type handling mechanism.

2025-11-07

How to dynamically obtain and display the unique user group in the template

In AnQiCMS template, dynamically obtaining and displaying the unique identifier of the user group is a key step to achieving personalized content display and user permission management on the website.AnQiCMS provides flexible and powerful template tags to help us easily achieve this goal.Next, we will discuss how to use these tags to accurately obtain and present user group information. ### Understanding User Group Identification in AnQiCMS In AnQiCMS, user groups are the foundation for managing user permissions and content access.

2025-11-07

How to dynamically add 'Read More' link text to each title on the AnQiCMS article list page?

When managing and operating a website, the article list page is an important entry for users to browse content and discover information.To enhance user experience, clearly guiding visitors to enter the article details, it is a common practice to dynamically add a "Read More" or "View Details" link text to each title in the list.AnQiCMS (AnQiCMS) provides a powerful and flexible template system, allowing us to easily achieve this feature.

2025-11-07

What is the practical difference in applying the `add` filter and the `stringformat` filter in template string concatenation?

In the AnQi CMS template world, flexibly handling strings is an indispensable skill for building dynamic web pages.Among them, the `add` filter and the `stringformat` filter can help us concatenate or combine strings, but their design philosophy and application scenarios are obviously different.Understanding these differences allows us to make more informed choices when developing templates, resulting in more efficient and readable code.### `add` filter: A concise and intuitive concatenation tool `add`

2025-11-07

How to use the `add` filter to dynamically add 'Yuan' or a specific currency symbol after the product price?

When managing website content, especially when it involves product display, we often need to present price and other numerical information in a more user-friendly manner.单纯显示一串数字,比如 `199`,往往不够直观,如果能自动加上货币符号,比如 `199元` 或 `$199`,就能大大提升信息的可读性和专业性。AnQiCMS (AnQiCMS) with its flexible template system makes it very simple to meet such requirements, and the key to this is the clever use of the `add` filter.### Understand `add`

2025-11-07

How to use the `add` filter to build dynamic SEO-friendly URL paths in the Anqi CMS template?

In Anqi CMS template creation, flexible and SEO-friendly URL paths are the key to improving the website's search engine performance.Although AnQi CMS provides powerful pseudo-static rule configuration capabilities in the background, generating static URLs through patterns such as `/module-id.html`, but in certain specific scenarios, we may need to control or combine the local URL path more dynamically and finely within the template to meet unique business needs or marketing strategies.At this point, the `add` filter has become a very practical auxiliary tool.###

2025-11-07