In AnQiCMS template design, sometimes we need to perform dynamic calculations on the numbers displayed on the page, such as calculating the total price of products, displaying the cumulative points of users, or adjusting certain numbers.Whether it is a simple addition of integers or calculations involving decimal floating-point numbers, AnQiCMS provides intuitive and powerful methods to meet these needs.This article will deeply explore how to implement addition operations in the AnQiCMS template, making your content display more dynamic and practical.
1. Use arithmetic operators directly for addition
AnQiCMS's template engine supports syntax similar to Django and Blade, which means you can use double curly braces{{ }}Directly use standard arithmetic operators for numerical calculations. For addition, simply use+the symbol. This method is intuitive and efficient, suitable for mathematical calculations between pure numbers.
1. Addition of integers
When you need to add two or more integers, simply place them+It can be done by placing numbers on both sides. This is very convenient for scenarios such as counting the number of article views, user points accumulation, or calculating the number of products.
For example, if you have a variableitem.ViewsRepresents the initial page views of the article and wants to increase a fixed value on this basis:
{# 假设 item.Views 的值为 100 #}
<p>文章总浏览量:{{ item.Views + 50 }}</p>
{# 页面将输出:文章总浏览量:150 #}
You can also add two variables together:
{% set quantity1 = 10 %}
{% set quantity2 = 20 %}
<p>商品总数量:{{ quantity1 + quantity2 }}</p>
{# 页面将输出:商品总数量:30 #}
2. Floating-point addition operation
When dealing with floating-point numbers containing decimals, you can use operators directly+for calculating the total price of goods, discounted prices, and other situations, which is crucial.
For example, add two floating-point variables:
{% set price1 = 19.99 %}
{% set price2 = 2.50 %}
<p>商品总价格:{{ price1 + price2 }}</p>
{# 页面将输出:商品总价格:22.49 #}
Similar to integer operations, you can also add floating-point numbers with integers:
{% set basePrice = 15.75 %}
<p>调整后价格:{{ basePrice + 3 }}</p>
{# 页面将输出:调整后价格:18.75 #}
In addition to addition, the AnQiCMS template also supports:-(subtraction),*Multiplication,/Division and other basic arithmetic operators, their usage is similar to addition.
Second, useaddThe filter adds numbers or strings.
Except for using arithmetic operators directly, AnQiCMS also provides a more flexible feature,addfilter.addThe filter is particularly suitable for scenarios where values of different types (numbers and strings) need to be 'added' or 'concatenated', and it will automatically try to perform type conversion.
1.addBasic usage of the filter.
addThe syntax of the filter is{{ obj|add:value }}of whichobjIt is the original value,valueThe value to be added.
WhenobjandvalueThey are all numbers (integers or floating-point numbers),addThe filter will perform mathematical addition:
{# 整数相加 #}
<p>整数结果:{{ 5|add:2 }}</p>
{# 页面将输出:整数结果:7 #}
{# 浮点数相加 #}
<p>浮点数结果:{{ 5.5|add:1.5 }}</p>
{# 页面将输出:浮点数结果:7.0 #}
2.addThe filter handles the addition of mixed types
addThe strength of the filter lies in its ability to intelligently handle the mixed addition of numbers and strings. When one of the operands is a string,addThe filter tries to convert the other operand to a string and then concatenate it. If the conversion fails, it ignores the part that cannot be converted.
For example, concatenating a number with a string:
{# 字符串与字符串拼接 #}
<p>文本内容:{{ "安企"|add:"CMS" }}</p>
{# 页面将输出:文本内容:安企CMS #}
{# 数字与字符串拼接 #}
<p>数字与字符串:{{ 5|add:"CMS" }}</p>
{# 页面将输出:数字与字符串:5CMS #}
{# 字符串与数字拼接 #}
<p>字符串与数字:{{ "安企"|add:2 }}</p>
{# 页面将输出:字符串与数字:安企2 #}
It should be noted that if:addOne side of the filter is a number,