The `add` filter performs mathematical calculations in the AnQiCMS template, what is the difference from the arithmetic operation tag in `tag-calc.md`?

Calendar 👁️ 53

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 beand(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: Passinandnot 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)

  1. 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.

  2. Do the built-in arithmetic expressions handle data types asaddthe 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).

  3. In what situations would I prefer to chooseadda 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.

Related articles

How to use the `add` filter to dynamically add a brand name or fixed suffix to the `Title` of the page `TDK`?

In website content operation, the page's "Title", "Keywords", and "Description", which is what we often call TDK, plays a crucial role in search engine optimization (SEO).A well-structured and brand-recognizable page title that not only helps search engines better understand the page content but also attracts users' attention in search results.AnQiCMS provides a flexible template engine, allowing us to finely control these key information

2025-11-07

When handling floating-point number addition with the `add` filter, should special attention be paid to its calculation accuracy?

When using AnQi CMS for website content management, template tags and filters are tools we often use, greatly enhancing the flexibility of content presentation.Among them, the `add` filter allows us to perform addition operations of numbers or strings in the template, which is very convenient in many scenarios.However, when it comes to adding floating-point numbers (which are decimal numbers), some users may wonder: Does the `add` filter need to pay special attention to its calculation accuracy when handling such calculations?

2025-11-07

How to concatenate the area code and phone number in the contact (`contact` tag) using the `add` filter?

It is crucial to clearly and effectively display contact information when operating your website.AnQiCMS (AnQiCMS) with its flexible template engine and powerful tag system, allows you to easily achieve various complex display needs.Today, let's talk about a common but somewhat tricky requirement: how to concatenate the area code and phone number in the contact information using the `add` filter to make your phone number clear at a glance.Why do you need to concatenate the area code and phone number?

2025-11-07

Can the `add` filter be used to concatenate the website name and footer copyright information obtained from the `system` tag?

In website operation, we often need to finely control the display of page content, especially those global information that needs to be repeatedly displayed throughout the website, such as the website name, footer copyright, and so on.AnQiCMS (AnQiCMS) provides powerful template tags and filters to help us flexibly handle these requirements.Today, let's talk about a very practical scenario: Can the `add` filter of AnQi CMS be used to concatenate the website name and footer copyright information obtained from the `system` tag?The answer is affirmative, and this method is both intuitive and efficient.

2025-11-07

How to use the `add` filter in a loop to implement alternating row colors or dynamically concatenate different CSS class names?

In modern web design, the visual presentation of list content has an important impact on user experience.Whether it is an article list, product display, or comment section, by giving list items different styles, such as alternating row colors or applying different class names based on specific conditions, it can significantly improve the readability and aesthetics of the page.AnQiCMS is a powerful template engine that provides flexible tools to meet these dynamic style requirements, where the `for` loop's `cycle` tag and `add` filter are our reliable assistants.###

2025-11-07

What is the final concatenation result when one operand of the `add` filter is a number and the other is a string that cannot be converted to a number?

AnQi CMS is an enterprise-level content management system developed based on the Go language, dedicated to providing users with efficient and customizable content management solutions.In daily content operation and template development, we often use various template tags and filters to process data, where the `add` filter is a very practical feature that can help us conveniently perform numerical addition or string concatenation.However, when operands of mixed types are involved, the logic of their behavior becomes particularly important.

2025-11-07

How to use the `add` filter to dynamically add 'Published on:' before the blog post's publish time?

When operating a blog, we often hope that the publication time of the articles can be presented in a more intuitive and brand style compliant manner.For example, adding 'Published on:' before the date can make it clear to the reader that this is a published article.AnQiCMS (AnQiCMS) with its flexible template engine makes such customization very simple.Today, let's discuss how to cleverly use the built-in `add` filter of AnQiCMS to dynamically add the phrase 'Published on:' before the publication time of blog posts.

2025-11-07

Does the `add` filter support chained calls, such as `{{ var1|add:var2|add:var3 }}` to concatenate multiple variables?

When developing templates in Anq CMS, we often encounter situations where we need to combine, concatenate, or sum multiple variables.Among them, the `add` filter is a very practical tool that allows us to perform addition operations on numbers or concatenate strings.However, some users may be curious whether the `add` filter supports a chain-like call like `{{ var1|add:var2|add:var3 }}` to concatenate multiple variables at once?Familiarize yourself with the template syntax of AnQi CMS after deep study

2025-11-07