In the template development of AnQi CMS, we often need to perform calculations on numbers, such as calculating the total price of a certain product, or dynamically adjusting the displayed quantity. AnQi CMS template engine provides a rich set of filters to handle these scenarios, whereaddThe filter is a powerful tool for performing numerical addition or string concatenation. It offers intelligent type handling mechanisms, allowing developers to more flexibly meet the data calculation needs within templates.
addFilter: Smart handling of addition between numbers and strings.
addThe core function of the filter is to perform addition operation on two values.Its uniqueness lies in the fact that it is not limited to simple addition of numbers, but also can intelligently handle scenarios involving the mixture of numbers and strings.When both operands are numbers, it performs regular mathematical addition.add
UseaddThe basic syntax of the filter is very intuitive:
{{ obj|add:obj2 }}
Among themobjIs the first operand.obj2isaddThe second operand passed to the filter.
Actual application scenarios and examples.
To better understandaddThe power of the filter, we demonstrate its different usages through several common examples.
1. Adding pure numbers
This isaddThe most direct use of the filter, applicable to addition between two integers or floating-point numbers.
{# 示例:计算两个整数的和 #}
{{ 5|add:2 }} {# 输出:7 #}
{# 示例:计算两个浮点数的和 #}
{{ 3.14|add:2.5 }} {# 假设输出:5.64 #}
{# 示例:数字与变量相加 #}
{% set quantity = 10 %}
{% set price = 15 %}
库存总量:{{ quantity|add:price }} {# 输出:25 #}
2. Addition of a number with a number string
When one operand is a number and the other is a string representing a number,addThe filter will try to convert the string to a number and then perform the addition operation.
{# 示例:整数与数字字符串相加 #}
{{ 5|add:"2" }} {# 输出:7 #}
{# 示例:浮点数与数字字符串相加 #}
{{ 3.14|add:"0.86" }} {# 输出:4.0 #}
3. Concatenation of numeric and non-numeric strings
This isaddAnother manifestation of filter intelligent processing. IfaddThe filter discovers that one operand is a number and the other is a string that cannot be converted to a number. It then falls back to concatenating them as strings.
{# 示例:数字与普通字符串拼接 #}
{{ 5|add:"CMS" }} {# 输出:5CMS #}
{# 示例:将ID与描述信息拼接 #}
{% set item_id = 1001 %}
{% set item_type = "商品" %}
产品编号:{{ item_id|add:item_type }} {# 输出:1001商品 #}
4. String Concatenation
When both operands are strings,addthe filter will work like traditional string concatenation.
{# 示例:两个字符串拼接 #}
{{ "安企"|add:"CMS" }} {# 输出:安企CMS #}
{# 示例:拼接带空格的字符串 #}
{{ "hello "|add:"world" }} {# 输出:hello world #}
5. Handling Null Values (nil/nothing)
In the template, data may sometimes be missing ornil.addThe filter behaves quite robustly when encountering such values, directly ignoring these empty values and only performing operations or concatenations on valid data.
{# 示例:数字与空值相加 #}
{{ 5|add:nothing }} {# 假设 nothing 为空值,输出:5 #}
Use suggestions and precautions
- Clarify the operation intention:When using
addWhen filtering, it is best to be clear about whether you want to perform numeric addition or string concatenation.Although it is intelligent, clear intention helps avoid unexpected results.integerorfloatThe filter converts values to numeric types. - Debugging and verification:If the calculation result does not meet expectations,
dumpA filter to check the actual type and value of variables, which helps to quickly locate problems. - Avoid over complexity:Although
addThe filter provides flexibility, but for calculations involving multiple operations or complex logic, it is recommended to process the data on the backend and only perform simple presentation layer calculations in the template.
By using flexibilityaddFilter, users of Anqi CMS can efficiently handle various combinations of numbers and strings in templates, thereby building more functional and more accurate data presentation web pages.
Common Questions (FAQ)
addDoes the filter support subtraction, multiplication, division, and other arithmetic operations?addThe filter is specifically designed for addition operations and intelligent string concatenation. For other arithmetic operations such as subtraction, multiplication, and division, the Anqi CMS template engine provides dedicated arithmetic operation tags (such astag-calc.mdmentioned{{ 10-100 }}or{{ 2 * 5 }})can be directly used in template expressions.How to ensure that my variable is a numeric type before addition to avoid unnecessary string concatenation?To ensure
addThe filter performs pure numeric addition, you can useintegerorfloatThe filter first converts the variable to a numeric type. For example:{{ some_string_value|integer|add:10 }}Can ensuresome_string_valueIs treated as an integer before addition.addIn what circumstances will the filter perform string concatenation instead of numeric addition?addThe filter will perform string concatenation in the following two main cases:- When one of the operands is a string and this string cannot be effectively parsed as a number (for example,
{{ 5|add:"不是数字" }}It will output5不是数字). - When both operands are strings (for example,
{{ "Hello "|add:"World" }}It will outputHello WorldIt will try to perform numeric addition first, and only fallback to string concatenation if it cannot complete the numeric addition.
- When one of the operands is a string and this string cannot be effectively parsed as a number (for example,