integerandfloatFilter is particularly important, as it helps us convert these string numbers into actual numeric types that can be used for computation.
Understanding the Problem: Why the conversion is necessary?
Imagine that you may have set a custom field named "Product Price{{ item.Price * 2 }}When performing calculations, you may find that the results are not as expected, or even that the template reports an error.This is because the template engine recognizes "99.99" as a string, not a computable number.The data stored in databases or custom fields is often like this, they require explicit type conversion to participate in mathematical operations.
Security CMS solution:integerandfloatFilter
To solve this problem, the Anqi CMS provides a pair of very practical filters:integerUsed to convert values to integers,floatUsed to convert values to floating-point numbers (i.e., decimals).
integerFilter: Convert a string to an integer
When we need to handle values like product quantity, product ID, article view count, etc., that do not require decimal parts,integerThe filter comes into play. It tries to convert the input value to an integer.
Example of use cases:Suppose we are taking a fielditem.ViewsThe browsing volume obtained is the string "12345", and we want to perform addition or comparison operations on it in the template.
Code example and its output:
{# 假设 item.Views 的值为字符串 "12345" #}
<div>今天的浏览量是:{{ item.Views|integer }}</div>
<div>明天预期的浏览量是:{{ item.Views|integer + 100 }}</div>
{# 假设 item.Id 的值为字符串 "5" #}
{% if item.Id|integer > 3 %}
<div>这是一个热门商品!</div>
{% endif %}
Output example:
今天的浏览量是:12345
明天预期的浏览量是:12445
这是一个热门商品!
IfintegerThe filter tries to convert a completely non-numeric string (e.g., "foobar" or a null valuenothing) It will convert it to an integer0.5.
floatFilter: Convert a string to a floating-point number
When dealing with numerical values such as product prices, discount percentages, and so on that require decimal places,floata filter is the ideal choice. It converts the input value to a floating-point number.
Example of use cases:If we have a custom fielditem.Pricestored as a string "99.99", oritem.Discountstored as a string "0.8".
Code example and its output:
{# 假设 item.Price 为字符串 "99.99",item.Discount 为字符串 "0.8" #}
<div>商品原价:{{ item.Price|float }}</div>
<div>打折后的价格:{{ item.Price|float * item.Discount|float }}</div>
{# 假设 item.Weight 为字符串 "1.5" #}
{% if item.Weight|float < 2.0 %}
<div>这是轻量级商品。</div>
{% endif %}
Output example:
商品原价:99.99
打折后的价格:79.992
这是轻量级商品。
Withintegersimilarly, iffloatFilter attempts to convert a non-numeric string, it will return a floating-point number0.0. Input of numeric type will also be correctly converted to a floating-point number. It is worth noting that,float过滤器可以与integercombined with, for example,{{ "5.6"|float|integer }}Will first convert '5.6' to the floating-point number 5.6, and then to the integer 5.
Actual application: Make your template come to life.
MasteredintegerandfloatAfter filtering, you can make more complex logical judgments and numerical calculations in the AnQi CMS template.
For example, display prices with units:
{# 假设 item.Price 为字符串 "120.50",item.Quantity 为字符串 "3" #}
{% set total_price = item.Price|float * item.Quantity|integer %}
<div>
商品名称:{{ item.Title }}<br>
单价:¥{{ item.Price|float|floatformat:2 }}<br>
数量:{{ item.Quantity|integer }}件<br>
总价:¥{{ total_price|floatformat:2 }}
</div>
Pass|floatformat:2Filter, we can also format floating-point numbers to retain two decimal places.
Precautions
AlthoughintegerandfloatThe filter provides convenient type conversion functions, but we also need to pay attention to the following points when using it:
- Data source reliabilityMake sure that the string you are trying to convert is indeed a number, even if it contains non-numeric characters, you should also expect that it will be converted
0. - chained usageThese filters can be chained together, for example
{{ "100"|integer + 20|float }}They will be executed in order from left to right. - Default behaviorUnderstand the default return value when conversion fails (for example, converting "abc" to an integer), which helps you handle it accordingly in the template and avoid unexpected situations.
By using flexibilityintegerandfloatFilter, your CMS template will become more powerful and dynamic, able to easily handle various numerical requirements, making the website functions more perfect.
Common Questions (FAQ)
Q1: If the string contains non-numeric characters,integerorfloathow will the filter handle it?A1: If the string contains non-numeric characters (for example, "abcintegerThe filter will convert it to,0,floatThe filter will convert it to,0.0This means that even if part is a number, if there is a non-numeric character, the entire conversion will fail and return the default value.
Q2: Can I directly useifused in statements.integerorfloatFilter?A2: Okay. When you are inifWhen these filters are used in a statement, they convert the string to the corresponding numeric type.ifThe statement will compare or judge based on the converted numeric value. For example,{% if item.Stock|integer < 10 %}.
Q3:integerandfloatCan the numerical value after filter conversion be directly calculated with other numbers?A3: Yes, once a string has passed throughintegerorfloatThe filter successfully converts to a numeric type, which is completely equivalent to other numbers in the template and can perform all mathematical operations such as addition, subtraction, multiplication, and division. It can also be mixed with values that are already numeric without conversion.