In AnQi CMS template design, dynamically combining text information or performing calculations on numerical values is a common requirement.It is crucial to handle string concatenation and numerical addition operations flexibly, whether it is to build personalized product descriptions or to display dynamic data on the page.The Anqi CMS template system draws on the syntax of the Django template engine, providing a variety of intuitive and powerful methods to complete these tasks.
Core operation:addFilter, considering concatenation and addition
In Anqi CMS template, the most common and convenient way to handle string concatenation and numeric addition is to useaddFilter. This filter is very smart, it can automatically judge whether to perform numerical addition or string concatenation based on the data type you provide.
1. Add numbers
When you use two numbersaddWhen the filter is applied, it performs standard mathematical addition. Even if one of the numbers is in string format, as long as it can be converted to a valid number,addThe filter will also try to convert it to a number before adding.
For example, if you have two variablesitem.Price(Numeric, such as100) ortax(Numeric, such as20) you can calculate their sum like this:
{% set itemPrice = 100 %}
{% set tax = 20 %}
<p>商品含税总价:{{ itemPrice|add:tax }}</p>
{# 输出:商品含税总价:120 #}
Even if the tax amount is defined as a string, as long as the content is purely numeric,addthe filter can also handle:
{% set itemPrice = 100 %}
{% set taxString = "20" %}
<p>商品含税总价:{{ itemPrice|add:taxString }}</p>
{# 输出:商品含税总价:120 #}
2. String concatenation
When you use a filter on two strings,addit will concatenate them directly.
For example, you want to concatenate a product prefix and product ID to form a product code:
{% set productPrefix = "PROD-" %}
{% set productId = "12345" %}
<p>产品代码:{{ productPrefix|add:productId }}</p>
{# 输出:产品代码:PROD-12345 #}
3. Mixed type processing
addThe most convenient aspect of the filter is that it can intelligently handle mixed operations of numbers and strings.If an operand is a number and the other is a string, it usually converts the number to a string and then performs string concatenation.
Suppose you want to display the number of times an article has been viewed and add the unit "views" at the end:
{% set archiveViews = 520 %}
{% set unit = " 次浏览" %}
<p>本文已被浏览:{{ archiveViews|add:unit }}</p>
{# 输出:本文已被浏览:520 次浏览 #}
4. Handling of empty values (nothing)
IfaddAn operand of the filter is an empty value (for examplenilOr undefined variables), it will try to ignore empty values and only use another valid value.
{% set validNumber = 10 %}
{% set emptyValue = nothing %} {# 假设 nothing 是一个空值 #}
<p>计算结果:{{ validNumber|add:emptyValue }}</p>
{# 输出:计算结果:10 #}
The following is an example of containing a variety ofaddAn integrated example of filter usage, you can refer to it to understand its flexibility:
{% set num1 = 5 %}
{% set num2 = 2 %}
{% set str1 = "安企" %}
{% set str2 = "CMS" %}
{% set numStr = "2" %}
{% set emptyVal = nothing %}
<p>数字相加:{{ num1|add:num2 }}</p> {# 5 + 2 = 7 #}
<p>数字与字符串数字相加:{{ num1|add:numStr }}</p> {# 5 + "2" = 7 #}
<p>字符串拼接:{{ str1|add:str2 }}</p> {# 安企 + CMS = 安企CMS #}
<p>数字与字符串拼接:{{ num1|add:str2 }}</p> {# 5 + CMS = 5CMS #}
<p>字符串与数字拼接:{{ str1|add:num1 }}</p> {# 安企 + 5 = 安企5 #}
<p>与空值相加(数字):{{ num1|add:emptyVal }}</p> {# 5 + nothing = 5 #}
<p>与空值拼接(字符串):{{ str1|add:emptyVal }}</p> {# 安企 + nothing = 安企 #}
Operations of concatenation and addition in more scenarios
AlthoughaddThe filter is very powerful, but you may also need other tools in certain specific scenarios:
1. Pure numerical calculations: Arithmetic operation label
If your calculation only involves numbers and requires the execution of more complex mathematical expressions such as subtraction, multiplication, and division, Anqi CMS supports direct arithmetic operations within double curly braces (following the Go language's syntax)text/templateStyle).
For example, calculate the difference or product:
{% set valA = 100 %}
{% set valB = 50 %}
<p>减法:{{ valA - valB }}</p> {# 输出:50 #}
<p>乘法:{{ valA * valB }}</p> {# 输出:5000 #}
<p>除法:{{ valA / valB }}</p> {# 输出:2 #}
2. Refine the output:stringformatFilter
After you complete the concatenation or addition operation, if you need to output the result in a specific format, such as controlling decimal places, adding prefixes and suffixes, and so on,stringformatThe filter is very useful. It can format various types of data into strings according to a specified format template.
For example, format a floating-point number into a string with two decimal places:
{% set price = 12.3456 %}
<p>格式化价格:{{ price|stringformat:"%.2f" }} 元</p> {# 输出:格式化价格:12.35 元 #}
3. Aggregate of list items:joinFilter
If you have an array (or list) and you want to concatenate all its elements into a single string,joinThe filter is your ideal choice. It allows you to specify a delimiter to connect all items in the array.
For example, connect a tag list into a string:
{% set tags = ["CMS", "模板", "Go"] %}
<p>所有标签:{{ tags|join:", " }}</p> {# 输出:所有标签:CMS, 模板, Go #}
Summary
MasteraddThe filter allows you to handle text concatenation and numerical calculations more flexibly in the Anqi CMS template. At the same time, combined算术运算标签to perform pure numerical operations, as well asstringformatandjoinFiltering to perform