During the template development process of AnQiCMS, we often encounter scenarios where numerical calculations or logical judgments are required. The system provides us with various processing methods, whereaddfilters andtag-calc.mdThe arithmetic operation capability introduced is two commonly used tools. Although they both involve numerical processing, they have significant differences in functional positioning, usage methods, and complexity.

Firstly, let's understandaddFilter. As the name suggests,addThe filter is mainly used to performaddition operations or string concatenation. Its unique feature is the flexibility in handling data types. When you useaddThe filter will attempt to intelligently add two values together: if both are numbers, it will perform mathematical addition; if either or both are strings, it will concatenate them. What is more notable is that even if automatic type conversion fails,addThe filter also has a certain degree of fault tolerance, ignoring content that cannot be converted and continuing to process the convertible part.

For example, if you want to add the numbers 5 and 2, you can write it as{{ 5 | add: 2 }},结果会是7。如果你想将字符串“安全”和“CMS”拼接起来,你可以使用{{ "安企" | add: "CMS" }},结果便是“安全CMS”。即使你混合使用,比如{{ 5 | add: "CMS" }},result will also be "5CMS". Its syntax is concise, with the pipe character|Following the variable, indicating that it is a 'filter' acting on the variable, which can only handle two values at a time (i.e., the variable itself and the filter parameter).

WithaddThe single responsibility of the filter is different,tag-calc.mdThe description actually refers to AnQiCMS template engineBuilt-in powerful arithmetic and logical expression evaluation capabilitiesThis is not a specific 'tag' or 'filter', but a template engine in parsing{{ ... }}The content within double curly braces can be directly understood and executed by multiple operations. It covers almost all basic mathematical operations we use in daily programming, including:

  • Addition, Subtraction, Multiplication, Division: For example{{ 10 + 20 - 5 * 2 / 10 }}.
  • Exponential operationFor example:{{ 2 ^ 3 }}(Indicates 2 to the power of 3).
  • Modulus and remainder: Used to obtain the remainder of the division operation.
  • Floating-point arithmetic and comparison:Supports precise calculation and comparison of decimal values, such as{{ 5.5 < 6.0 }}.
  • Logical expression:Can beand(AND),or(OR),notNot Boolean logic judgment, for example{{ true and (false or true) }}.
  • Comparison operators:==(equals,)!=(not equal to,)>(greater than,)<(less than,)>=(greater than or equal to,)<=(less than or equal to).
  • Inclusion relationship judgment: Throughinandnot inTo check if a value exists in a list or map.
  • Operator precedence:It follows the standard precedence rules of mathematical operators, and allows the use of parentheses()to explicitly specify the order of calculation.

In short,addThe filter is an auxiliary tool focused on binary addition/pasting, syntactically following the 'pipe character', processing only two inputs at a time. The built-in arithmetic operation capability of the template engine allows us to{{ ... }}Write complex, multi-operands, and multi-operators mathematical and logical expressions directly in the middle, with a wider range of functions and closer to expression evaluation in traditional programming languages.

In practical use, the choice of which method depends on your specific needs. If you just want to quickly add two numbers or concatenate two strings, and have a certain need for intelligent error tolerance in type conversion, thenaddThe filter is undoubtedly a more concise and convenient choice.But when you need to perform subtraction, multiplication, division, or build expressions involving multiple operations and complex logical judgments, directly utilizing the built-in arithmetic operation capabilities of the template engine will be a more powerful and flexible approach.Understanding the difference between these two can help us achieve more efficient and accurate calculation and display of various dynamic contents in the AnQiCMS template.


Common Questions (FAQ)

  1. addFilter can perform subtraction, multiplication, or division? addFilter can only perform addition operations or string concatenation.It cannot be used for subtraction, multiplication, division, and other arithmetic operations.{{ ... }}Write an expression in it.

  2. Do built-in arithmetic expressions handle data typesaddas forgiving as the filter?The built-in arithmetic expressions have stricter requirements for data types.For example, you cannot directly multiply a string that cannot be converted to a number with a number.Although AnQiCMS's Go template engine may perform type inference in some cases, it is recommended to ensure that the data types of the operands are compatible when performing complex arithmetic operations to avoid errors and unexpected results (for example, numbers multiplied or divided by numbers).

  3. Under what circumstances would I preferadda filter over built-in arithmetic expressions?when your needs are limited to adding two numbers, or concatenating two strings,addThe filter would be a more concise choice. Especially when dealing with variable types that may not always be purely numeric, but you still want to perform numerical addition where possible, otherwise perform string concatenation,addThe fault tolerance of the filter will be useful. For example, if a variable may contain numbers or an empty string, usingaddit can avoid template rendering errors caused by type mismatch.