An efficient and feature-rich enterprise-level content management system, AnQi CMS provides flexible data processing capabilities.During the template development process, making good use of various filters can help us accurately control the display format of content.addEspecially its specific performance in handling numeric string types, which is often a place where users are likely to have doubts.

addFilter: Overview of Features

addFilter is mainly used to 'add' two values in the template context of Anqi CMS.The meaning of this 'addition' is determined by the type of the operands: when the operands are pure numbers, it will perform arithmetic addition; when the operands are strings, it tends to perform string concatenation.This design aims to simplify common data merging or simple calculation tasks in templates.

核心问题:数字型字符串相加的真相

许多用户会好奇,如果我将两个包含数字的字符串(例如"123"and"456") toadd过滤器,它会像数学运算一样得到779, or concatenate strings to get123456?

according to the instructions in the Anqi CMS document,addThe filter will perform actions when processing stringsconcatenation operationThis means that even if the content of the string is purely numeric, the system will treat it as text and concatenate it directly.

Therefore, when you use it in the template like this:

{{ "123"|add:"456" }}

The displayed result will be:123456

This is similar to the string concatenation operator in Go language+The behavior is very similar, i.e., string concatenation takes precedence

Deep understandingaddFilter behavior

To understand more comprehensivelyaddFilter, let's look at some examples under different combinations of operands:

  1. Add numbers to numbers:When both operands are of numeric type,addThe filter will perform standard arithmetic addition.

    {{ 5|add:2 }}
    

    Show results:7

  2. Addition of a number and a non-numeric string:When one operand is a number and the other is a non-numeric string,addthe filter will convert the number to a string and then concatenate.

    {{ 5|add:"CMS" }}
    

    Show results:5CMS

  3. The addition of strings (including numeric strings):Whether the content of the string is purely numeric or not, as long as the operand is a string type,addthe filter will perform string concatenation.

    {{ "安企"|add:"CMS" }}  {# 两个普通字符串 #}
    {{ "安企"|add:"2" }}    {# 字符串与数字型字符串 #}
    {{ "123"|add:"456" }}   {# 两个数字型字符串 #}
    

    Show results:安企CMS 安企2 123456

  4. Interaction with empty value (nothing/nil): addThe filter will try to ignore these contents when encountering unrecognizable or invalid types, and return the result of the valid operation.

    {{ 5|add:nothing }}
    

    Show results:5

Why is there such a design?

The template engine is usually intended to provide flexibility in content display, rather than complex programming logic.Under this design concept, the default handling of "data" often tends to display or combine it as "textConcatenate strings by default instead of performing type coercion for arithmetic operations can effectively avoid template rendering errors caused by uncertain data types (such as the user inputting a non-numeric string).This behavior pattern simplifies the mental burden of template writers, reduces unexpected behavior caused by implicit type conversions, making the template more robust and easier to maintain.

Recommended for actual application

In the template development of AnQi CMS, understandaddThis feature of the filter is crucial. If you need to perform mathematical addition operations on the template,numeric stringonly rely onaddThe filter is not enough. You should consider the following strategies:

  • Backend preprocessing:Before the data is transmitted to the template, in the Go language backend logic, the strings that need to be subjected to arithmetic operations are converted to their actual numeric types.So, the template receives numbers that can be directly used for mathematical calculations.
  • Explore custom filters:If the built-in filter of the system cannot meet the requirements and you have a certain understanding of the Go language, you can consider developing a custom template filter for AnQiCMS. This filter is specifically responsible for converting strings to numbers and then performing arithmetic operations.
  • Using the existing conversion filter:Refer to the documentation更多过滤器(such asinteger/float)usage to see if they can help you safely convert strings to numbers in the template. For example:{{ "123"|integer|add:("456"|integer) }}If the filter can be successfully converted, this combination may achieve the expected result.

Summary

addThe filter in the security CMS template is an intelligent judgment tool based on the type of the operand.It performs arithmetic addition when dealing with pure numbers, but when dealing with strings, even if the string content is a number, it will perform string concatenation.Understanding this core behavior clearly can help you control data display more accurately when building a website, avoiding unnecessary confusion and errors.


Common Questions (FAQ)

Q1: If I really want to add two numeric string (such as "123" and "456") to get a numeric result779What methods are there in the Anqi CMS template?

A1: Currently the CMS template that comes with the security systemaddThe filter will concatenate numeric strings.If you indeed need to perform such arithmetic operations, the safest method is to convert these string data to numeric types in advance within the backend logic (Go code), and then pass them to the template.integerorfloat)Performing chain operations, for example{{ "123"|integer|add:("456"|integer) }}But the prerequisite is that these conversion filters can successfully parse strings into numbers. If needed frequently, you can also consider developing a custom template filter.

Q2:addFilters and Go language's+Do the operator behaviors match completely?

A2: When handling mixed string and numeric calculations, in the safe CMS template ofaddThe filter behavior matches that of Go language's+Operator (string concatenation) very