How to perform addition of numbers and strings in AnQiCMS template and display the result?

Calendar 👁️ 71

When building dynamic content in Anqi CMS templates, it is often necessary to perform simple calculations and concatenations of numbers or strings.Whether it is to display the cumulative user points or to display the product codes with specific prefixes in combination, Anqi CMS provides a flexible and convenient way to meet these needs.This article will deeply explore how to perform addition operations in the AnQiCMS template and provide detailed examples and practical suggestions.

Understanding the calculation capabilities of AnQiCMS template

The Anqi CMS template system is based on syntax similar to the Django template engine, which means it can not only display variable data intuitively but also supports some basic logical judgments and arithmetic operations.This design philosophy makes the dynamic content handling of the website more efficient and intuitive, reducing the over-reliance on backend logic, allowing front-end developers to have more freedom in controlling the presentation of content.

There are mainly two ways to perform addition operations in AnQiCMS templates: one is to use specializedaddfilters, and the other is to use directly+Operator. Both methods have their own characteristics, but they can achieve the same effect in many scenarios.

Core function:addThe filter performs addition operations.

addThe filter is a direct tool in Anqi CMS template used for performing addition operations. Its design takes into account multiple data types, making the addition handling of numbers and strings more flexible.

How to use: {{ 变量 | add: 值 }}

