In the template design of AnQi CMS, dynamically combining text information or performing calculations on numerical values is a common requirement.Whether it is to construct personalized product descriptions or to display dynamic data on the page, flexibly handling string concatenation and numeric addition operations is crucial.The template system of Anqi CMS, draws on the syntax of Django template engine, and provides a variety of intuitive and powerful methods to complete these tasks.

Core operation:addFilter, considering concatenation and addition

The most common and convenient way to handle string concatenation and numerical addition in Anqi CMS template is to useaddFilter. This filter is designed to be very intelligent, it can automatically determine whether to perform numerical addition or string concatenation based on the data type you provide.

1. Numerical Addition

When you apply theaddfilter to two numbers, it performs standard mathematical addition. Even if one of the numbers is in string format, as long as it can be converted into 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 as)100) andtax(numeric, such as)20), 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 two stringsaddWhen filtering, it will connect them directly together.

For example, if 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. Handling mixed types

addThe most convenient aspect of the filter is that it can intelligently handle mixed operations of numbers and strings.If one operand is a number and the other is a string, it usually converts the number to a string and then performs string concatenation.

Assuming you want to display the page views of an article and add the unit "times viewed":

{% set archiveViews = 520 %}
{% set unit = " 次浏览" %}
<p>本文已被浏览:{{ archiveViews|add:unit }}</p>
{# 输出:本文已被浏览:520 次浏览 #}

4. When encountering a null value (nothing) handling

IfaddOne operand of the filter is a null value (e.g.nilIt will attempt to ignore empty values and only use another valid value if one is available.

{% set validNumber = 10 %}
{% set emptyValue = nothing %} {# 假设 nothing 是一个空值 #}
<p>计算结果:{{ validNumber|add:emptyValue }}</p>
{# 输出:计算结果:10 #}

The following is an example that includes a variety ofaddA comprehensive example of filter usage methods, 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 = 安企 #}

Concatenation and addition operations in more scenarios

AlthoughaddThe filter is very powerful, but in some specific scenarios, you may also need to use other tools:

1. Pure numerical calculations: Arithmetic operation tags

If your calculation involves only numbers and requires complex mathematical expressions such as subtraction, multiplication, and division, Anqi CMS supports direct arithmetic operations within double curly braces (following the syntax of the Go language)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. Fine-grained 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 or suffixes, etc.stringformatThe filter will be very useful. It can output various types of data as strings in the specified format template.

For example, format a floating-point number to a string with two decimal places:

{% set price = 12.3456 %}
<p>格式化价格:{{ price|stringformat:"%.2f" }} 元</p> {# 输出:格式化价格:12.35 元 #}

3. Aggregation of list items:joinFilter

If you have an array (or list) and you want to concatenate all its elements into a string,joinThe filter is your ideal choice. It allows you to specify a delimiter to connect all items in the array.

For example, concatenate a list of tags into a string:

{% set tags = ["CMS", "模板", "Go"] %}
<p>所有标签:{{ tags|join:", " }}</p> {# 输出:所有标签:CMS, 模板, Go #}

Summary

MasteraddFilter, and you can handle text concatenation and numerical calculation requirements more flexibly in the Anqi CMS template. At the same time, combined算术运算标签perform pure numerical operations, as well asstringformatandjoinfilter to perform