In the process of managing and displaying website content, we often encounter situations where we need to combine different types of data.For example, concatenate a number with a specific prefix, or automatically add a unit when displaying the quantity of products.The template system of AnQiCMS (AnQiCMS) provides us with flexible and powerful functions to handle such requirements, especially for the operation of adding numbers and strings mixed together, based on the Go language and compatible with the Django template engine syntax.
Mixed addition of numbers and strings in AnQiCMS template
When we need to mix numbers and strings in templates for operations, such as combining the list number with the article title, or displaying the calculation result with the unit, Anqi CMS provides intuitive and powerful tools to achieve this.
Core Tool:addFilter
In AnQi CMS template syntax,addThe filter is the key to performing the mixed addition of numbers and strings.This filter is designed to be very intelligent, it can automatically judge whether to perform addition on mathematical operands or to concatenate strings based on the specific type of the operands.
addHow to use the filter
addThe syntax of the filter is very concise:{{ 变量A|add:变量B }}.变量Ais the initial value you want to operate on,变量Bis the value you want to add or concatenate.
Let's understand its behavior through some specific examples:
Pure numbers additionIf
变量Aand变量BAll of them can be parsed as numbers,addThe filter will perform mathematical addition.{{ 5|add:2 }} {# 显示结果: 7 #} {{ 5|add:40 }} {# 显示结果: 47 #}Concatenation of numbers with stringsWhen one operand is a number and the other is a string that cannot be parsed as a pure number,
addThe filter performs string concatenation. It automatically converts numbers to strings and then concatenates them with another string.{{ 5|add:"CMS" }} {# 显示结果: 5CMS #}String concatenation with string: If both operands are strings,
addthe filter will concatenate them directly.{{ "安企"|add:"CMS" }} {# 显示结果: 安企CMS #} {{ "安企"|add:"2" }} {# 显示结果: 安企2 (这里“2”被视为字符串,因为“安企”不是数字) #}
Actual application example
in the actual display of website content,addThe filter can be very useful. For example, when looping through a list of articles, we may want to add a unique identifier based on the loop index to the CSS class name of each article, or add a serial number before the title:
{% archiveList archives with type="page" limit="5" %}
{% for item in archives %}
<div class="article-item item-{{ forloop.Counter|add:100 }}"> {# 将循环计数器加上100,作为类名后缀 #}
<h3>第{{ forloop.Counter|add:"篇" }}文章:{{ item.Title }}</h3> {# 将循环计数器与文本拼接 #}
<p>{{ item.Description }}</p>
</div>
{% endfor %}
{% endarchiveList %}
In this example,forloop.Counteris a number, passedadd:100implemented number addition, generated likeitem-101such a class name; passedadd:"篇"implemented the concatenation of numbers and strings, generated第1篇文章such a title.
Not justadd: Other arithmetic operations
In addition to the flexible mixed addition operation, the AnQiCMS template also supports pure number arithmetic operations. When you are sure that the operands are all numbers, you can use them directly in the template.+(add),-(subtract),*(multiply),/(divide) operators. They are usually used in simple numerical calculations or conditional judgments.
For example:
{% set price = 100 %}
{% set discount = 20 %}
{% set finalPrice = price - discount %} {# 纯数字减法 #}
<p>原价:{{ price }}元,折扣:{{ discount }}元,实际支付:{{ finalPrice }}元</p>
{% set quantity = 5 %}
{% set unitPrice = 15.5 %}
{% set totalCost = quantity * unitPrice %} {# 纯数字乘法 #}
<p>购买数量:{{ quantity }},单价:{{ unitPrice }}元,总费用:{{ totalCost }}元</p>
These operators are mainly used for calculations between pure numbers, and if it involves strings and you want to get the concatenation result, thenaddThe filter is a safer and clearer choice.
Summary
The Anqi CMS template system passedaddThe filter provides a powerful and flexible ability to mix numbers and strings, whether it is generating dynamic CSS class names, concatenating text with numbers, or combining values with units, it can handle it easily.At the same time, for operations on pure numbers, you can also use basic arithmetic symbols directly.Understand and make good use of these template functions, which will greatly improve the efficiency of dynamic content generation on your website and the flexibility of template development.
Frequently Asked Questions (FAQ)
addCan the filter perform mathematical addition on two strings representing numbers? For example,"5"|add:"2"The result is"52"Or7?Answer: According to the behavior of the AnQiCMS template engine, if both operands are strings that can be parsed as pure numbers (such as"5"and"2")addThe filter will first try to perform mathematical addition, therefore{{ "5"|add:"2" }}The result is7. Only when one of the operands is clearly not parseable as a number (for example"安企"), it will fallback to string concatenation mode.except
|add:Filter, can I use it directly?+Can I add numbers and strings together using the symbol?Answer: It is not recommended to use it directly.+Symbols for adding numbers and strings together. In AnQiCMS templates,+The operator is mainly used for mathematical addition between pure numbers. If strings are involved, it is strongly recommended to use a dedicated|add:Filter to handle mixed concatenation or addition operations of numbers and strings.If I need to repeat a string or number multiple times without adding or concatenating, which filter should I use?Answer: If you need to repeat a string or number multiple times, for example, to repeat
“安企CMS”repeated5times, you should userepeatfilter. Its usage is{{ "安企CMS"|repeat:5 }}, which will output安企CMS安企CMS安企CMS安企CMS安企CMS.