This filter can intelligently handle different types of data:

  • Add numbers:If变量andThey are all numeric types (including integers and floating-point numbers),addThe filter will perform standard arithmetic addition. For example:
    
    {# 假设 price 为 10,quantity 为 2 #}
    <p>总金额:{{ price | add:quantity }}</p>
    {# 输出: 总金额:12 #}
    
    {# 假设 decimal_price 为 10.5,tax_rate 为 0.05 #}
    <p>含税价:{{ decimal_price | add:(decimal_price * tax_rate) }}</p>
    {# 输出: 含税价:11.025 (或者根据后续浮点数处理,如 floatformat) #}
    
  • String addition of strings:If变量andare all of string type,addThe filter will perform string concatenation. For example:
    
    {# 假设 greeting 为 "Hello, ",name 为 "World" #}
    <p>{{ greeting | add:name }}</p>
    {# 输出: Hello, World #}
    
  • Addition of a number and a string (mixed types): addThe filter performs especially intelligently in this aspect. When a number is added to a string, it usually converts the number to a string and then performs concatenation. For example:
    
    {# 假设 product_id 为 123 #}
    <p>产品编号:{{ "PRO-" | add:product_id }}</p>
    {# 输出: 产品编号:PRO-123 #}
    
    {# 假设 score 为 95 #}
    <p>您的得分为:{{ score | add:"分" }}</p>
    {# 输出: 您的得分为:95分 #}
    
    It is worth noting that ifaddThe filter encounters an unhandled type when trying to convert or perform operations (such asnilornothing), it tends to ignore this part to avoid breaking the template rendering.

More general arithmetic operation tags

exceptaddThe filter, the Anqi CMS template also supports using it directly in expressions+operators for addition operations, and other basic arithmetic operations, such as subtraction (-), multiplication (*), division (/), and modulo (%)。These operators can be used directly in template expressions, providing a calculation method closer to a programming language.

How to use: {{ 表达式 + 另一个表达式 }}

  • Add numbers:This is a common means of performing pure numerical calculations. For example:
    
    {# 假设 num1 为 10,num2 为 5 #}
    <p>结果:{{ num1 + num2 }}</p>
    {# 输出: 结果:15 #}
    
  • string concatenation:In the Pongo2 template engine used in Anqi CMS,+The operator can also be used for string concatenation. For example:
    
    {# 假设 prefix 为 "订单号:",order_number 为 "2023001" #}
    <p>{{ prefix + order_number }}</p>
    {# 输出: 订单号:2023001 #}
    

When dealing with pure numbers or pure strings for addition/concatenation,addFilters and+Operators usually yield the same result. The choice depends on personal preference and specific scenario requirements, for example, if you want to handle potential null values more robustly,addThe filter may provide more predictable behavior.

Example of practical scenarios

  • Calculate the total price of the shopping cart:On the product detail page or shopping cart page, you may need to add the product price to the shipping cost.

    {# 假设 product.price 为 99.99,shipping_cost 为 10.00 #}
    <p>商品价格:{{ product.price }}</p>
    <p>运费:{{ shipping_cost }}</p>
    <p>总计:{{ (product.price + shipping_cost) | floatformat:2 }}</p>
    {# 使用 floatformat 过滤器保留两位小数,输出: 总计:109.99 #}
    
  • Display page number information:In the pagination navigation, it may be necessary to display information such as

    {# 假设 current_index 为 0 (表示第一页的索引) #}
    <p>当前第 {{ current_index | add:1 }} 页</p>
    {# 输出: 当前第 1 页 #}
    
  • Dynamically generate file names or paths:Concatenate strings to build a complete image URL or download link. “`twig {# Assuming base_url is “http://example.com/uploads/"image_name"For "my_image.jpg" #Image{# Output: <img src= “http://example.com/uploads/my_image.

Related articles

How to display the AnQiCMS page's canonical URL?

In website operation, we often encounter the situation where content appears repeatedly under different URLs.For example, a product may have multiple colors, each color generating an independent page, but their core content is the same.Or, in order to track traffic, websites may add various parameters to the URL.This "duplicate" content, although it has little impact on user experience, but it may confuse search engines, not knowing which is the "original version", thus possibly affecting the crawling efficiency and ranking of the website.

2025-11-07

How to correctly configure the pseudo-static rules in AnQiCMS to generate URLs that are friendly to users and search engines?

In website operation, a URL structure that is friendly to both users and search engines is crucial.It not only allows visitors to understand the page content at a glance, but also makes it convenient for search engines to crawl and understand your website, which directly affects the SEO performance and user experience of your website.AnQiCMS (AnQiCMS) knows this well, therefore it is built with powerful pseudo-static features, allowing you to easily customize the URL of your website.Next, we will explore how to correctly configure the pseudo-static rules in AnQiCMS to generate those beautiful and practical URLs.

2025-11-07

How to use AnQiCMS built-in TDK tags to dynamically set page SEO information?

As an experienced website operator, I am well aware of the importance of Search Engine Optimization (SEO) for website visibility and traffic growth.In SEO work, the reasonable setting of TDK (Title, Description, Keywords) tags is undoubtedly the cornerstone of the cornerstone.A clear and accurate TDK that can not only help search engines better understand the page content, but also attract users to click on the search results.

2025-11-07

How to use the Canonical URL feature of AnQiCMS to avoid content duplication display issues?

In website operations, we often encounter a tricky problem: content duplication.This does not refer to manually copying and pasting the article, but rather to the fact that the same content can be accessed through multiple different URL addresses due to various technical reasons.For search engines, this can cause confusion, as they are not sure which URL is the official version of the content, which may scatter the ranking weight of the page, reduce the efficiency of the crawler, and even affect the overall SEO performance of the website.Luckyly, AnQiCMS provides a powerful and convenient feature——Canonical

2025-11-07

How to use AnQiCMS filter to display strings centered or aligned to a specified length?

In website content display, we often need to format text to make it more beautiful and tidy in layout, especially in lists, tables, or areas that require a unified visual effect.AnQiCMS (AnQiCMS) powerful template engine provides a variety of practical filters (filters) that can help us easily achieve centered, left-aligned, or right-aligned strings and can be filled to a specified length.

2025-11-07

How to judge whether a string or array contains a specific keyword and display the result in AnQiCMS template?

When building and managing website content, we often encounter such needs: to decide whether to display a "hot" tag based on whether the article title contains a certain keyword; or check whether a certain feature is mentioned in the product description to adjust its display style.These seemingly subtle dynamic adjustments can greatly enhance the intelligence and user experience of the website.In AnQiCMS, due to its flexible Django template engine syntax, implementing such judgments is not complicated.

2025-11-07

How to calculate the number of occurrences of a specific keyword in the AnQiCMS template string or array?

In daily website operations, we often need to analyze and manage content in detail.One common requirement is to count the number of occurrences of a specific keyword in a string or array within a website template.It is very helpful to understand how to implement this feature in AnQiCMS templates, whether it is for optimizing SEO keyword density, analyzing content hotspots, or for dynamically displaying relevant information on the page.AnQiCMS benefits from its powerful and flexible Django-style template engine, providing rich built-in tags and filters, making these operations extremely simple.

2025-11-07

How to remove the specified characters from a string in the AnQiCMS template to optimize display?

In website operation, the clarity of content presentation and user experience are crucial.Sometimes, the content we enter from the background or the fields we retrieve from the database may contain some unnecessary characters, such as extra spaces, specific delimiters, or leading text that needs to be cleaned up, all of which may affect the aesthetics of the page and the readability of the content.

2025-11-07