In daily website content display and function development, we often need to process data in a simple way, such as adding numbers or concatenating several segments of text.AnQi CMS is well aware of these needs and provides very practical template filters to simplify these operations.Today, let's take a deep dive into one of the especially convenient tools -addfilter.
Get to knowaddThe filter: a 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 born for this. It's like the operator in Go language.+It can intelligently handle both addition of numbers and concatenation of strings.
No matter what your data is, an integer, floating-point number or stringaddThe filter can try to perform type conversion and execute the corresponding operation.Even if some values cannot be correctly translated or are empty, it will handle them flexibly, avoiding errors in template rendering, which greatly improves 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 value you need to process|add:Then, write down the value you want to add or concatenate immediately. The basic syntax format is as follows:
{{ 原始值|add:要添加的值 }}
Let's look at some actual examples to see how it works.
1. Addition of 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 the operands are both strings,addThe filter will naturally concatenate them, just as you expect.
{# 字符串拼接 #}
{{ "安企"|add:"CMS" }} {# 输出:安企CMS #}
{{ "Hello "|add:"World!" }} {# 输出:Hello World! #}
3. Mixing numbers with strings operation
addThe most convenient aspect 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 noteworthy that if you try to add a string to a number and the string cannot be parsed as a number for the operation, then it will be concatenated as a string:
{{ 5|add:"CMS" }} {# 输出:5CMS #}
4. Handle null values (nothing)
Sometimes, you might encounter some variables with empty values (for examplenothingornil) situations.addThe filter also has good fault tolerance. When encountering null values, 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'saddA filter is a very flexible and practical tool that simplifies common numerical calculations and text concatenation needs in templates. Whether it's building dynamic content, displaying statistics, or generating personalized copy,addFilters can help you a lot, making your template code more concise and readable. Make good use of these built-in tools to greatly improve your content operation efficiency and website display effect.
Frequently Asked 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, AnQi CMS provides special arithmetic operation tags (such as intag-calc.mdThere is an introduction), you can refer to the relevant documents to learn how to use these features.
2. If I want to concatenate multiple strings or numbers, I need to use them multiple times.addfilter?
Yes,addThe filter is a binary operation that can only handle two values at a time. If you need to add or concatenate three or more values continuously, you need toadduse filters in sequence. For example:{{ value1|add:value2|add:value3 }}.
3.addThe filter is prioritized to add numbers and strings together or concatenate them?
addThe filter will prioritize numerical operations when handling 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. For example,5|add:"2"It will output.7(Because "2" can be converted to a number), and5|add:"CMS"it will output5CMSBecause "CMS" cannot be converted to a number.