How to perform addition operation of two numbers (integer or floating point) in AnQiCMS template?

Calendar 👁️ 74

In AnQiCMS templates, handling numbers, especially performing simple addition operations, is a common requirement for content display and data processing.AnQiCMS with its efficient architecture based on the Go language and flexible Django-style template engine provides us with intuitive and powerful tools to deal with these scenarios.Whether you need to accumulate statistical data or make some simple numerical adjustments when displaying on the front end, you can easily perform addition operations on numbers in the template.

Use arithmetic operators for addition directly

First, for some direct, fixed number addition, or when you have determined the variable type is a number, AnQiCMS template engine allows you to use the plus sign directly+Performing calculations. This is very similar to the way we perform mathematical calculations in programming languages, where the template engine intelligently parses expressions and provides results.

For example, if you want to add two integers in a template:

{{ 10 + 20 }}

The result will be displayed30.

Similarly, the operation is the same for floating-point numbers:

{{ 5.5 + 3.2 }}

This will result in8.7.

When you need to add a variable to a fixed number, you can also do it like this. Assuming youritemobject has aPriceattribute:

商品价格加上运费:{{ item.Price + 15.00 }}

here, the template willitem.Priceadd the value of15.00and display the total price.

In addition to addition, you can also use subtraction (-), multiplication (*), division (/) even modulo (%)Basic arithmetic operations, all of which follow similar direct operation rules. This method is simple and intuitive, suitable for those scenarios where the operation object types are clear and the operations are direct.

UseaddThe filter implements a more flexible addition

When the source of the number you are dealing with is uncertain, or you may need to concatenate numbers with strings (although this is not strictly mathematical addition, it is common in some display scenarios), in the AnQiCMS template,addThe filter will be a more powerful and versatile choice. This filter can not only perform addition of numbers but also intelligently handle different types of data.

addThe method of using a filter is to add a pipe symbol after a variable|then isadd:and the value you want to add. For example:

{{ 变量 | add: 要相加的值 }}

Let's look at some specific examples:

  1. Pure number addition:Assuming you have two numeric variablesnum1andnum2.

    {% set num1 = 15 %}
    {% set num2 = 7 %}
    计算结果:{{ num1|add:num2 }}
    

    This will display22.

  2. Add floating-point numbers:

    {% set float1 = 12.3 %}
    {% set float2 = 8.7 %}
    浮点数结果:{{ float1|add:float2 }}
    

    the result will be21.0.

  3. Add variables with literals:Assumearchiveobject has aViews(View count) attribute, do you want to add extra on top of this100.

    总浏览量:{{ archive.Views|add:100 }}
    
  4. The 'concatenation' (concatenation) of numbers and strings:This isaddA very practical feature of the filter. When one of the operands is a string and the other is a number,addThe filter will try to concatenate strings.

    {% set quantity = 5 %}
    显示数量:{{ "商品数量:"|add:quantity }}
    

    This will display.商品数量:5.

addThe filter will retain the original number or directly perform string concatenation when encountering mixed types that cannot be added mathematically (such as adding a pure number to a string that cannot be converted to a number), which increases the error tolerance of template data processing and makes your template code more robust.

When to choose which method?

Generally speaking, if your operands are clear numeric types (whether integers or floating-point numbers) and involve simple addition, subtraction, multiplication, or division, you can directly use+The equality operator makes it more concise and intuitive, and the code readability is also higher.

When you need to handle variables from different sources, the types of which may be uncertain, or when there is a need to concatenate numbers and strings,addThe filter provides greater flexibility and stronger fault tolerance, avoiding potential errors caused by type mismatches, allowing the template to run stably in complex scenarios.

In the actual operation of Anqi CMS, these template calculation functions are very practical.For example, on an e-commerce website, you may need to calculate and display the cumulative sales of products, total inventory, or display dynamic reading volume on article pages (such as the basic reading volume plus the new reading volume on the day).Apply these addition operation skills flexibly, allowing you to build and maintain websites more easily, achieving more dynamic and expressive content display.


