How to concatenate or add two strings or numbers in AnQiCMS template?

Calendar 👁️ 71

In AnQi CMS template design, dynamically combining text information or performing calculations on numerical values is a common requirement.It is crucial to handle string concatenation and numerical addition operations flexibly, whether it is to build personalized product descriptions or to display dynamic data on the page.The Anqi CMS template system draws on the syntax of the Django template engine, providing a variety of intuitive and powerful methods to complete these tasks.

Core operation:addFilter, considering concatenation and addition

In Anqi CMS template, the most common and convenient way to handle string concatenation and numeric addition is to useaddFilter. This filter is very smart, it can automatically judge whether to perform numerical addition or string concatenation based on the data type you provide.

1. Add numbers

When you use two numbersaddWhen the filter is applied, it performs standard mathematical addition. Even if one of the numbers is in string format, as long as it can be converted to a valid number,addThe filter will also try to convert it to a number before adding.

For example, if you have two variablesitem.Price(Numeric, such as100) ortax(Numeric, such as20) you can calculate their sum like this:

{% set itemPrice = 100 %}
{% set tax = 20 %}
<p>商品含税总价:{{ itemPrice|add:tax }}</p>
{# 输出:商品含税总价:120 #}

Even if the tax amount is defined as a string, as long as the content is purely numeric,addthe filter can also handle:

{% set itemPrice = 100 %}
{% set taxString = "20" %}
<p>商品含税总价:{{ itemPrice|add:taxString }}</p>
{# 输出:商品含税总价:120 #}

2. String concatenation

When you use a filter on two strings,addit will concatenate them directly.

For example, you want to concatenate a product prefix and product ID to form a product code:

{% set productPrefix = "PROD-" %}
{% set productId = "12345" %}
<p>产品代码:{{ productPrefix|add:productId }}</p>
{# 输出:产品代码:PROD-12345 #}

3. Mixed type processing

addThe most convenient aspect of the filter is that it can intelligently handle mixed operations of numbers and strings.If an operand is a number and the other is a string, it usually converts the number to a string and then performs string concatenation.

Suppose you want to display the number of times an article has been viewed and add the unit "views" at the end:

{% set archiveViews = 520 %}
{% set unit = " 次浏览" %}
<p>本文已被浏览:{{ archiveViews|add:unit }}</p>
{# 输出:本文已被浏览:520 次浏览 #}

4. Handling of empty values (nothing)

IfaddAn operand of the filter is an empty value (for examplenilOr undefined variables), it will try to ignore empty values and only use another valid value.

{% set validNumber = 10 %}
{% set emptyValue = nothing %} {# 假设 nothing 是一个空值 #}
<p>计算结果:{{ validNumber|add:emptyValue }}</p>
{# 输出:计算结果:10 #}

The following is an example of containing a variety ofaddAn integrated example of filter usage, you can refer to it to understand its flexibility:

{% set num1 = 5 %}
{% set num2 = 2 %}
{% set str1 = "安企" %}
{% set str2 = "CMS" %}
{% set numStr = "2" %}
{% set emptyVal = nothing %}

<p>数字相加:{{ num1|add:num2 }}</p> {# 5 + 2 = 7 #}
<p>数字与字符串数字相加:{{ num1|add:numStr }}</p> {# 5 + "2" = 7 #}
<p>字符串拼接:{{ str1|add:str2 }}</p> {# 安企 + CMS = 安企CMS #}
<p>数字与字符串拼接:{{ num1|add:str2 }}</p> {# 5 + CMS = 5CMS #}
<p>字符串与数字拼接:{{ str1|add:num1 }}</p> {# 安企 + 5 = 安企5 #}
<p>与空值相加(数字):{{ num1|add:emptyVal }}</p> {# 5 + nothing = 5 #}
<p>与空值拼接(字符串):{{ str1|add:emptyVal }}</p> {# 安企 + nothing = 安企 #}

Operations of concatenation and addition in more scenarios

AlthoughaddThe filter is very powerful, but you may also need other tools in certain specific scenarios:

1. Pure numerical calculations: Arithmetic operation label

If your calculation only involves numbers and requires the execution of more complex mathematical expressions such as subtraction, multiplication, and division, Anqi CMS supports direct arithmetic operations within double curly braces (following the Go language's syntax)text/templateStyle).

For example, calculate the difference or product:

{% set valA = 100 %}
{% set valB = 50 %}
<p>减法:{{ valA - valB }}</p> {# 输出:50 #}
<p>乘法:{{ valA * valB }}</p> {# 输出:5000 #}
<p>除法:{{ valA / valB }}</p> {# 输出:2 #}

2. Refine the output:stringformatFilter

After you complete the concatenation or addition operation, if you need to output the result in a specific format, such as controlling decimal places, adding prefixes and suffixes, and so on,stringformatThe filter is very useful. It can format various types of data into strings according to a specified format template.

For example, format a floating-point number into a string with two decimal places:

{% set price = 12.3456 %}
<p>格式化价格:{{ price|stringformat:"%.2f" }} 元</p> {# 输出:格式化价格:12.35 元 #}

3. Aggregate of list items:joinFilter

If you have an array (or list) and you want to concatenate all its elements into a single string,joinThe filter is your ideal choice. It allows you to specify a delimiter to connect all items in the array.

For example, connect a tag list into a string:

{% set tags = ["CMS", "模板", "Go"] %}
<p>所有标签:{{ tags|join:", " }}</p> {# 输出:所有标签:CMS, 模板, Go #}

Summary

MasteraddThe filter allows you to handle text concatenation and numerical calculations more flexibly in the Anqi CMS template. At the same time, combined算术运算标签to perform pure numerical operations, as well asstringformatandjoinFiltering to perform

Related articles

How to determine if a number (such as article ID) can be evenly divided by a specific number in the AnQiCMS template?

In website content display, we often encounter some special requirements, such as wanting to apply different styles to specific elements in the list, or to make some distinctions based on the parity of the article ID.AnQiCMS is a powerful template engine, drawing inspiration from the excellent design of Django templates, and provides a simple and efficient way to handle these logic.Today, let's discuss a very practical feature in the template: how to determine if a certain number (such as the article ID) can be evenly divided by a specific number.

2025-11-07

How to concatenate array elements obtained by traversing AnQiCMS templates with a specified separator into a string?

During the template creation process in Anqi CMS, we often encounter the need to concatenate a certain field from an array (or list) queried from the database using a specific symbol to form a continuous string for beautiful display on the page, such as connecting multiple tags (Tag) of an article or displaying all the characteristics of a product.The Anq CMS template engine supports syntax similar to Django templates, making it intuitive and flexible to handle such requirements.The core idea is to use the loop structure of the template engine to traverse the array

2025-11-07

How to quickly split a comma-separated string into an array for traversal in AnQiCMS template?

In website content operation, we often encounter such situations: a content field stores a series of interconnected information, usually connected by commas.For example, an article may be associated with multiple tags ("SEO, website optimization, content marketing"), a product may have various color options ("red, blue, green"), or you may need to display a set of user-defined keywords.How to efficiently convert the comma-separated strings to traversable data structures when we need to display them one by one on the front-end page or perform more complex processing

2025-11-07

Why does the AnQiCMS template default to escaping HTML code? How can HTML content be safely output?

When using AnQiCMS for template development, we may notice an interesting phenomenon: sometimes, the HTML code directly output in the template, such as a `<div>` tag, is not parsed by the browser into a visible area as expected, but is displayed exactly as it is, showing `&lt;div &gt; `such characters. This may be confusing, why does AnQiCMS default to escaping HTML code?How can we safely output the HTML content we want?--- ### One

2025-11-07

How to implement the `replace` filter of AnQiCMS for batch replacement of sensitive words in article content?

In the field of content management, the flexibility and maintainability of website content are crucial.Batch replacement of article content is an efficient and practical operation, whether it is for brand unification, information update, or sensitive word filtering.AnQiCMS as a rich-featured enterprise-level content management system, provides a variety of content processing mechanisms, among which the `replace` filter and the background content batch replacement feature play a key role in different scenarios.

2025-11-07

How to automatically render the multi-line text (including newline characters) of AnQiCMS custom fields into HTML tags `<p>` and `<br/>`?

When using AnQiCMS to manage website content, we often take advantage of its powerful custom field features to enrich page information.Especially for text that needs to include multiline descriptions, notes, or detailed explanations, the 'Multiline Text' type in custom fields is undoubtedly the ideal choice.However, when we eagerly display these text contents with line breaks on the front-end page, we may find that the original line breaks are not automatically recognized by the browser, resulting in all the text being cramped together, greatly reducing the reading experience.This is actually a feature of HTML rendering mechanism

2025-11-07

How to automatically add line numbers to plain text content entered in the background of AnQiCMS template?

When operating a website, we often encounter such a need: we hope that the pure text content entered in the background can automatically display line numbers on the front end, which is very helpful for displaying code, poetry, configuration scripts, or any text content that needs to be read line by line.Although AnQiCMS back-end content entry is very convenient, how can these plain text contents be automatically numbered with beautiful line numbers when displayed on the front end?The flexible template engine of Anqi CMS can help us easily achieve this.### Understanding the Source and Processing Basics Firstly, we need to clarify the source of this plain text content

2025-11-07

How to print the complete type and value of a variable during the development and debugging of AnQiCMS templates for problem troubleshooting?

During the development and debugging of AnQiCMS templates, we often encounter such situations: the page displays incorrectly, or some data is not displayed as expected.At this time, we hope to 'penetrate' the variables in the template and understand what types they are and what specific values they contain.In fact, only by mastering the true state of variables can one accurately locate the problem.AnQiCMS's template system adopts syntax similar to Django template engine, which makes variable output and logical control intuitive.In templates, we usually use double curly braces `{{ variable name

2025-11-07