How does the `add` filter handle the addition/pasting operation with boolean values `true` and `false` as well as numbers or strings?

Calendar 👁️ 59

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

Today, let's delve deeper into it.addHow the filter handles the addition/pasting operation between boolean values and numbers or strings.

addFilter: Flexible handling of numbers and strings.

First, let's reviewaddThe basic function of the filter. In Anqi CMS templates,addThe filter is very intelligent, it can perform corresponding operations according to the type of operands:

  • Add numbers:When all operands are numbers,addThe filter performs standard mathematical addition.
    
    {{ 5|add:2 }}  {# 输出: 7 #}
    {{ 10.5|add:3.2 }} {# 输出: 13.7 #}
    
  • string concatenation:When the operand contains a string,addthe filter will concatenate them.
    
    {{ "安企"|add:"CMS" }} {# 输出: 安企CMS #}
    
  • Mixed type handling:Even if the operand is a mixed type,addThe filter also tries to perform type conversion to complete the operation. For example, when a number is concatenated with a string, the number is converted to a string.
    
    {{ "安企"|add:"2" }} {# 输出: 安企2 #}
    {{ 5|add:"CMS" }}   {# 输出: 5CMS #}
    
    It is worth noting that if the type conversion fails,addFilters usually 'ignore' content that cannot be converted, only processing the parts that are successfully converted. For example,{{ 5|add:nothing }}It will output.5becausenothingcannot be added or concatenated effectively.

Booleantrueandfalseof 'Metamorphosis'

Now, let's focus on boolean valuestrueandfalse. In the Anqi CMS template environment, when boolean values are involved inaddWhen performing filter operations, they will undergo a直观 transformation based on the context:

  1. When added to numbers:

    • truewill be treated as numbers1.
    • falsewill be treated as numbers0This is consistent with the practice of many programming languages and template engines, and the purpose is to facilitate simple conditional counting or mathematical operations.

    Let's see some 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:

    • trueis converted to a string"true".
    • falseis 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 applications.

UnderstandingaddThe filter handles boolean values, which can help us build dynamic content more flexibly.For example, displaying the enabled status of a feature in a table or performing simple numerical summation based on the value of a boolean variable.

AlthoughaddThe filter provides this flexible automatic type conversion, but we still recommend:

  • Keep it clear:If you need to perform complex logical judgments or numerical conversions, it is best to useiftags or clearer variable assignments to preprocess boolean values rather than over-rely onaddimplicit conversion of filters.
  • Test is for the king:Before releasing any complex template code, it is essential to perform thorough testing to ensureaddThe behavior of the filter meets expectations under various data type combinations.

In short, the Anqi CMS'saddThe filter follows a practical and user-friendly principle when dealing with boolean values: when added to numbers, booleans become0or1; and when concatenated with strings, they act as"true"or"false"It appears in the form. Mastering these "secrets" will make your Anqi CMS template development more skillful.


Frequently Asked Questions (FAQ)

1.addCan the filter add two boolean values together? For example{{ true|add:true }}What will be output?Answer: WhenaddThe filter tries to add two boolean values together, and it will first convert them to numbers. So,trueWill become1,falseWill become0Thus,{{ true|add:true }}It will output.2(that is,}1 + 1) while,{{ true|add:false }}It will output.1(that is,}1 + 0).

2.addWhat will the filter do when it encounters a null value (such as)nilornothing)?Answer: As mentioned in the article, whenaddThe filter will 'ignore' the null values that cannot be effectively converted to numbers or strings for addition or concatenation and will only process the valid operands. For example,{{ 5|add:nothing }}It will output.5,{{ "文本"|add:nil }}It will output."文本".

3.addThe filter is only applicable to variables in the template, or is it also applicable to literal values written directly?Answer:addThe filter can act on both variables in the template and literal values (such as5/"字符串"/trueAs long as the operand is a valid type, the filter will work normally. For example,{{ 10|add:20 }}and{% set num = 10 %}{{ num|add:20 }}will be output normally.30.

Related articles

How to dynamically generate JavaScript code snippets in AnQiCMS templates and use the `add` filter to concatenate variables?

In website operations, we often need to make the page more intelligent and interactive.AnQiCMS as an efficient and flexible content management system not only provides powerful content organization and display capabilities, but also allows us to ingeniously integrate dynamic logic in templates, such as by generating JavaScript code snippets to achieve more complex functions.AnQiCMS's template engine syntax is similar to Django, which provides great convenience for us to dynamically process data.It is developed based on the Go language, runs fast

2025-11-07

Can the `add` filter be used to generate dynamic HTML attributes, such as the value of `data-` attributes?

In modern web development, dynamically generating HTML attributes, especially `data-` attributes, has become a common requirement.These properties not only provide additional data support for front-end JavaScript, but also enhance the presentation of elements without affecting the page semantics.AnQiCMS as a content management system that focuses on flexibility, its powerful template engine naturally also provides the possibility to meet this need.So, can the `add` filter in the AnQiCMS template handle the task of dynamically generating `data-` attribute values?

2025-11-07

How to use the `add` filter to dynamically add additional descriptions to the field in the AnQiCMS backend user group management (`userGroupDetail`)?

In the AnQiCMS management background, the user group management (`userGroupDetail`) module is the basis for us to divide permissions, set levels for different user groups, and even configure VIP services.Generally, each user group has its core attributes, such as name, level, price, etc., which are directly displayed on the back-end interface for convenient daily management.However, sometimes we hope that these fields are not just cold data, but can also have some additional, more descriptive information. For example

2025-11-07

How to pass the `add` filter as a parameter to the `macro` macro function and perform internal text processing?

In AnQi CMS daily content operation, we pursue efficient and flexible management and display of website content.The template system is the core of achieving this goal, among which the `macro` macro function and the `add` filter are two very practical tools.Understand how they work together, especially how to use the `add` filter for internal text processing in macro functions, which can greatly enhance the reusability and dynamism of our templates.The AnQi CMS template system uses syntax similar to Django, which allows us familiar with Web development to quickly get started.

2025-11-07

How to use the `add` filter to dynamically add a uniform prefix or suffix to the single page titles in the `pageList`?

In Anqi CMS, content management is not limited to the backend's add, delete, modify, and query, but also manifests in the flexibility and diversity of front-end display.In the face of common single-page websites (such as "About Us", "Contact Us", etc.), sometimes we hope that they can be unified with specific identifiers when displayed in lists or navigation, such as If you modify the background title one by one, it is not only inefficient but also lacks uniformity and maintainability.At this time, utilizing the powerful template engine function of AnqiCMS, with the clever filter

2025-11-07

How to combine other filters with the `add` filter to ensure safe concatenation and prevent XSS attacks when processing user input?

In AnQi CMS, managing website content involves entering a variety of information regularly.Whether it is the main text of the article, user comments, or form submissions, this user-generated content injects vitality into the website while also bringing potential security risks, the most common and severe of which is XSS (Cross-site Scripting attack).This article will discuss how the `add` filter works in processing user input content, how it协同acts with other security filters to build a strong defense line, effectively preventing XSS attacks.

2025-11-07

How to dynamically generate a link to the comment area based on `item.Id` in `archiveList`?

In AnQi CMS, in order to enhance the user experience of the website, we often hope that users can quickly find the specific content on the page.For example, when you click on the "View Comments" link next to an article title on a document list page, you can directly jump to the comments section of the article detail page.This not only saves users time, but also makes interactions more direct. Today, let's discuss how to dynamically generate anchor links to comment sections in the `archiveList` loop based on the unique identifier `item.Id`.

2025-11-07

`add` filter combined with `default` filter to set default values for variables that may be empty before concatenation?

In the daily operation of AnQi CMS, we often need to concatenate different field content to form a complete information, such as generating a complete address or combining an attractive product title.However, a common challenge with dynamic content is that some variables may be empty.If concatenated directly, the blank content not only affects the appearance but may also lead to missing information or disordered format.Fortunately, AnQiCMS's powerful Django template engine grammar provides us with a flexible solution

2025-11-07