Frequently Asked Questions (FAQ)

  1. Q: Besides addition, what arithmetic operations does AnQiCMS template support?A: Besides addition (+), you can also use subtraction directly in the template (-), multiplication (*), division (/)、modulus(%) and other basic arithmetic operators. In addition,addThe filter is mainly used for addition and concatenation, but it can also be regarded as a "cumulative" operation with stronger versatility.

  2. Q: If I want to add a numeric variable and a string variable together, what will happen?A: If you use directly+The operator performs the operation, the template engine will try to convert the string to a number for calculation. If the conversion fails, it may cause an error. But if usingaddA filter that handles more intelligently: if both are numbers, then perform mathematical addition; if one is a number and the other is a string, it usually performs string concatenation, converting the number to a string and then connecting it.

  3. Q: How to control the precision of the decimal point of the calculation result?A: If you have specific requirements for the precision of the floating-point number calculation result, you can usefloatformatthe filter to control it. For example,{{ value|floatformat:2 }}WillvalueFormatted to two decimal places. If a specific number of digits is not specified, it will default to one decimal place, and if the last digit is 0, the decimal part will not be displayed.

Related articles

The `wordcount` filter considers which delimiters in addition to spaces when distinguishing words?

In AnQi CMS template design and content management, we often use various filters to process and display data, among which the `wordcount` filter is a practical tool for counting the number of words in the text.For content operators, it is crucial to accurately understand its working mechanism, especially in distinguishing words when it considers boundaries in addition to spaces.According to AnQiCMS documentation, the `wordcount` filter's core recognition logic is **based on space separation** when calculating word count.

2025-11-07

How to quickly calculate the number of words in the AnQiCMS article summary?

In the daily operation of website content, the number of characters and words in the article introduction (or summary) is often an indispensable part of content optimization.In order to optimize for search engines (SEO), ensure that the abstract is fully displayed in the search results, or to improve the user reading experience, the length of a well-suited introduction is crucial.For those who use AnQiCMS, understanding how to effectively manage and quickly calculate the length of these summaries can significantly improve work efficiency.###

2025-11-07

How compatible are the `cut` and `replace` filters when handling Chinese character strings?

In daily website content operations, we often need to process various text content, whether it is to remove unnecessary characters or replace specific words, efficient and accurate string operations are indispensable.AnQiCMS is a modern content management system developed based on the Go language, which provides rich filters (Filters) in the template engine to meet these needs.

2025-11-07

What is the effect when the new word is empty in the `replace` filter while performing keyword replacement?

In AnQiCMS template language, the `replace` filter is a very practical tool that allows us to flexibly modify text content, such as replacing keywords, unifying terms, or performing simple text formatting.But a common issue when using this filter is: What if I intend to replace a word but do not provide a new replacement word?This is the core we are discussing today. ### Basic usage of `replace` filter First, let's review the basic operation of the `replace` filter

2025-11-07

Can the `add` filter be used directly for string concatenation to achieve the effect of 'Hello' + 'World'?

When creating website content and customizing templates on AnQi CMS, we often encounter situations where we need to process and combine text or data.For example, you may want to combine two separate words into a complete sentence;Or when displaying numerical information, concatenate it with a specific unit or description.At this point, various filters in the template are particularly important.The AnQi CMS is built-in with a powerful Django-style template engine, which provides rich tags and filters to help us display content more flexibly.These tools can not only traverse data, but also make conditional judgments

2025-11-07

What is the processing logic when the `add` filter encounters different types of data (such as numbers and strings) during addition?

In AnQi CMS template development, Filters are an important tool for processing and transforming data.Among them, the `add` filter, due to its unique behavior in handling numbers and strings, often causes users to think about its processing logic.What happens when the `add` filter encounters different types of data (such as numbers and strings) when adding?This article will delve into this mechanism in depth.

2025-11-07

How to combine template filters for front-end validation or preprocessing when performing 'content keyword replacement' in the AnQiCMS background?

In AnQiCMS content operation, the 'Content Keyword Replacement' is undoubtedly a powerful tool to improve efficiency and optimize content quality.It allows the operator to globally adjust specific words or phrases in the website content in batches, whether it is for brand unification, SEO optimization, or information updates.However, relying solely on the backend replacement function may sometimes be insufficient to meet the refined display needs of the front end.This cleverly combines AnQiCMS's template filters to bring more flexibility and control to content display, achieving a better user experience.

2025-11-07

How to combine text filters (such as `split`, `contain`, `replace`) to build a more intelligent AnQiCMS content review mechanism?

Content review is an indispensable part of website operation, not only concerning the quality of content, but also an important guarantee for maintaining a healthy ecological environment and ensuring compliance.In AnQiCMS, in addition to the built-in security mechanisms such as sensitive word filtering, we can also cleverly utilize its powerful template filter functions, especially the `split`, `contain`, and `replace` three text processing filters, to build a more flexible, intelligent, and practical content review auxiliary mechanism that meets actual operational needs.

2025-11-07