Use arithmetic operators for addition directly

First, for some direct, fixed number addition, or when you have determined the variable type is a number, AnQiCMS template engine allows you to use the plus sign directly+Performing calculations. This is very similar to the way we perform mathematical calculations in programming languages, where the template engine intelligently parses expressions and provides results.

For example, if you want to add two integers in a template:

{{ 10 + 20 }}

The result will be displayed30.

Similarly, the operation is the same for floating-point numbers:

{{ 5.5 + 3.2 }}

This will result in8.7.

When you need to add a variable to a fixed number, you can also do it like this. Assuming youritemobject has aPriceattribute:

商品价格加上运费:{{ item.Price + 15.00 }}

here, the template willitem.Priceadd the value of15.00and display the total price.

In addition to addition, you can also use subtraction (-), multiplication (*), division (/) even modulo (%)Basic arithmetic operations, all of which follow similar direct operation rules. This method is simple and intuitive, suitable for those scenarios where the operation object types are clear and the operations are direct.

UseaddThe filter implements a more flexible addition

When the source of the number you are dealing with is uncertain, or you may need to concatenate numbers with strings (although this is not strictly mathematical addition, it is common in some display scenarios), in the AnQiCMS template,addThe filter will be a more powerful and versatile choice. This filter can not only perform addition of numbers but also intelligently handle different types of data.

addThe method of using a filter is to add a pipe symbol after a variable|then isadd:and the value you want to add. For example:

{{ 变量 | add: 要相加的值 }}

Let's look at some specific examples:

  1. Pure number addition:Assuming you have two numeric variablesnum1andnum2.

    {% set num1 = 15 %}
    {% set num2 = 7 %}
    计算结果:{{ num1|add:num2 }}
    

    This will display22.

  2. Add floating-point numbers:

    {% set float1 = 12.3 %}
    {% set float2 = 8.7 %}
    浮点数结果:{{ float1|add:float2 }}
    

    the result will be21.0.

  3. Add variables with literals:Assumearchiveobject has aViews(View count) attribute, do you want to add extra on top of this100.

    总浏览量:{{ archive.Views|add:100 }}
    
  4. The 'concatenation' (concatenation) of numbers and strings:This isaddA very practical feature of the filter. When one of the operands is a string and the other is a number,addThe filter will try to concatenate strings.

    {% set quantity = 5 %}
    显示数量:{{ "商品数量:"|add:quantity }}
    

    This will display.商品数量:5.

addThe filter will retain the original number or directly perform string concatenation when encountering mixed types that cannot be added mathematically (such as adding a pure number to a string that cannot be converted to a number), which increases the error tolerance of template data processing and makes your template code more robust.

When to choose which method?

Generally speaking, if your operands are clear numeric types (whether integers or floating-point numbers) and involve simple addition, subtraction, multiplication, or division, you can directly use+The equality operator makes it more concise and intuitive, and the code readability is also higher.

When you need to handle variables from different sources, the types of which may be uncertain, or when there is a need to concatenate numbers and strings,addThe filter provides greater flexibility and stronger fault tolerance, avoiding potential errors caused by type mismatches, allowing the template to run stably in complex scenarios.

In the actual operation of Anqi CMS, these template calculation functions are very practical.For example, on an e-commerce website, you may need to calculate and display the cumulative sales of products, total inventory, or display dynamic reading volume on article pages (such as the basic reading volume plus the new reading volume on the day).Apply these addition operation skills flexibly, allowing you to build and maintain websites more easily, achieving more dynamic and expressive content display.


Frequently Asked Questions (FAQ)

  1. Q: Besides addition, what arithmetic operations does AnQiCMS template support?A: Besides addition (+), you can also use subtraction directly in the template (-), multiplication (*), division (/)、modulus(%) and other basic arithmetic operators. In addition,addThe filter is mainly used for addition and concatenation, but it can also be regarded as a "cumulative" operation with stronger versatility.

  2. Q: If I want to add a numeric variable and a string variable together, what will happen?A: If you use directly+The operator performs the operation, the template engine will try to convert the string to a number for calculation. If the conversion fails, it may cause an error. But if usingaddA filter that handles more intelligently: if both are numbers, then perform mathematical addition; if one is a number and the other is a string, it usually performs string concatenation, converting the number to a string and then connecting it.

  3. Q: How to control the precision of the decimal point of the calculation result?A: If you have specific requirements for the precision of the floating-point number calculation result, you can usefloatformatthe filter to control it. For example,{{ value|floatformat:2 }}WillvalueFormatted to two decimal places. If a specific number of digits is not specified, it will default to one decimal place, and if the last digit is 0, the decimal part will not be displayed.