In AnQi CMS template design, building dynamic prompt information with real-time interactive sense is the key to improving user experience.Whether it is to display product inventory, user greeting messages, or article reading volume, we hope to seamlessly combine fixed text with continuously changing variable content. At this point,addThe filter becomes a very practical and flexible tool.
addThe filter's role in the Anqi CMS template is very intuitive: it can add or concatenate two values.This process has a high degree of intelligence, if the two values operated on are both numeric types, it will perform mathematical addition.And if one or two of the values are strings, it will concatenate them.This flexibility allows us to not worry about data type mismatch issues, 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 addition of two numbers in a template,
addthe filter can easily complete it.{% set num1 = 5 %} {% set num2 = 2 %} 结果:{{ num1|add:num2 }} {# 显示结果:7 #}It also supports floating-point numbers, or automatically converts string 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 scenario of the filter, especially suitable for building dynamic prompt information.We can easily concatenate a descriptive text with data variables obtained from the backend.For example, display copyright information at the footer of the website, combining fixed text with the website name and the current year from system variables:<p>版权所有 © {{ now "2006"|add:' '|add:system.SiteName }}</p> {# 假设当前年份是2023,网站名称是“安企CMS”,显示结果:版权所有 © 2023 安企CMS #}On the article detail page, display the number of reads of the article:
<p>本文已被阅读 {{ archive.Views|add:' 次' }}</p> {# 假设文章阅读量是1234,显示结果:本文已被阅读 1234 次 #}If you need to display detailed contact information on the contact us page, you can also make use of
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 handling variables that may be empty. If one of the operands is a null value (nothingornilIt tries to keep 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 error-tolerant mechanism makes developers feel more at ease when dealing with variables that may not exist.
Use suggestions and precautions
- Readability first:Though
addFilters support 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 pre-process complex strings at the controller level in the Go language. - Context-aware:Ensure the variable you are using is available in the current template context. The Anqi CMS template tags (such as
archiveDetail/system/contactetc.) will provide the corresponding context data. - Combined with other filters:
addThe filter can be used with other filters, such asdefault/truncatecharsUse them in combination to achieve more fine-grained control, such as providing default values for variables that may be empty, or limiting the length of the concatenated string.
ByaddThe filter makes the dynamic content generation of Anqi CMS templates extremely simple and efficient, helping us to seamlessly integrate fixed information with variable business data in a natural and smooth way, thereby jointly constructing user-friendly and informative page experiences.
Frequently Asked Questions (FAQ)
Q1:addFilters and using directly{{ var1 }}{{ var2 }}What is the difference?
A1:directly{{ var1 }}{{ var2 }}It will directly output the values of two variables one by one, always performing string concatenation. AndaddThe filter is smarter: 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: IfaddFor example, one variable in the filter is nullnothingornil) what is the impact?
A2:WhenaddThe filter usually shows strong 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, null values are usually treated as empty strings, and the final result is a concatenation of strings without null content.This helps to avoid errors when the template is 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 calculations such as multiplication, division, or advanced arithmetic logic, Anqi CMS provides specializedcalcLabel (please refer to)tag-calc.mdThe document supports a wider range of mathematical expressions.addThe design philosophy is to keep it simple, focusing on its core addition/joining function.