AnQiCMS (AnQiCMS) provides flexible and powerful tools for content creators and website developers with its Django-like template engine syntax.When building dynamic web content, we often need to combine different types of data (such as numbers and strings) together to form the final display effect.addThe filter can be put to good use.It can not only perform the conventional addition of numbers, but also handle string concatenation, and most importantly, it shows intelligent conversion capabilities when dealing with mixed number and string concatenation and type mismatch.

addFilter: Flexible partner of numbers and strings

addA filter, as the name implies, is used to perform the 'addition' operation.In the context of AnQi CMS templates, it plays a versatile role.addFilters can try to give logical results based on the specific type of the operand.

Its basic usage is very intuitive: the variable or literal to be operated on is passed through the pipeline symbol.|pass toaddFilter, followed by a colon:with another operand as an argument. For example,{{ obj|add:obj2 }}.

Addition of numbers:WhenaddThe filter performs standard arithmetic addition when both operands it receives are numbers (or can be parsed as string representations of numbers). For example, if you write in the template:

{{ 5|add:2 }}

The final page will display7. This meets our expectations for numerical addition.

Concatenation of strings:If both operands are strings,addThe filter will perform string concatenation, joining them into a new string. For example:

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

Will be displayed on the page安企CMS.

Intelligent handling when types do not match: ExploringaddThe 'magic' of the filter

addThe true charm of the filter lies in its intelligent behavior when handling the mixing of numbers and strings.It does not report an error directly when there is a type mismatch, like some strict programming languages, but rather tries to internally convert and adapt, striving to provide a usable result.

The combination of numbers and strings:When one operand is a number and the other is a string:addThe filter will decide the behavior according to the specific situation.

  1. If the string can be interpreted as a number:In some cases, ifaddThe filter determines whether the content of the string can be safely converted to a number, it may try to perform numeric addition.However, from the actual performance and document examples of Anqi CMS, it tends to concatenate numbers after converting them to strings to avoid potential type conversion errors.

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

    Here, although"2"It looks like a number, but because"安企"It is an explicit string,addThe filter will also treat2as a string, and the final result is安企2, achieving string concatenation.

  2. If the string cannot be interpreted as a number:This is the most common case. At this point,addThe filter 'falls back' and converts the numeric operands to strings for concatenation. For example:

    {{ 5|add:"CMS" }}
    

    It will be displayed on the page:5CMSHere are the numbers.5Implicitly converted to a string"5"Then concatenated with"CMS".

When the operand isnilornothing:The document specifically mentions “When the automatic conversion fails, the content to be added will be ignored”. This rule is mainly reflected in whenaddThe filter encountersnil/nothingWhen an operand is undefined or empty. In this case, it will ignore the invalid operand and only process the valid part. For example:

{{ 5|add:nothing }}

AssumenothingIs an undefined variable or null value,addThe filter will ignore it, the final result is still5.

Actual application example:

To understand more clearly,addThe behavior of the filter, we can observe its output in different scenarios through the following example:

Template code Display result Description
{{ 5|add:2 }} 7