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 operations and template development, we often use various template tags and filters to process data, among whichaddThe filter is a very practical feature that can help us easily perform operations such as adding numbers or concatenating strings.However, when operands of mixed types are involved, the logic of their behavior becomes particularly important.

Today, let's delve into a common scenario: whenaddWhat will be the result of concatenation when one operand of the filter is a number and the other is a string that cannot be converted to a number?Understanding this can help us avoid unnecessary confusion when developing templates and write more robust code.

Get to knowaddFilter

In the AnQiCMS template system,addA filter is a very flexible tool that can perform addition operations on two numbers or concatenate two strings.

  • Add numbers:When both operands are numbers or can be automatically identified as numeric strings,addthe filter will perform mathematical addition.{{ 5|add:2 }}The result is7.
  • string concatenation:When both operands are strings,addthe filter will simply concatenate them. For example:{{ "安企"|add:"CMS" }}The result is安企CMS.

Then, when these two types are mixed, especially when one operand is a number and the other is a string that cannot be converted to a number,addhow will the filter behave?

Core scenario analysis: the combination of numbers and non-numeric strings

Now, let's focus on the core issue of the article: whenaddA filter operand is a number and the other operand is a string containing non-numeric characters, for example"CMS"What is the final concatenation result?

In this specific case,addThe filter will first try to convert the operands to the same type for calculation. It will prioritize numeric operations. However, when it finds one of the string operandsCannot be fully parsed as a numberWhen this happens, it will adopt a 'second best' strategy: converting the numeric operandsImplicitly converted to a stringThen, concatenate these two strings.

In other words, it performs string concatenation rather than mathematical addition.

Example demonstration:

Suppose we have a number5and a non-numeric string"CMS":

{{ 5|add:"CMS" }}

You might expect an error or just numbers5But the actual output is:5CMS.

This is a flexible handling method designed in the AnQiCMS template engine, aiming to avoid throwing errors during mixed type operations, but rather to try to provide results in a reasonable way.It prioritizes mathematical operations, but when it encounters a string that cannot be converted to a number, it falls back to string concatenation.

Overview of the performance of different operand types

To understand more comprehensivelyaddFilter, let's summarize the behavior under different operand types:

  • Number + Number (or a string that can be converted to a number)This is the most intuitive case,addPerform addition in mathematics. For example:{{ 5|add:2 }}The result is7. For example:{{ 5|add:"40" }}The result is45.
  • Number + null/undefined(nothing/nil)In this case, null or undefined values will be ignored,addIt will return the number itself.{{ 5|add:nothing }}The result is5.
  • The number + string that cannot be converted to a number.As we discussed above, numbers will be converted to strings and then concatenated with another string. For example:{{ 5|add:"CMS" }}The result is5CMS.
  • String + String addThe filter will perform a simple string concatenation. For example:{{ "安企"|add:"CMS" }}The result is安企CMS.
  • String + number (or a string that can be converted to a number)If the first operand is a string, then regardless of whether the second operand is a number or a string that can be converted to a number,addThe filter will treat them all as strings to concatenate. For example:{{ "安企"|add:2 }}The result is安企2. For example:{{ "安企"|add:"2" }}The result is安企2.

Practical suggestions

UnderstoodaddWith these behaviors of the filter, we can be more skillful in actual template development:

  1. Specify data type:Perform in templateaddBefore performing operations, it is best to anticipate the variable types involved. If your intention is numerical computation, try to ensure that all operands are pure numbers