In AnQi CMS template design, we often need to perform some simple data processing, such as adding several numbers to display the total, or combining different text segments into a complete sentence. At this point,addThe filter is particularly convenient, acting as a universal connector that can easily handle everything from numerical addition operations to text concatenation, making your template more flexible and dynamic.

addFilter: The smart connector in the template

As the name implies,addThe main function of the filter is to add two values together.Its intelligence lies in the fact that it can not only handle pure numerical addition, but also cleverly concatenate numbers with strings, and strings with strings.This is due to the internal processing mechanism of the AnQiCMS template engine, which tries to intelligently perform type conversion to achieve the expected "addition" effect.

While usingaddWhen filtering, the syntax is very intuitive: you just need to place the first value after the pipe symbol|on the left, and theaddThe filter and its second operand are placed on the right, like this:{{ 变量A|add:变量B }}.

It is worth noting that even if you pass toaddThe filter handles different types of values, such as a number and a string, and AnQiCMS template engine will also try to convert and process them.If it cannot perform an effective numeric addition or string concatenation, it will silently ignore the parts that cannot be converted, ensuring the normal rendering of the template without causing the page to report an error.This fault-tolerant mechanism allows template developers to feel more at ease when combining content.

Actual application: adding numbers and concatenating strings

Let's look at some specific examples to see.addHow the filter works in your AnQiCMS template.

Flexible addition of numbers

When you need to calculate and display the sum of two or more numbers,addThe filter is an ideal choice. For example, you might need to add the unit price of the goods and shipping costs, or calculate some statistical data.

Assuming you have a variablepriceStore the price of the goods as100Another variableshipping_feeStore shipping costs as20The total amount you want to display:

{% set price = 100 %}
{% set shipping_fee = 20 %}

<p>商品总价:{{ price|add:shipping_fee }} 元</p>

This code will output:商品总价:120 元.

This applies to integers as well as floating-point numbers, ensuring the accuracy of calculations.

Seamless concatenation of strings

In addition to adding numbers,addThe filter performs well in processing string concatenation.When you need to combine different text segments, dynamic content, or preset words together, it can provide a very smooth experience.

For example, do you want to concatenate your CMS system name 'Anqi' with 'CMS':

<p>我们的系统是:{{ "安企"|add:"CMS" }}</p>

This code will output:我们的系统是:安企CMS.

If you have a dynamic brand name or suffix, you can also easily combine it:

{% set brand = "AnQi" %}
<p>我们提供的产品:{{ brand|add:"CMS" }}</p>

The output will be:我们提供的产品:AnQiCMS.

A clever combination of numbers and strings

addThe true charm of the filter lies in its ability to intelligently combine numbers and strings. This is very useful in scenarios such as building dynamic links, displaying version numbers with numbers or IDs, etc.

For example, you have an article IDarticle_idWith123And you want to add a prefix 'Article Number-' when displaying it, you can do it like this:

{% set article_id = 123 %}
<p>文章编号:{{ "文章编号-"|add:article_id }}</p>

The output will be:文章编号:文章编号-123.

You can also put the number in front:

{% set version = 2 %}
<p>当前版本号:{{ version|add:".1.0" }}</p>

The output will be:当前版本号:2.1.0.

Please note that in this mixed type operation, the string part is directly connected to the numeric part, rather than trying to convert the string to a number for mathematical addition.This behavior usually meets our expectations.

Handle null values and unexpected situations

addAnother practical aspect of the filter is its handling of null values. If you try to add a number with a non-existent (nothingOr adding a variable with an empty string will show good error tolerance.

{% set count = 5 %}
{% set nothing_value = nothing %} {# nothing表示变量未定义或为空 #}

<p>计算结果1:{{ count|add:nothing_value }}</p>
<p>计算结果2:{{ "安企"|add:nothing_value }}</p>
<p>计算结果3:{{ 5|add:"CMS"|add:nothing_value }}</p>

The output of this code will be:计算结果1:5 计算结果2:安企 计算结果3:5CMS

As you can see,addThe filter will ignore null values directly, returning only valid values, which is very convenient when dealing with templates that may contain null data, and avoids additional conditional judgment.

Summary

addThe filter is a seemingly simple but powerful tool in the AnQiCMS template engine, playing an important role in simplifying template logic and enhancing content dynamics. Whether it is pure numerical addition, flexible string concatenation, or a clever combination of both, addFilters can process with intelligent types and good error tolerance, helping you build rich and diverse website content more efficiently.Reasonably utilizing this filter can make your template code more concise and easier to maintain, allowing you to focus on the presentation of content itself.


Frequently Asked Questions (FAQ)

1.addDoes the filter support subtraction, multiplication, or division operations?

addThe filter is specifically used for the addition of numbers and concatenation of strings.It does not support direct subtraction, multiplication, or division operations.算术运算标签To get more information and usage methods.

2. If I need to add multiple strings or numbers consecutively,addHow to write a filter?

When you need to combine multiple values consecutively, you can do so like a pipeline andaddchain filters together. For example, if you want to combine three numbers or stringsvalue1/value2andvalue3Add consecutively, you can write it like this:{{ value1|add:value2|add:value3 }}The template engine will process these operations from left to right.

3.addWhat is the difference between the filter in processing Chinese and English?

addThe filter does not make any essential difference when processing Chinese and English strings.It will treat them all as ordinary text characters for concatenation.Whether it is Chinese, English, or characters of other languages, they will be connected in the order they appear in the string without affecting their function or performance.