In the template development of Anqi CMS, we often need to process and display data.addA filter is a very practical tool that allows us to add numbers, concatenate strings. However, when it comes to boolean valuestrueandfalseWhen participating in these operations, their behavior may be curious to some new users.

Today, let's take a deeper look into it.addThe filter handles the addition/pasting operation of boolean values with numbers or strings.

addFilter: flexible handling of numbers and strings

First, let's take a look back ataddThe basic function of the filter. In the template of AnQi CMS,addThe filter is very intelligent, it can perform corresponding operations according to the type of the operands:

  • Add numbers:When both operands are numbers,addThe filter performs standard mathematical addition.
    
    {{ 5|add:2 }}  {# 输出: 7 #}
    {{ 10.5|add:3.2 }} {# 输出: 13.7 #}
    
  • String concatenation:When the operands include strings,addThe filter will concatenate them.
    
    {{ "安企"|add:"CMS" }} {# 输出: 安企CMS #}
    
  • Mixed type processing:Even if the operands are mixed types,addThe filter will also attempt type conversion to complete the operation. For example, when a number is concatenated with a string, the number will be converted to a string.
    
    {{ "安企"|add:"2" }} {# 输出: 安企2 #}
    {{ 5|add:"CMS" }}   {# 输出: 5CMS #}
    
    It is worth noting that if type conversion fails,addThe filter will typically 'ignore' content that cannot be converted, processing only the successfully converted parts. For example,{{ 5|add:nothing }}It will output5becausenothingthat cannot be added or concatenated effectively.

Boolean valuetrueandfalseof the 'Metamorphosis'

Now, let's focus on Boolean valuestrueandfalseIn the template environment of AnQi CMS, when Boolean values participate inaddIn the operation of the filter, they will undergo a kind of intuitive 'transformation' based on the context:

  1. When added to numbers:

    • truethey are considered as numbers1.
    • falsethey are considered as numbers0This is consistent with the conventions of many programming languages and template engines, the purpose of which is to facilitate simple conditional counting or mathematical operations.

    Let's look at a few examples:

    {{ true|add:5 }}    {# 输出: 6 (因为 true 被当作 1) #}
    {{ false|add:10 }}  {# 输出: 10 (因为 false 被当作 0) #}
    {{ 100|add:true }}  {# 输出: 101 (同样,true 被当作 1) #}
    {{ 200|add:false }} {# 输出: 200 (同样,false 被当作 0) #}
    
  2. When concatenated with a string:

    • trueit will be converted to a string"true".
    • falseit will be converted to a string"false". This conversion is also to maintain the intuitiveness of the operation, ensuring that boolean values can be integrated into strings in their text form.

    For example:

    {{ "AnQiCMS 版本: "|add:true }}   {# 输出: AnQiCMS 版本: true #}
    {{ "网站状态: "|add:false }}      {# 输出: 网站状态: false #}
    {{ true|add:" 已启用" }}         {# 输出: true 已启用 #}
    {{ false|add:" 未启用" }}        {# 输出: false 未启用 #}
    

Considerations in practical application

UnderstandingaddThe mechanism for filtering boolean values helps us to build dynamic content more flexibly.For example, to display the enabled state of a feature in a table, or to perform simple numerical summation based on the value of a boolean variable.

AlthoughaddFilter provides this flexible automatic type conversion, but we still suggest:

  • Keep it clear:If you need to perform complex logical judgments or numerical conversions, it is best to useiflabeling or more explicit variable assignment to preprocess boolean values, rather than over-reliance onaddthe implicit conversion of filters.
  • Testing is the king:Be sure to carry out sufficient testing before releasing any complex template code, to ensureaddThe behavior of the filter meets expectations under various combinations of data types.

In short, the Anqi CMS isaddThe filter follows a practical and user-friendly principle when handling boolean values: when added to numbers, booleans transform into0or1; and when concatenated with strings, they act as"true"or"false"The form appears. Mastering these 'little secrets' will make your security CMS template development more intuitive.


Common Questions (FAQ)

1.addCan the filter add two boolean values together? For example{{ true|add:true }}What will be output?答:WhenaddThe filter tries to add two boolean values together. So,trueWill become1,falseWill become0.{{ true|add:true }}It will output2(i.e.,)1 + 1) and,{{ true|add:false }}It will output1(i.e.,)1 + 0).

2.addThe filter encounters a null value (such asnilornothingWhat would happen in this case?Answer: As described in the article,addThe filter will “ignore” the empty value when it encounters an empty value that cannot be effectively converted to a number or string for addition/pasting, and will only process valid operands. For example,{{ 5|add:nothing }}It will output5,{{ "文本"|add:nil }}It will output"文本".

3.addFilter is it only applicable to variables within the template, or is it also applicable to literal values written directly?Answer:addFilter can be applied to both variables within the template and to literal values (such as)5/"字符串"/trueauto").Just as long as the operands are of valid types, the filter can work normally. For example,{{ 10|add:20 }}and{% set num = 10 %}{{ num|add:20 }}will be output normally.30.