How to use the `add` filter for numeric addition or string concatenation in AnQi CMS template?

Calendar 👁️ 61

In daily website content display and function development, we often need to process data in a simple way, such as adding numbers or concatenating several segments of text.AnQi CMS is well aware of these needs and provides very practical template filters to simplify these operations.Today, let's take a deep dive into one of the especially convenient tools -addfilter.

Get to knowaddThe filter: a tool for adding numbers and concatenating strings

Imagine that you need to sum two numbers in a template, or you need to merge the article title and subtitle for display. In Anqi CMS,addThe filter is born for this. It's like the operator in Go language.+It can intelligently handle both addition of numbers and concatenation of strings.

No matter what your data is, an integer, floating-point number or stringaddThe filter can try to perform type conversion and execute the corresponding operation.Even if some values cannot be correctly translated or are empty, it will handle them flexibly, avoiding errors in template rendering, which greatly improves the robustness and convenience of template writing.

addHow to use the filter

UseaddThe filter is very intuitive, you just need to add it after the variable or literal value you need to process|add:Then, write down the value you want to add or concatenate immediately. The basic syntax format is as follows:

{{ 原始值|add:要添加的值 }}

Let's look at some actual examples to see how it works.

1. Addition of pure numbers

When you need to add two numbers (whether they are integers or floating-point numbers),addThe filter will give the expected mathematical result.

{# 简单数字相加 #}
{{ 5|add:2 }}        {# 输出:7 #}
{{ 10|add:40 }}      {# 输出:50 #}
{{ 3.5|add:1.2 }}    {# 输出:4.7 #}

2. Pure string concatenation

If the operands are both strings,addThe filter will naturally concatenate them, just as you expect.

{# 字符串拼接 #}
{{ "安企"|add:"CMS" }}         {# 输出:安企CMS #}
{{ "Hello "|add:"World!" }}     {# 输出:Hello World! #}

3. Mixing numbers with strings operation

addThe most convenient aspect of the filter is that it can handle mixed operations of numbers and strings. In this case, it is usually necessary to convert numbers to strings for concatenation.

{# 数字与字符串混合 #}
{{ "订单号:"|add:12345 }}    {# 输出:订单号:12345 #}
{{ 2023|add:" 年" }}         {# 输出:2023 年 #}
{{ "文章总数:"|add:100|add:" 篇" }} {# 输出:文章总数:100 篇 (可以连续使用) #}

It is noteworthy that if you try to add a string to a number and the string cannot be parsed as a number for the operation, then it will be concatenated as a string:

{{ 5|add:"CMS" }}          {# 输出:5CMS #}

4. Handle null values (nothing)

Sometimes, you might encounter some variables with empty values (for examplenothingornil) situations.addThe filter also has good fault tolerance. When encountering null values, it usually ignores the null value and returns the value of another non-null operand, or treats it as an empty string in string concatenation.

{% set nothing_value = nothing %}
{{ 5|add:nothing_value }}          {# 输出:5 (nothing_value无法有效参与数字运算) #}
{{ "前缀"|add:nothing_value }}     {# 输出:前缀 (nothing_value被视为空字符串拼接) #}

Summary

As you can see, the Anqi CMS'saddA filter is a very flexible and practical tool that simplifies common numerical calculations and text concatenation needs in templates. Whether it's building dynamic content, displaying statistics, or generating personalized copy,addFilters can help you a lot, making your template code more concise and readable. Make good use of these built-in tools to greatly improve your content operation efficiency and website display effect.


Frequently Asked Questions (FAQ)

1.addCan the filter perform subtraction, multiplication, or division operations?

addThe filter is specifically used for adding numbers or concatenating strings. If you need to perform other arithmetic operations such as subtraction, multiplication, or division, AnQi CMS provides special arithmetic operation tags (such as intag-calc.mdThere is an introduction), you can refer to the relevant documents to learn how to use these features.

2. If I want to concatenate multiple strings or numbers, I need to use them multiple times.addfilter?

Yes,addThe filter is a binary operation that can only handle two values at a time. If you need to add or concatenate three or more values continuously, you need toadduse filters in sequence. For example:{{ value1|add:value2|add:value3 }}.

3.addThe filter is prioritized to add numbers and strings together or concatenate them?

addThe filter will prioritize numerical operations when handling mixed types.Only when one of the operands is explicitly a string, or cannot be effectively converted to a number, will it convert all operands to strings for concatenation. For example,5|add:"2"It will output.7(Because "2" can be converted to a number), and5|add:"CMS"it will output5CMSBecause "CMS" cannot be converted to a number.

Related articles

How does the string case conversion filter of Anqi CMS affect strings containing Chinese or other non-English characters?

In the daily content operation of AnQi CMS, we often need to process strings in various ways, among which case conversion is one of the common requirements.AnQi CMS is developed based on Go language, its template engine supports rich filter functions, which can easily realize these operations.However, when dealing with strings containing Chinese or other non-English characters, the behavior of these case conversion filters may differ from that of handling English characters. Understanding this is very important for ensuring the correct display of content.

2025-11-07

I want to capitalize the first letter of each word in the article title, can the `title` filter of Anqi CMS template achieve this?

In website operation, we often need to format content in detail to enhance user experience and page aesthetics.Especially when dealing with article titles, if each word can be automatically capitalized, it will undoubtedly make the title look more standardized and professional.So, as a user of AnQiCMS, can we meet this need through its powerful template function?

2025-11-07

What method does the AnQi CMS template provide to convert all letters of a string to uppercase?

In website content management, unified text format is an important factor in improving user experience and website professionalism.Especially for English strings, sometimes it is necessary to convert them all to uppercase to emphasize, standardize, or display the brand.AnQiCMS provides a flexible 'Filter' function in template design, making this operation very simple and intuitive.

2025-11-07

How to convert the entire English string to lowercase in AnQi CMS template?

In Anqi CMS template design, we often need to format the displayed content to match the overall style of the website or specific display requirements.When encountering a situation where it is necessary to convert an English string to lowercase, Anqicms provides a very convenient and intuitive template filter (Filter) to help us achieve this goal.

2025-11-07

How does AnQi CMS ensure that website content is presented efficiently and elegantly to users?

## AnQi CMS: The smart choice for efficient and elegant display of website content In today's digital age, a website is not just a carrier of information, but also a window of brand image and a stage for user experience.To ensure that the content of the website can reach the target audience quickly and presented in an engaging way, efficient management and elegant presentation are indispensable.

2025-11-07

How to customize the content model to meet the needs of diverse content display?

In the field of content management, website operators often face a variety of content display needs.From traditional article publishing and news updates, to complex e-commerce product details, event registration pages, and even team member introductions or customer case displays, each type of content has its unique structure and field requirements.If relying solely on a single content format, it is not only difficult to manage efficiently, but it will also limit the flexibility and user experience of the website.

2025-11-07

How does the Anqi CMS implement seamless switching and display of website content with multilingual functionality?

In the wave of globalization, multilingual support for websites is no longer just a cherry on top, but the foundation for reaching a wider user base.For enterprises that wish to expand into the international market and serve a diverse multicultural audience, a CMS that can efficiently manage and display multilingual content is particularly important.AnQiCMS (AnQiCMS) provides very practical functions in this aspect, helping us achieve seamless switching and display of website content.

2025-11-07

How does the static URL structure affect the display and user experience of my content in search engines?

In website operation, we often encounter such situations: the same content, but the access address looks quite different.Some URLs are long and contain complex question marks and equals signs, such as `www.example.com/article.php?id=123&cat=tech; whereas some are concise and clear, like `www.example.com/tech/my-great-article.html`.

2025-11-07