In the template design of Anqi CMS, building dynamic prompts with real-time interaction is the key to enhancing user experience.Whether it is displaying product inventory, user greeting messages, or article reading volume, we hope to seamlessly combine fixed text with continuously changing variable content.addThe filter becomes a very practical and flexible tool.
addThe role of the filter in the security CMS template is very intuitive: it can add or concatenate two values.This process has high intelligence. If the two values being operated on are both numeric types, it will perform mathematical addition; while if one or both are strings, it will concatenate them.This flexibility allows us to worry-free when it comes to type mismatch issues, and we can directly integrate different types of data together, which is very convenient.
Flexible concatenation of scenarios and examples
UnderstoodaddThe basic principle of the filter, let's see how it plays a role in the actual template construction.
Simple numerical additionWhen we need to perform a simple sum of two numbers in a template,
addthe filter can easily handle it.{% set num1 = 5 %} {% set num2 = 2 %} 结果:{{ num1|add:num2 }} {# 显示结果:7 #}It also supports floating-point numbers, or automatically converts string-type numbers to numbers for calculation:
{% set price = 10.50 %} {% set tax = 2.30 %} 总金额:{{ price|add:tax }} {# 显示结果:12.8 #}Concatenation of plain text contentIn addition to numbers,
addThe filter can also handle the concatenation of plain text content well.{% set brand = "安企" %} {% set product = "CMS" %} 产品名称:{{ brand|add:product }} {# 显示结果:安企CMS #}Combine fixed text with dynamic variablesThis is
addThe most common application scenarios of filters, especially suitable for building dynamic prompt information.We can easily concatenate a descriptive text with data variables obtained from the backend.<p>版权所有 © {{ now "2006"|add:' '|add:system.SiteName }}</p> {# 假设当前年份是2023,网站名称是“安企CMS”,显示结果:版权所有 © 2023 安企CMS #}Display the number of views of the article on the article detail page:
<p>本文已被阅读 {{ archive.Views|add:' 次' }}</p> {# 假设文章阅读量是1234,显示结果:本文已被阅读 1234 次 #}If you need to display detailed contact information on the contact us page, you can also use it
addFilter:<p>客服热线:{{ contact.Cellphone|add:'(周一至周五 9:00-18:00)' }}</p> {# 假设联系电话是13800138000,显示结果:客服热线:13800138000(周一至周五 9:00-18:00) #}Handle cases where the variable is empty or the type is incompatible
addThe filter also performs robustly when dealing with variables that may be empty. If one of the operands is a null value (nothingornilIt will try to retain non-empty parts or ignore empty parts to avoid template errors.{% set value = 100 %} {% set empty_var = nothing %} 结果:{{ value|add:empty_var }} {# 显示结果:100 (将empty_var忽略)#} {% set text = "产品编号:" %} {% set id = nothing %} 产品信息:{{ text|add:id }} {# 显示结果:产品编号: (如果id为空,会直接拼接空字符串) #}This fault-tolerant mechanism makes developers feel more at ease when handling variables that may or may not exist.
Use suggestions and precautions
- Readability first:Although
addThe filter supports chained calls (such as{{ var1|add:var2|add:var3 }}),but when concatenating long texts or multiple variables, excessive chaining may reduce the readability of the template. At this point, it may be considered to usesetLabel the intermediate results first, or handle complex strings in advance at the controller level in the Go language. - Context awareness:Ensure that the variables you are using are available in the current template context. The template tags of safe CMS (such as
archiveDetail/system/contact) will provide the corresponding context data. - Combined with other filters:
addFilter can be used with other filters (for exampledefault/truncatecharsCombining them to achieve finer control, such as providing default values for possibly empty variables or limiting the length of the concatenated string.
PassaddFilter, the dynamic content generation of the Aiqi CMS template becomes extremely simple and efficient, it helps us skillfully integrate fixed information with changing business data in a natural and smooth manner, jointly constructing a user-friendly and information-rich page experience.
Common Questions (FAQ)
Q1:addFilters and using directly{{ var1 }}{{ var2 }}What is the difference?
A1:Directly use:{{ var1 }}{{ var2 }}Will directly output the values of two variables one by one, always performing string concatenation.addFilter more intelligent: If both operands are numbers, it will perform mathematical addition; if one or both are strings, it will perform string concatenation. This meansaddWhen processing numbers, you can perform calculations, not just simple string concatenation.
Q2: IfaddA variable in the filter is empty (for examplenothingornil), what is the impact?
A2:WhenaddThe filter usually shows strong fault tolerance when encountering null values.If the other operand is a number, the empty value is ignored and only the number itself is retained; if the other operand is a string, the empty value is typically treated as an empty string, resulting in a concatenated string that does not include the content of the empty value.This helps avoid errors when the template is reporting missing data.
Q3:addCan the filter perform complex mathematical operations? For example, multiplication or division?
A3: addThe filter focuses only on addition and concatenation operations. If you need to perform more complex mathematical operations such as multiplication, division, or more advanced arithmetic logic, the Anqi CMS provides specializedcalcLabel (please refer to)tag-calc.mddocument), it supports a wider range of mathematical expressions.addis designed to keep it simple, focusing on its core addition/joining functions.