In AnQi CMS template development, we often need to process data in a basic way, such as performing simple addition operations on numbers, or combining different text fragments to form new strings. To meet these common and practical needs, AnQi CMS provides a very convenient template filter -add. This is a compact but powerful tool that can help us easily add numbers and concatenate strings, making template logic more flexible.

addFilter: The magic of numerical operations and text concatenation

addThe filter is a very practical feature in Anqi CMS template, it's like the plus sign in programming languages (+The same, used for two core operations: the addition of numbers and the concatenation of strings.Its uniqueness lies in its intelligent type handling mechanism.addThe filter will try to provide the most reasonable processing result.When it can recognize all operands as numbers, it will perform numerical addition; if it contains strings or cannot recognize the operands as valid numbers, it will concatenate all operands as strings.nothingIt will gracefully ignore such content, ensuring the smooth process of template rendering.

How to useaddFilter

addThe usage of the filter is very intuitive and simple, following the general syntax of the Anqi CMS template filter:

{{ 变量 | add: 另一个变量或值 }}

Here, 变量It is the initial value you need to operate on,|The symbol indicates that you are applying a filter,addthe name of the filter, followed by the colon:the following另一个变量或值is what you want to add to the initial value.

Next, let's experience it through some specific examplesaddThe convenience of the filter.

Practical scenario examples

1. Addition of pure numbers

When you need to perform simple mathematical operations in a template, such as calculating product quantities, user points, etc.,addFilters can come in handy.

Assuming you need to add the base price of the product to the additional charges:

{{ 5|add:2 }}  {# 结果为 7 #}

Or, in a loop, add to some counter:

{% set totalCount = 0 %}
{% for item in items %}
    {% set totalCount = totalCount|add:item.quantity %}
{% endfor %}
<span>总数量:{{ totalCount }}</span>

This is much more flexible than handling it in the background code and then passing it to the template.

2. String concatenation

addThe filter also performs well when it needs to dynamically combine text, such as generating dynamic titles, URL fragments, or hints.

If you want to concatenate the website name with the page title:

{% set siteName = "安企CMS" %}
{% set pageType = "内容管理系统" %}
<span>欢迎使用{{ siteName|add:pageType }}</span> {# 结果为 "欢迎使用安企CMS内容管理系统" #}

Even strings that look like numbers are treated as strings when concatenated with another string:

{{ "安企"|add:"2" }} {# 结果为 "安企2" #}

This is very convenient when you need to display numbers as part of text.

3. Mix of numbers and string concatenation

addThe most intelligent place of the filter is that it can handle mixed operations of numbers and strings.When one of the operands is a string, the entire operation tends to be string concatenation.Numbers are automatically converted to strings and then concatenated with another string.

For example, you need to display the number of an item, but this number is of numeric type, and you need to display it in a text environment:

{% set productID = 100 %}
<span>商品编号:{{ "编号:"|add:productID }}</span> {# 结果为 "商品编号:编号:100" #}

Or vice versa, add the number to the front of the string:

{{ 5|add:"CMS" }} {# 结果为 "5CMS" #}

4. Gracefully handle null or undefined variables

In complex templates, variables may sometimes be empty (nilornothing)addThe filter will selectively ignore these empty values when encountering such situations, rather than causing errors or unnecessary output, which greatly enhances the robustness of the template.

Suppose you have a number5Try adding an undefined variablenothing:

{{ 5|add:nothing }} {# 结果为 5 #}

It won't cause an error nor output5nothingInstead, it will return directly5This makes it unnecessary to make additionalifcondition judgments, making the code more concise.

Deep understandingaddThe principle of the filter.

addThe intelligence of the filter lies in its internal type conversion logic. WhenaddWhen the filter is executed, it will try:

  1. Convert both operands to numbers.If both can be successfully converted to integers or floating-point numbers, then standard mathematical addition will be performed.
  2. If at least one operand cannot be recognized as a number (or recognized as a string), or the conversion failsthenaddThe filter will convert all available operands to strings and then perform string concatenation operations.
  3. EncounternilornothingAnd empty values will be ignored.It is as if it does not exist, which avoids unexpected output or errors due to null values.

This mechanism enablesaddThe filter can provide the expected results in most scenarios, reducing the burden on template authors to predict and convert data types.

Summary

Of Security CMSaddThe filter is a simple but powerful tool in template development.It can not only efficiently complete numerical addition and string concatenation, but also greatly improve the efficiency and robustness of template writing with its intelligent handling of mixed types and null values.Mastering this filter will give you an edge when building dynamic, highly interactive secure CMS websites, enabling smoother implementation of various content display needs.


Frequently Asked Questions (FAQ)

  1. Question:addThe filter performs addition or string concatenation first when dealing with mixed numbers and strings?Answer:addThe filter will attempt to convert all operands to numbers.If all operands can be successfully converted to numbers, it will perform numeric addition.But if any operand is recognized as a string or cannot be converted to a number, it will convert all operands to strings and then perform string concatenation.{{ 5|add:"2" }}It will output."52"instead of7, because it will"2"be recognized as a string.

  2. Can I useaddDoes the filter perform multiple operations in a row? For example{{ 1|add:2|add:3 }}.Answer: Yes, it can. Anqi CMS template filters support chained calls.{{ 1|add:2|add:3 }}It will calculate first1|add:2Get3, then3|add:3Finally get6. This is very convenient when multiple steps of addition or concatenation are needed.

  3. Ask: What will be the result if I try to add a non-numeric string (such as "hello") to a number?Answer: The result will be a string concatenation. For example,{{ 10|add:"hello" }}It will output."10hello"In this case, the number10is converted to a string"10"Then concatenated with"hello"for concatenation. This again illustratesaddthe characteristic of the filter degrading to string concatenation when it cannot perform numeric addition.