In the template development of AnQi CMS, Filters are an important tool for processing and transforming data. Among them,addThe filter, due to its unique behavior in handling numbers and strings, often causes users to ponder its processing logic. Whether whenaddThe filter encounters different types of data (such as numbers and strings) when they are added together. How does it work? This article will deeply analyze this mechanism.


addThe core function and basic usage of the filter

addThe main function of the filter is to perform a 'sum' operation on two values. This 'sum' is not just a mathematical addition, it also includes string concatenation. In terms of usage, it follows{{ obj|add:obj2 }}This concise format, whereobjis the original data,obj2Is the data you want to "add". Its design goal is to provide a flexible way to handle the "accumulation" needs of mixed data types.

In-depth analysis:addFilter handles the logic for different data types.

addWhen processing different types of data, the filter will intelligently choose to perform mathematical addition or string concatenation based on the specific situation of the operands.

  1. Pure numbers addition: Standard mathematical operationWhenobjandobj2Are all pure numbers (whether integers or floating-point numbers)addThe filter performs standard mathematical addition operations and returns their sum. This is consistent with our daily mathematical calculations.

    • For example:{{ 5|add:2 }}The result is7.
    • For example:{{ 5|add:40 }}The result is47.
  2. Pure string concatenation: directly combineIfobjandobj2They are all of string type,addThe filter will simply concatenate them in order to form a new string.

    • For example:{{ "安企"|add:"CMS" }}The result is安企CMS.
  3. Mixture of numbers and strings: smart conversion and decision makingThis isaddThe filter is the most 'smart' and also the most attention-worthy place. When the types of operands are mixed, it will make a 'judgment':

    • Try numeric conversion first:IfobjIt is a number, andobj2It looks like a string that resembles a number (e.g.,"2")addThe filter will first try to convert the stringobj2to a number, then perform mathematical addition.
      • For example:{{ 5|add:"2" }}The result is7.
    • Degrades to string concatenation:If the stringobj2Cannot be recognized as a number (for example, containing letters or other non-numeric characters), or if itobjis of string type,addThe filter will convert all operands to strings and then perform string concatenation.
      • For example:{{ 5|add:"CMS" }}number,5it will be converted to a string"5"then concatenated with"CMS"concatenation, the result is5CMS.
      • For example:{{ "安企"|add:"2" }}, even"2"can be converted to a number, but due to the first operand"安企"Is a string, the entire operation will be directly performed string concatenation, the result is安企2.
  4. Interaction with empty values (nil/nothing): treated as zero or ignoredWhenaddThe filter encountersnilornothing(May represent a non-existent or empty value in the template) Such an empty value usually behaves as 'ignore' or is treated as 'zero value' to ensure the smooth operation of calculations.

    • For example:{{ 5|add:nothing }}The result is5In this case,nothing被视为不影响数字运算的存在(similar to addition0).

Actual case demonstration

To better understandaddfilter's behavior, we demonstrate its processing logic through several specific examples:

{# 示例一:纯数字相加 #}
{{ 5|add:2 }}        {# 输出:7 (标准的数字加法) #}

{# 示例二:数字与可转换字符串相加 #}
{{ 5|add:"40" }}     {# 输出:45 (字符串"40"被转换为数字40进行加法) #}

{# 示例三:数字与不可转换字符串相加 #}
{{ 5|add:"CMS" }}    {# 输出:5CMS (数字5被转换为字符串"5"后与"CMS"拼接) #}

{# 示例四:纯字符串拼接 #}
{{ "安企"|add:"CMS" }} {# 输出:安企CMS (字符串直接拼接) #}

{# 示例五:字符串与可转换字符串相加 #}
{{ "安企"|add:"2" }}    {# 输出:安企2 (即使"2"可转换为数字,但因第一个操作数是字符串,仍进行拼接) #}

{# 示例六:数字与空值相加 #}
{{ 5|add:nothing }}  {# 输出:5 (nothing被视为0或忽略) #}

In the template,addThe filter can help us combine dynamic content very conveniently. For example, if there is a product objectproduct,IdandVersionfields, you can generate a product code like this:{{ "PROD-"|add:product.Id|add:"-V"|add:product.Version }}

Use suggestions

addThe filter seamlessly switches between numeric operations and string concatenation through its intelligent type judgment, greatly simplifying template writing. However, during use, to ensure the clarity of the template and avoid potential type conversion ambiguities, especially when performing important numerical calculations, we recommend:

  • Specify data type:Before performing complex numerical calculations, if the operands may come from user input or other dynamic sources and their type is uncertain, consider usingintegerorfloatExplicit type conversion with filters is used to ensure the execution of the desired mathematical operations.
  • Pay attention to the order of operations:When involving multipleaddPlease pay attention to the type of operands during the operation, especially the type of the first operand, as it will affect the default behavior of subsequent operations, whether it is a numeric calculation or a string concatenation.

Summary

addThe filter is a powerful and flexible tool in the AnQi CMS template, which makes appropriate choices between summing numbers and concatenating strings through 'intelligent' type judgment.Understand its processing logic, which can help you build dynamic web content more efficiently and accurately, avoiding confusion caused by implicit type conversion.Mastering this skill will take your security CMS template development to a new level.


Common Questions and Answers (FAQ)

  1. addFilter can handle negative numbers and decimals?