In AnQiCMS template, we often need to display website content and data.But sometimes, we need not only to display the stored data, but also to perform some simple processing on it, such as calculating the total price of goods, statistics of the loading progress percentage of the page, or累加 etc.The template system of AnQiCMS provides very friendly support for such needs, it built-in powerful expression processing capabilities, allowing us to directly perform basic arithmetic operations such as addition, subtraction, multiplication, and division in the template, and display the results in real time.
AnQiCMS's template syntax is similar to Django template engine, so it will be very easy for users familiar with this syntax to get started. It allows you to use double curly braces{{ ... }}Write expressions directly, and the system will automatically parse and calculate the results.
Perform arithmetic operations directly.
In AnQiCMS templates, arithmetic operations are very intuitive. You just need to place operands and operators inside double curly braces, just like writing a regular mathematical formula.
Addition (+) And subtraction (-)
Imagine, you may want to display the result of adding or subtracting a number from another number on the page. This is very easy in AnQiCMS templates.
For example, you have a base valuebaseValueand want to increase or decrease it on this basisoffsetValues:
{# 显示两个数字的和 #}
<div>基础数值 + 偏移量:{{ baseValue + offset }}</div>
{# 显示两个数字的差 #}
<div>总金额 - 折扣:{{ totalAmount - discount }}</div>
AnQiCMS's template engine will automatically recognize these numbers and perform the corresponding operations, whether they are integers or floating-point numbers, and will always get an accurate result.
Multiplication (*) and division (/)
In e-commerce websites, calculating the total price of a product is a very common requirement, which requires the use of multiplication. For example, if you have a product objectitem, it haspriceandquantityProperties:
{# 计算商品总价 #}
<div>商品总价:{{ item.price * item.quantity }}</div>
Division operations are also intuitive, for example, if you want to calculate a ratio or percentage, such as the ratio of completed tasks to total tasks:
{# 计算任务完成度百分比 #}
<div>完成度:{{ (completedTasks / totalTasks) * 100 }}%</div>
It is worth noting that multiplication and division have a higher precedence than addition and subtraction. If you need to change the order of operations, you can use parentheses as in mathematics.()Come to clarify the priority to ensure the desired result.
Modulo (%)
If you need to determine whether one number is a multiple of another or need to periodically display certain content, the modulo operation (remainder) comes in handy.
{# 判断一个数除以3的余数 #}
<div>10除以3的余数是:{{ 10 % 3 }}</div>
{# 输出结果为 1 #}
This is very practical in scenarios where you need to alternate row colors, group display data, etc.
Extended applications:addFilter
In addition to using directly in{{ ... }}Performing arithmetic operations in expressions, AnQiCMS also provides some convenient filters (Filters) to help us process data, whereaddA filter is a good example, it can be used to quickly implement addition operations.
addThe main advantage of the filter lies in its flexibility, which can add integers, floating-point numbers, strings, and even mixed types of data.When the type does not match, it will attempt to perform an intelligent conversion. If the conversion fails, it will ignore the parts that cannot be added to avoid template errors.
{# 使用add过滤器进行数字相加 #}
<div>5加上2:{{ 5 | add:2 }}</div>
{# 输出结果为 7 #}
{# 字符串拼接 #}
<div>品牌名称:{{ "安企" | add:"CMS" }}</div>
{# 输出结果为 安企CMS #}
{# 变量与数字相加 #}
<div>文章浏览量增加100:{{ archive.Views | add:100 }}</div>
This approach is particularly convenient when dealing with scenarios that require string concatenation or numerical addition, as it provides a concise and powerful data processing method.
Practical scenarios and considerations
Mastered these basic arithmetic operations, your AnQiCMS template can handle more dynamic data displays.Whether it is the product price calculation on the front-end page, the generation of star display based on ratings, or simple proportion calculation in data charts, all can be directly implemented at the template level.
Processing such logic in the template allows the page to display more flexibility and intelligence, reducing reliance on backend data preprocessing and improving development efficiency.Of course, if the computational logic is too complex, involves a large amount of data, or complex business rules, it is usually recommended to place this part of the logic on the backend. Templates are more responsible for displaying, maintaining simplicity and maintainability.
In short, through simple{{ ... }}expressions and flexibleaddFilter, AnQiCMS template system opens the door for us to perform data arithmetic operations directly on the front end.It greatly enhances the dynamics and practicality of the template, making content operation and website management more efficient and convenient.
Common Questions (FAQ)
Q1: Can template variables or values returned by tags be used directly in arithmetic expressions?
A1: Yes, of course. AnQiCMS's template engine supports the direct use of defined template variables in arithmetic expressions, for example{{ item.Price * item.Quantity }}. In addition, some tags (such as{% archiveDetail with name="Views" %})The value directly output can also be used for further arithmetic operations, but it is recommended to assign it to a variable (such as{% set views = archive.Views %}),and then perform the calculation to maintain the clarity of the code.
Q2: AnQiCMS template supports which complex mathematical operations, such as exponentiation or square root?
A2: AnQiCMS模板引擎支持基本的算术运算符,包括加 (English)+)、减 (English)-)、乘 (English)*)、除 (English)/) 和取模 (English)%) Additionally, it also supports exponentiation operations, using^symbols, such as{{ 2 ^ 3 }}Will get 8.For more complex mathematical functions (such as square roots, trigonometric functions, logarithms, etc.), the template engine itself does not provide them directly. Usually, they need to be implemented through backend processing or custom filters.
Q3: What happens if the divisor is zero during division operations?
A3: When performing division operations in the template of AnQiCMS, if the divisor is zero, the template engine usually avoids directly reporting an error to prevent the page from crashing.It may return a zero value or an empty value, the specific behavior may vary depending on the version of the template engine or the context.