How to perform addition operations on numbers in AnQi CMS template, including integers and floating-point numbers?

Calendar 👁️ 58

In AnQiCMS template design, sometimes we need to perform dynamic calculations on the numbers displayed on the page, such as calculating the total price of products, displaying the cumulative points of users, or adjusting certain numbers.Whether it is a simple addition of integers or calculations involving decimal floating-point numbers, AnQiCMS provides intuitive and powerful methods to meet these needs.This article will deeply explore how to implement addition operations in the AnQiCMS template, making your content display more dynamic and practical.

1. Use arithmetic operators directly for addition

AnQiCMS's template engine supports syntax similar to Django and Blade, which means you can use double curly braces{{ }}Directly use standard arithmetic operators for numerical calculations. For addition, simply use+the symbol. This method is intuitive and efficient, suitable for mathematical calculations between pure numbers.

1. Addition of integers

When you need to add two or more integers, simply place them+It can be done by placing numbers on both sides. This is very convenient for scenarios such as counting the number of article views, user points accumulation, or calculating the number of products.

For example, if you have a variableitem.ViewsRepresents the initial page views of the article and wants to increase a fixed value on this basis:

{# 假设 item.Views 的值为 100 #}
<p>文章总浏览量:{{ item.Views + 50 }}</p>
{# 页面将输出:文章总浏览量:150 #}

You can also add two variables together:

{% set quantity1 = 10 %}
{% set quantity2 = 20 %}
<p>商品总数量:{{ quantity1 + quantity2 }}</p>
{# 页面将输出:商品总数量:30 #}

2. Floating-point addition operation

When dealing with floating-point numbers containing decimals, you can use operators directly+for calculating the total price of goods, discounted prices, and other situations, which is crucial.

For example, add two floating-point variables:

{% set price1 = 19.99 %}
{% set price2 = 2.50 %}
<p>商品总价格:{{ price1 + price2 }}</p>
{# 页面将输出:商品总价格:22.49 #}

Similar to integer operations, you can also add floating-point numbers with integers:

{% set basePrice = 15.75 %}
<p>调整后价格:{{ basePrice + 3 }}</p>
{# 页面将输出:调整后价格:18.75 #}

In addition to addition, the AnQiCMS template also supports:-(subtraction),*Multiplication,/Division and other basic arithmetic operators, their usage is similar to addition.

Second, useaddThe filter adds numbers or strings.

Except for using arithmetic operators directly, AnQiCMS also provides a more flexible feature,addfilter.addThe filter is particularly suitable for scenarios where values of different types (numbers and strings) need to be 'added' or 'concatenated', and it will automatically try to perform type conversion.

1.addBasic usage of the filter.

addThe syntax of the filter is{{ obj|add:value }}of whichobjIt is the original value,valueThe value to be added. WhenobjandvalueThey are all numbers (integers or floating-point numbers),addThe filter will perform mathematical addition:

{# 整数相加 #}
<p>整数结果:{{ 5|add:2 }}</p>
{# 页面将输出:整数结果:7 #}

{# 浮点数相加 #}
<p>浮点数结果:{{ 5.5|add:1.5 }}</p>
{# 页面将输出:浮点数结果:7.0 #}

2.addThe filter handles the addition of mixed types

addThe strength of the filter lies in its ability to intelligently handle the mixed addition of numbers and strings. When one of the operands is a string,addThe filter tries to convert the other operand to a string and then concatenate it. If the conversion fails, it ignores the part that cannot be converted.

For example, concatenating a number with a string:

{# 字符串与字符串拼接 #}
<p>文本内容:{{ "安企"|add:"CMS" }}</p>
{# 页面将输出:文本内容:安企CMS #}

{# 数字与字符串拼接 #}
<p>数字与字符串:{{ 5|add:"CMS" }}</p>
{# 页面将输出:数字与字符串:5CMS #}

{# 字符串与数字拼接 #}
<p>字符串与数字:{{ "安企"|add:2 }}</p>
{# 页面将输出:字符串与数字:安企2 #}

It should be noted that if:addOne side of the filter is a number,

Related articles

How to get the last element/character of an array or string in Anqi CMS template?

In Anqi CMS template development, we often encounter the need to obtain the last element or character from a set of data (whether it is an array, list, or string).No matter whether it is to display the latest comments, the last item in the list, or extract the end information of specific text, understanding how to efficiently implement this at the template level will greatly enhance the flexibility and development efficiency of the template.The AnQi CMS template engine provides a simple and powerful filter function that can help us easily achieve this goal.

2025-11-08

How to get the first element/character of an array or string in AnQi CMS template?

During the development of website templates, you often encounter the need to extract the first element or character from a data collection (whether it is an array, slice, or string).AnQiCMS uses syntax similar to the Django template engine, providing rich tags (Tags) and filters (Filters), allowing us to flexibly and efficiently handle data.This article will introduce in detail how to use these powerful tools in the AnQiCMS template to easily achieve the operation of getting the first element/character of an array or string.--- ### One

2025-11-08

How to randomly select an element or character from an array or string in Anqi CMS template?

In Anqi CMS template, adding dynamism and fun to the content is an important aspect of improving user experience.Sometimes, we hope to display a random element on the page, such as a random slogan, a random recommendation tag, or randomly select a picture from a group of images.AnQi CMS relies on its flexible Django template engine syntax to provide a simple yet powerful way to meet this requirement, with the core tool being the `random` filter.

2025-11-08

The `count` filter calculates the number of times a keyword appears in an array, is it a partial match or a complete match?

In Anqi CMS template development, the `count` filter is undoubtedly a very practical tool for processing data, which can help us quickly count the frequency of a specific keyword in the data.However, when using this filter, many users may be puzzled: when it handles string and array data of different types, is the keyword matching method 'partial matching' or 'exact matching'?This is crucial for accurately presenting content data.

2025-11-08

How does the `add` filter handle string concatenation operations?

When developing templates with Anqi CMS, we often encounter scenarios where we need to combine text fragments or numbers for display.Whether it is to build dynamic page titles, concatenate URLs, or perform simple numerical calculations on the page, flexible data processing capabilities are crucial.The Anqi CMS template engine provides a very practical tool - the `add` filter, which can help us easily achieve string concatenation and numeric addition operations.What is the `add` filter?

2025-11-08

How to set a default display value for possibly empty variables (such as arrays or strings) in Anqi CMS templates?

During the process of building a website with AnqiCMS, we often encounter such situations: a variable needs to be displayed in the template, but it may be empty for various reasons, such as not setting a thumbnail, a list without content, or optional information not filled in.If these empty variables are not processed, the page may appear with ugly blank areas, error messages, or layout errors, which undoubtedly affects the user experience.

2025-11-08

What is the difference between the `default_if_none` filter and the `default` filter in handling null values?

In the development of content management system templates, we often encounter situations where variable values are uncertain.Sometimes, the data may not have been filled in; sometimes, the data may exist, but its value is an empty string, zero, or a boolean false.To ensure the completeness and user experience of the website page display, it is particularly important to provide a friendly default display for these 'empty' values.

2025-11-08

How to dynamically generate filter conditions for content models in AnQi CMS templates (e.g., filtering by properties)?

When building and operating a website, providing users with an efficient content filtering function is crucial for improving user experience and content discoverability.AnQiCMS (AnQiCMS) leverages its flexible content model and powerful template tag system to help us easily implement dynamic generation of content model filtering conditions in templates, such as filtering by attributes. ### Content Model and Custom Fields: The Basis of Filtering One of the core strengths of AnQi CMS is its highly flexible content model.

2025-11-08