In the daily display of website content and functional development, we often need to perform some simple data processing, such as adding numbers together, or combining several paragraphs of text.AutoCMS knows these needs and provides very practical template filters to simplify these operations.addFilter.
KnowaddFilter: A powerful tool for adding numbers and concatenating strings
Imagine that you need to sum two numbers in a template, or you need to merge the article title and subtitle for display. In Anqi CMS,addThe filter is designed for this. It is like the Go language's+operator, which can intelligently handle both addition of numbers and concatenation of strings.
No matter whether your data is an integer, floating-point number, or string,addThe filter can attempt to perform type conversion and execute the corresponding operation.Even if some values cannot be correctly converted or are empty, it will handle them flexibly to avoid errors in template rendering, which greatly enhances the robustness and convenience of template writing.
addHow to use the filter
Useaddthe filter is very intuitive, you just need to add it after the variable or literal you want to process,|add:Then, write down the value you want to add or concatenate. The basic syntax format is as follows:
{{ 原始值|add:要添加的值 }}
Let's see how it works with some practical examples.
1. Adding pure numbers
When you need to add two numbers (whether they are integers or floating-point numbers),addthe filter will give the expected mathematical result.
{# 简单数字相加 #}
{{ 5|add:2 }} {# 输出:7 #}
{{ 10|add:40 }} {# 输出:50 #}
{{ 3.5|add:1.2 }} {# 输出:4.7 #}
2. Pure string concatenation
if both operands are strings,addThe filter will naturally concatenate them, just as you expect.
{# 字符串拼接 #}
{{ "安企"|add:"CMS" }} {# 输出:安企CMS #}
{{ "Hello "|add:"World!" }} {# 输出:Hello World! #}
3. Mixed operations with numbers and strings
addThe most convenient feature of the filter is that it can handle mixed operations of numbers and strings. In this case, it is usually necessary to convert numbers to strings for concatenation.
{# 数字与字符串混合 #}
{{ "订单号:"|add:12345 }} {# 输出:订单号:12345 #}
{{ 2023|add:" 年" }} {# 输出:2023 年 #}
{{ "文章总数:"|add:100|add:" 篇" }} {# 输出:文章总数:100 篇 (可以连续使用) #}
It is worth noting that if you try to add a string to a number and the string cannot be effectively parsed as a number for calculation, it will be concatenated as a string:
{{ 5|add:"CMS" }} {# 输出:5CMS #}
4. Handling Null Values (nothing)
Sometimes, you might encounter some variables with null values (for example)nothingornil) situation.addThe filter also has good fault tolerance.When encountering a null value, it usually ignores the null value and returns the value of another non-null operand, or treats it as an empty string in string concatenation.
{% set nothing_value = nothing %}
{{ 5|add:nothing_value }} {# 输出:5 (nothing_value无法有效参与数字运算) #}
{{ "前缀"|add:nothing_value }} {# 输出:前缀 (nothing_value被视为空字符串拼接) #}
Summary
As you can see, the Anqi CMS'saddFilter is a very flexible and practical tool that simplifies common numeric calculations and text concatenation requirements in templates. Whether it is to build dynamic content, display statistical information, or generate personalized copy,addFilters can lend you a helping hand, making your template code more concise and easier to read. Make good use of these built-in tools, and you can significantly enhance your content operation efficiency and website display effect.
Common Questions (FAQ)
1.addCan the filter perform subtraction, multiplication, or division operations?
addThe filter is specifically used for adding numbers or concatenating strings. If you need to perform other arithmetic operations, such as subtraction, multiplication, or division, SafeCMS provides dedicated arithmetic operation tags (such as intag-calc.mdThere is an introduction), you can refer to the relevant documentation to learn how to use these features.
2. If I want to concatenate multiple strings or numbers, I need to use them multiple timesaddFilter?
Yes,addThe filter is a binary operation, which can only process two values at a time. If you need to add or concatenate three or more values continuously, you need toaddfilter in a continuous manner. For example:{{ value1|add:value2|add:value3 }}.
3.addFilter is preferred to add numbers and strings together or concatenate them first?
addThe filter will try to perform numerical operations first when processing mixed types.Only when one of the operands is explicitly a string or cannot be effectively converted to a number, will it convert all operands to strings for concatenation.5|add:"2"It will output7(Because "2" can be converted to a number), and5|add:"CMS"it will output5CMS(Because "CMS" cannot be converted to a number).