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 that allows us to flexibly add numeric and string values.Understand its working principle, which can help us build template logic 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 addition of numbers.It is quite intelligent and can handle mixed addition scenarios between integers, floating-point numbers, and strings.The core idea is to perform meaningful addition or concatenation as much as possible, thereby simplifying the complexity of template writing.

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

{{ obj|add:obj2 }}

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

Next, let's take a look at how it performs under different data type combinations:

1. Adding numbers: pure arithmetic operation

WhenaddThe operands on both sides of the filter are of numeric type (integer or floating point) when it performs standard arithmetic addition.

Example: Integer addition

{{ 5|add:2 }}

Display result: 7

Example: Floating-point additionWe have a floating-point variableprice = 10.5:

{{ price|add:3.2 }}

Display result: 13.7

2. String concatenation: content splicing

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

Example: String concatenation

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

Display result: 安企CMS

3. Add mixed numbers and strings: Smart conversion and concatenation

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

Example: Numbers followed by strings

{{ 5|add:"CMS" }}

Display result: 5CMSHere, the number5is implicitly converted to a string"5"Then concatenated with"CMS".

Example: a string followed by a number

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

Display result: 安企2Similarly, a number2Converted to a string"2"And with"安企".

4. Handling empty values and non-numeric content: an elegant ignore mechanism

AnQiCMS'addThe filter demonstrates its robustness when encountering invalid number conversion or an empty operand (nothingornil) by displaying it:to ignoreUnable to convert or content is empty.

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

{{ 5|add:nothing }}

Display result: 5In this case,nothingIs considered0, or more accurately, it is ignored, resulting in the number itself.

Example: Adding a number to a non-numeric string

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

Display result: 5非数字Number5Convert to string"5"and"非数字"Concatenation. It will not"非数字"result in an error when

Example: null value added to

{{ nothing|add:"CMS" }}

Display result: CMS nothingbe ignored here, the result is only a string"CMS".

By the above examples, it can be seen that,addThe filter can intelligently perform type conversion and content processing when adding mixed types, which greatly enhances the flexibility and fault tolerance of the AnQiCMS template, allowing developers to handle dynamic data more conveniently. Whether you need to calculate the total price, concatenate paths, or dynamically generate text,addFilters can provide simple and efficient solutions.


Frequently Asked Questions (FAQ)

1.addCan the filter 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 number type before addition to avoid unexpected string concatenation?AlthoughaddThe filter is fault-tolerant 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 }}Make sure both are converted to floating-point numbers first before adding to avoid10.5+2.0becomes"10.52.0"the situation.

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