How to perform the mixed addition of numbers and strings in the AnQi CMS template?

Calendar 👁️ 77

In the process of managing and displaying website content, we often encounter situations where we need to combine different types of data.For example, concatenate a number with a specific prefix, or automatically add a unit when displaying the quantity of products.The template system of AnQiCMS (AnQiCMS) provides us with flexible and powerful functions to handle such requirements, especially for the operation of adding numbers and strings mixed together, based on the Go language and compatible with the Django template engine syntax.

Mixed addition of numbers and strings in AnQiCMS template

When we need to mix numbers and strings in templates for operations, such as combining the list number with the article title, or displaying the calculation result with the unit, Anqi CMS provides intuitive and powerful tools to achieve this.

Core Tool:addFilter

In AnQi CMS template syntax,addThe filter is the key to performing the mixed addition of numbers and strings.This filter is designed to be very intelligent, it can automatically judge whether to perform addition on mathematical operands or to concatenate strings based on the specific type of the operands.

addHow to use the filter

addThe syntax of the filter is very concise:{{ 变量A|add:变量B }}.变量Ais the initial value you want to operate on,变量Bis the value you want to add or concatenate.

Let's understand its behavior through some specific examples:

  • Pure numbers additionIf变量Aand变量BAll of them can be parsed as numbers,addThe filter will perform mathematical addition.

    {{ 5|add:2 }}  {# 显示结果: 7 #}
    {{ 5|add:40 }} {# 显示结果: 47 #}
    
  • Concatenation of numbers with stringsWhen one operand is a number and the other is a string that cannot be parsed as a pure number,addThe filter performs string concatenation. It automatically converts numbers to strings and then concatenates them with another string.

    {{ 5|add:"CMS" }} {# 显示结果: 5CMS #}
    
  • String concatenation with string: If both operands are strings,addthe filter will concatenate them directly.

    {{ "安企"|add:"CMS" }} {# 显示结果: 安企CMS #}
    {{ "安企"|add:"2" }}   {# 显示结果: 安企2 (这里“2”被视为字符串,因为“安企”不是数字) #}
    

Actual application example

in the actual display of website content,addThe filter can be very useful. For example, when looping through a list of articles, we may want to add a unique identifier based on the loop index to the CSS class name of each article, or add a serial number before the title:

{% archiveList archives with type="page" limit="5" %}
    {% for item in archives %}
        <div class="article-item item-{{ forloop.Counter|add:100 }}"> {# 将循环计数器加上100,作为类名后缀 #}
            <h3>第{{ forloop.Counter|add:"篇" }}文章:{{ item.Title }}</h3> {# 将循环计数器与文本拼接 #}
            <p>{{ item.Description }}</p>
        </div>
    {% endfor %}
{% endarchiveList %}

In this example,forloop.Counteris a number, passedadd:100implemented number addition, generated likeitem-101such a class name; passedadd:"篇"implemented the concatenation of numbers and strings, generated第1篇文章such a title.

Not justadd: Other arithmetic operations

In addition to the flexible mixed addition operation, the AnQiCMS template also supports pure number arithmetic operations. When you are sure that the operands are all numbers, you can use them directly in the template.+(add),-(subtract),*(multiply),/(divide) operators. They are usually used in simple numerical calculations or conditional judgments.

For example:

{% set price = 100 %}
{% set discount = 20 %}
{% set finalPrice = price - discount %} {# 纯数字减法 #}
<p>原价:{{ price }}元,折扣:{{ discount }}元,实际支付:{{ finalPrice }}元</p>

{% set quantity = 5 %}
{% set unitPrice = 15.5 %}
{% set totalCost = quantity * unitPrice %} {# 纯数字乘法 #}
<p>购买数量:{{ quantity }},单价:{{ unitPrice }}元,总费用:{{ totalCost }}元</p>

These operators are mainly used for calculations between pure numbers, and if it involves strings and you want to get the concatenation result, thenaddThe filter is a safer and clearer choice.

Summary

The Anqi CMS template system passedaddThe filter provides a powerful and flexible ability to mix numbers and strings, whether it is generating dynamic CSS class names, concatenating text with numbers, or combining values with units, it can handle it easily.At the same time, for operations on pure numbers, you can also use basic arithmetic symbols directly.Understand and make good use of these template functions, which will greatly improve the efficiency of dynamic content generation on your website and the flexibility of template development.


Frequently Asked Questions (FAQ)

  1. addCan the filter perform mathematical addition on two strings representing numbers? For example,"5"|add:"2"The result is"52"Or7?Answer: According to the behavior of the AnQiCMS template engine, if both operands are strings that can be parsed as pure numbers (such as"5"and"2")addThe filter will first try to perform mathematical addition, therefore{{ "5"|add:"2" }}The result is7. Only when one of the operands is clearly not parseable as a number (for example"安企"), it will fallback to string concatenation mode.

  2. except|add:Filter, can I use it directly?+Can I add numbers and strings together using the symbol?Answer: It is not recommended to use it directly.+Symbols for adding numbers and strings together. In AnQiCMS templates,+The operator is mainly used for mathematical addition between pure numbers. If strings are involved, it is strongly recommended to use a dedicated|add:Filter to handle mixed concatenation or addition operations of numbers and strings.

  3. If I need to repeat a string or number multiple times without adding or concatenating, which filter should I use?Answer: If you need to repeat a string or number multiple times, for example, to repeat“安企CMS”repeated5times, you should userepeatfilter. Its usage is{{ "安企CMS"|repeat:5 }}, which will output安企CMS安企CMS安企CMS安企CMS安企CMS.

Related articles

What are the specific application scenarios of the `phone2numeric` filter in AnQiCMS?

In AnQiCMS template engine, there are many powerful filters built-in, which can help us process and display data in a flexible manner.Among them, the `phone2numeric` filter is a tool that is not widely used but can play a unique role in specific scenarios.It mainly functions to convert the corresponding letters on the mobile phone number keypad to numbers.To understand the application scenario of `phone2numeric`, we first need to understand how it works.

2025-11-08

How to convert letters on the mobile phone number keypad to numbers in the AnQiCMS template?

In AnQiCMS template development, we sometimes encounter the need to convert letters on the mobile phone number keypad to corresponding numbers.For example, a contact phone number may be recorded as “1-800-CALL-NOW”, or a product code may contain letters based on the phone keypad, and we would like to standardize it to pure numeric form for display or processing.Luckyly, AnQiCMS provides a very practical template filter `phone2numeric` to elegantly solve this problem.

2025-11-08

How to use the `Ld` tag in Anqi CMS template to customize structured data for optimizing search results display?

In today's highly competitive online environment, ensuring that website content is better understood by search engines is the key to improving visibility.Structured data, especially Json-LD format, is the important way we communicate with search engines, which can help search engines accurately identify page content and may be displayed as rich media summaries (Rich Snippets) in search results, thereby attracting more clicks.

2025-11-08

How does AnQi CMS ensure the quick display of content under high traffic conditions by using the high concurrency features of the Go language?

How Go language high concurrency technology creates ultra-fast content presentation?In today's internet environment, the speed of website access directly affects user experience and the success or failure of business.Especially during peak traffic periods, how to ensure that website content can be displayed quickly and stably is a major concern for every website operator.AnQiCMS (AnQiCMS) is an content management system developed based on the Go language, which shows unique advantages in this aspect.It cleverly utilizes the inherent high concurrency characteristics of the Go language, combining a series of optimization strategies

2025-11-08

How to handle type conversion failure when using the `add` filter in AnQiCMS templates?

When using the AnQiCMS template for content display and data processing, we often use various filters to conveniently handle data.Among them, the `add` filter is favored by many users for its flexible ability to add mixed types.It can not only perform arithmetic addition of numbers, but also cleverly realize the concatenation of strings.However, when dealing with the addition of mixed types, especially in scenarios where type conversion may fail, understanding how the `add` filter handles it is crucial for ensuring the stability and accuracy of the template output.

2025-11-08

What characters are escaped by the `addslashes` filter of AnQiCMS?

In the template development of Anqi CMS, we often need to handle various data, some of which may contain special characters, and directly outputting them to the page may cause display anomalies or parsing errors.At this time, the `addslashes` filter comes into play, which helps us preprocess these special characters to ensure the correct display of content. In particular, what characters does the `addslashes` filter escape?

2025-11-08

How to protect user input through the `addslashes` filter in AnQiCMS template to prevent potential security issues?

In website operation, ensuring the safety of user input content is always one of the core considerations.Any unprocessed user input may become a potential security vulnerability, ranging from destroying the page layout to triggering cross-site scripting (XSS) attacks, harming website visitors.AnQiCMS (AnQiCMS) is a system that emphasizes security and provides various tools to help us meet these challenges, among which the `addslashes` filter in the template is a practical feature.###

2025-11-08

How to capitalize the first letter of an English string in AnQiCMS template?

It is crucial to ensure consistency and beauty in the display of text in website content operation.Whether it is user-submitted data, content imported from the outside, or information dynamically generated within the system, we often need to standardize the case of English strings, such as capitalizing the first letter of the title or converting labels to lowercase.AnQiCMS provides a flexible template engine, through built-in filters (Filters), we can easily achieve these case conversion requirements.AnQiCMS uses a template engine syntax similar to Django

2025-11-08