In AnQiCMS template design, dynamically handling data is a key factor in building a feature-rich website.Whether it is to calculate the total price of goods or to concatenate user-friendly prompt information, the template engine provides a concise and efficient way to complete these tasks.This article will deeply explore how to add numbers and concatenate strings in AnQiCMS templates, helping you better utilize template functions.


1. Add numbers in the template: Make the calculation more intuitive

In the AnQiCMS template, when you need to perform arithmetic operations such as addition, subtraction, multiplication, and division on numbers, you can directly use double curly braces{{ }}It uses standard mathematical operators. This means that if your variable stores numeric values, you can perform direct calculations as you would in a programming language.

For example, you may need to calculate the total cost of a product, which includes the unit price and quantity, as well as shipping fees:

{# 假设 item.Price 是商品单价,item.Quantity 是购买数量,item.ShippingFee 是运费 #}
商品总成本:{{ item.Price * item.Quantity + item.ShippingFee }} 元

This style makes the template code more readable and more intuitive. The template engine will automatically recognize and execute these arithmetic operations.

In addition to this direct arithmetic operation, AnQiCMS also provides a convenientaddFilter, used specifically to add two values.This filter is particularly suitable for adding the value of one variable to another variable or a fixed number, it can intelligently handle different types of input.

Useaddexample of using the filter to add numbers as follows:

{# 假设 num1 = 10,num2 = 5 #}
计算结果:{{ num1|add:num2 }}
{# 输出: 15 #}

addThe filter attempts to convert numbers to numeric types for addition when processing them.If the conversion is successful, the result is the sum of two numbers; if one of the values cannot be converted to a number, it will usually try to concatenate strings, which is what we will discuss next.addThe filter is very useful when dealing with mixed data types.

II. String concatenation in templates: building flexible text content.

In website operation, we often need to dynamically combine text according to different scenarios, such as generating messages with variable values, dynamic titles, or URL parameters. In the AnQiCMS template,addThe filter is a powerful tool for implementing string concatenation.

WhenaddThe filter detects when the operand is a string or cannot be converted to a pure number, and then it intelligently performs string concatenation.This means you can easily concatenate text literals, variable values, and other data types together.

Here are some uses ofaddCommon scenarios for using the filter to concatenate strings:

1. Concatenating two string variables:

{# 假设 username = "AnQiCMS 用户" #}
欢迎消息:{{ "欢迎您,"|add:username }}
{# 输出: 欢迎您,AnQiCMS 用户 #}

2. Concatenating strings with numeric variables:

When you need to embed numeric values into text,addthe filter will automatically convert it to a string and then concatenate.

{# 假设 orderId = 12345,status = "已发货" #}
订单状态:{{ "订单号:"|add:orderId|add:",状态:"|add:status }}
{# 输出: 订单号:12345,状态:已发货 #}

here we use chaining.addA filter that concatenates multiple string and numeric variables to form a complete sentence.Pay attention, if you need to add spaces or other punctuation marks between concatenated strings, you must include these symbols in the string to be concatenated or use them as separate strings.

3. Concatenating strings with boolean values or other non-string types:

Similarly,addThe filter will also try to convert boolean values (such astrueorfalse)or other complex types (such as object names) are converted to their string representation and then concatenated.

{# 假设 isAdmin = true #}
权限信息:{{ "当前用户是管理员:"|add:isAdmin }}
{# 输出: 当前用户是管理员:true #}

ByaddFilter, you can flexibly combine various data types to build dynamic and adaptable text content to meet the various display needs of the website front-end.


Summary

AnQiCMS's template engine provides intuitive and powerful support for numerical calculations and string concatenation. For simple arithmetic operations, you can directly use{{ }}Use standard operators instead;addThe filter handles addition of numbers and concatenation of strings, especially showing great flexibility and convenience when dealing with mixed data types.Mastering these methods will help you design and implement the dynamic content display of the AnQiCMS website more efficiently.


Frequently Asked Questions (FAQ)

Q1: Can I perform subtraction, multiplication, division, etc. directly in the template?A1: Yes, AnQiCMS template engine supports direct arithmetic operations. You can{{ }}use standard operators, such as{{ var1 - var2 }}to perform subtraction,{{ var1 * var2 }}to perform multiplication, or{{ var1 / var2 }}Perform division. This makes basic mathematical calculations in the template very intuitive and convenient.

Q2: If I try to useaddA filter to concatenate two values, but one of the values isnilWhat will happen if a null value or undefined variable occurs?A2:addThe filter encountersnilor an undefined variable, it will try to perform error tolerance handling. Usually, if it expects a number to be added,nilit will be treated as,0processing; if it expects a string,nilbe treated as an empty string.This means that your template usually will not cause an error because the variable is empty, but you need to pay attention to whether the final output meets your expectations.iftags).

Q3:addHow to automatically add spaces between strings when concatenating filters?A3:addThe filter does not automatically add spaces between concatenated strings.If you need a space, you must explicitly include it in one of the string parameters or concatenate it as a separate string.{{ "Hello "|add:"World" }}Or{{ "Hello"|add:" "|add:"World" }}.