In AnQiCMS template development, we often need to combine or calculate different data, especially when displaying dynamic content. Wherein,addFilter is a very practical tool, it allows us to flexibly add values of numeric and string types.Understand its working principle can help us build templates more efficiently and accurately.

addFilter: Flexible addition of numbers and strings

addThe filter plays the role of 'addition' in the template engine of AnQiCMS, but its capabilities are not limited to simple numerical addition.It is designed to be quite intelligent, capable of handling mixed addition scenarios involving integers, floating-point numbers, and strings.The core idea is to perform meaningful addition or concatenation as much as possible to simplify the complexity of template writing.

Basic usage addThe filter syntax is very intuitive, it accepts two operands:

{{ obj|add:obj2 }}

Among them,objIs the variable or literal on the left side of the filter,obj2It is the variable or literal after the filter colon, indicating the value to be added or concatenated.

Next, let's see its specific performance under different data type combinations:

1. Numbers added to numbers: Pure arithmetic operations

WhenaddWhen both operands on the filter are numeric types (integer or floating-point numbers), it performs standard arithmetic addition.

Example: Adding integers

{{ 5|add:2 }}

Show results: 7

Example: Adding floating-point numbersAssuming we have a floating-point variableprice = 10.5:

{{ price|add:3.2 }}

Show results: 13.7

2. Adding strings to strings: content concatenation

IfaddThe operands on both sides of the filter are string types, and it simply concatenates them to form a new string.

Example: String concatenation

{{ "安企"|add:"CMS" }}

Show results: 安企CMS

3. Mixed addition of numbers and strings: Smart conversion and concatenation

This isaddThe most intelligent place of the filter. When numbers meet strings,addThe filter tries to convert numbers to strings and then perform string concatenation.

Example: Number followed by a string

{{ 5|add:"CMS" }}

Show results: 5CMSHere, numbers5are implicitly converted to strings"5"then concatenated with"CMS"and concatenated.

Example: String followed by number

{{ "安企"|add:"2" }}

Show results: 安企2Similarly, number2Is converted to string"2"And with"安企"and concatenated.

4. Handling null values and non-numeric content: Graceful ignore mechanism

AnQiCMSaddFilter exhibits its robustness when encountering invalid number conversion or an empty operand: itnothingornilwillignorebe unable to convert content effectively or be empty.

Example: adding a number with a null valueAssumenothingRepresents an undefined or empty variable:

{{ 5|add:nothing }}

Show results: 5In this case,nothingis considered0, or more accurately, it is ignored, resulting in the number itself.

Example: Adding a number and a non-numeric string

{{ 5|add:"非数字" }}

Show results: 5非数字Number5to strings"5", and"非数字"Concatenation. It will not"非数字"report an error because it cannot be converted to a number, but it will concatenate as a string.

Example: adding an empty value to a string

{{ nothing|add:"CMS" }}

Show results: CMS nothingis ignored here, and the result is just a string"CMS".

It can be seen from the above examples,addThe filter can intelligently perform type conversion and content processing when dealing with mixed type addition, which greatly enhances the flexibility and fault tolerance of the AnQiCMS template, allowing developers to handle dynamic data more effortlessly. Whether you need to calculate the total price, concatenate paths, or dynamically generate copywriting,addFilters can provide simple and efficient solutions.


Common Questions (FAQ)

1.addFilter can perform subtraction, multiplication, or division operations? addThe filter is used to perform addition and string concatenation of numbers. If you need to perform more complex arithmetic operations such as subtraction, multiplication, or division, the AnQiCMS template engine providescalcArithmetic operation tag, you can directly write mathematical expressions in it, for example{{ 10 - 5 * 2 }}.

2. How to ensure that my variable is a valid numeric type before addition to avoid unexpected string concatenation?AlthoughaddThe filter has fault tolerance in handling mixed types, but if your intention is to add pure numbers, it is recommended to use it before the operationintegerorfloatThe filter explicitly converts variable types. For example,{{ item.price|float|add:item.tax|float }}Ensure both are converted to floating-point numbers before addition to avoid10.5+2.0becomes"10.52.0"the situation.

3. If I need to concatenate all strings in an array,addIs a filter a **choice?For connecting a single string or two variables,addThe filter is very convenient. But if you need to concatenate multiple string elements from an array (list),joinThe filter is usually a more elegant and efficient choice.joinThe filter allows you to specify a delimiter to concatenate all elements in an array into a single string, for example{{ tags|join:", " }}.