During the template development process of AnQiCMS, we often encounter scenarios where numerical calculations or logical judgments are required. The system provides us with a variety of processing methods, includingaddFilters andtag-calc.mdThe arithmetic operation capabilities introduced are two commonly used tools. Although they both involve numerical processing, they have significant differences in functional positioning, usage methods, and complexity.
First, let's understandaddA filter. As the name suggests,addFilters are mainly used to performaddition operations or string concatenation. Its uniqueness lies in its flexibility in handling data types. When you useaddWhen filtering, it will try to intelligently add two values together: if both are numbers, it will perform mathematical addition; if either one or both are strings, it will concatenate them. What is more noteworthy is that even if automatic type conversion fails,addThe filter also has certain error-tolerance, it will ignore content that cannot be converted and continue processing the convertible parts.
For example, if you want to add the numbers 5 and 2, you can write it as{{ 5 | add: 2 }}The result will be 7. If you want to concatenate the strings '安企' and 'CMS', you can use{{ "安企" | add: "CMS" }}The result will be '安企CMS'. Even if you mix them up, like{{ 5 | add: "CMS" }}The result will also be '5CMS'. Its syntax is concise, using the pipe character|Following the variable indicates that it is a 'filter' acting on the variable, which can only handle two values (i.e., the variable itself and the filter parameter).
withaddThe single responsibility of the filter is different,tag-calc.mdThis describes the actual AnQiCMS template engineBuilt-in powerful arithmetic and logical expression evaluation capabilitiesThis is not a specific "label" or "filter", but a template engine in parsing{{ ... }}Double curly braces can directly understand and execute a variety of operations. It covers almost all the basic mathematical operations we use in daily programming, including:
- Addition, subtraction, multiplication, divisionFor example
{{ 10 + 20 - 5 * 2 / 10 }}. - Exponential operation: such as
{{ 2 ^ 3 }}(represents 2 to the power of 3). - Modulus and remainder: Used to get the remainder of the division operation.
- Floating-point arithmetic and comparisonSupports precise decimal calculation and comparison of size, such as
{{ 5.5 < 6.0 }}. - Logical expressionCan be
and(AND),or(Or),not(NOT) and other boolean logical judgments, such as{{ true and (false or true) }}. - Comparison operator:
==(equal to),!=(Not equal),>(greater than),<(Less than),>=(Greater than or equal),<=(Less than or equal). - relationship judgment: Pass
inandnot into check if a value exists in a list or mapping. - operator precedence: It follows the standard mathematical operator precedence rules and allows the use of parentheses
()Be specific about the order of calculation.
In short,addA filter is an auxiliary tool focused on binary addition/suffixing, syntactically followed by a pipe character, processing only two inputs at a time. The built-in arithmetic operation capability of the template engine allows us to{{ ... }}Complex mathematical and logical expressions with multiple operands and operators are written directly, with a wider range of functions and closer to expression evaluation in traditional programming languages.
In practice, the choice depends on your specific needs. If you just want to quickly add two numbers or concatenate two strings, and you have certain requirements 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 capabilities of the template engine will be a more powerful and flexible approach.Understanding the difference between these two can help us implement dynamic content calculations and displays more efficiently and accurately in the AnQiCMS template.
Frequently Asked Questions (FAQ)
addCan the filter perform subtraction, multiplication, or division?addThe filter can only perform addition operations or string concatenation. It cannot be used for subtraction, multiplication, division, and other arithmetic operations.If you need these operations, you should use the built-in arithmetic expression capabilities of the template engine, directly in double curly braces{{ ... }}to write an expression.Do the built-in arithmetic expressions handle data types as
addthe filter is fault-tolerant?Built-in arithmetic expressions have more strict requirements for data types.For example, you cannot directly multiply a number by a string that cannot be converted to a number.Although AnQiCMS's Go template engine may perform type inference in some cases, to avoid errors and unexpected results, it is recommended to ensure that the data types of the operands are compatible when performing complex arithmetic operations (such as multiplying and dividing numbers).In what situations would I prefer to choose
adda 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 pure numbers, but you still want to perform numeric addition where possible, otherwise perform string concatenation,addThe filter's fault tolerance will come in handy. For example, if a variable may contain a number or an empty string, useaddIt can avoid template rendering errors caused by type mismatch.