In the template development of Anqi CMS, we often encounter situations where we need to compare data such as product inventory quantity. These numbers may be stored in the content model as strings, for example, a custom field in a product modelStock可能存储着”100”、”50”甚至是”0.5”这样的文本。However, when performing numerical comparisons, simple string comparisons do not meet our needs. For example, the string '10' is smaller than the string '2' in string comparison (because '1' is smaller than '2'), which is obviously inconsistent with the actual numerical size.

To solve this problem, we need to explicitly convert these string representations of quantities to integers or floating-point numbers before comparing them.The template engine of Anqi CMS provides a series of built-in filters, including utility tools for type conversion.

When you want to convert the stock quantity (usually a string) to an integer for comparison, the most direct and recommended filter isinteger.

UseintegerFilter for type conversion

integerThe filter can attempt to convert a string to an integer. Its usage is very concise, you just need to add it after the variable you want to convert.|integer.

For example, assume that there is a document variable in the product detail page of your productarchiveand its inventory field isarchive.StockAnd the stored value is "100". If you want to check if the inventory is greater than 0, you can use it like thisintegerFilter:

{% if archive.Stock|integer > 0 %}
    <p>当前库存:{{ archive.Stock }} 件,有货!</p>
{% else %}
    <p>库存紧张或已售罄。</p>
{% endif %}

In this example,archive.Stock|integerThe string "100" will be converted to the integer 100, and then a normal numerical comparison will be performed with the number 0. This ensures the accuracy of the comparison result.

It is worth noting that ifintegerThe string to be converted by the filter is not a valid integer (for example, it contains letters or decimals, such as "Sold Out" or "0.5"), then it will return0This feature is very useful in many cases. For example, ifarchive.Stockthe value is "Sold Out", afterintegerfiltering by the filter it will become0, so that in{% if ... > 0 %}The judgment, it will not be considered as having inventory, and will be automatically categorized into the situation of "inventory tight or sold out."

Consider the floating-point number situation

If the quantity of your inventory may include decimals, such as '0.5' (indicating half a kilogram or half a case), thenintegerThe filter will truncate the decimal part directly (for example, '0.5' will become 0). In this case, you should usefloatFilter.floatThe filter will convert the string to a floating-point number, allowing you to perform precise decimal comparisons.

{% if archive.Stock|float > 0.0 %}
    <p>当前库存:{{ archive.Stock }} kg,有货!</p>
{% else %}
    <p>库存紧张或已售罄。</p>
{% endif %}

Withintegersimilarly, iffloatThe string that the filter tries to convert is not a valid floating-point number, it will return0.0.

By making reasonable use ofintegerorfloatFilter, you can easily perform accurate numeric comparisons on string-formatted inventory quantities in the Anqi CMS template, thereby achieving more flexible and intelligent content display logic.

Common Questions (FAQ)

1. If my inventory quantity may contain decimals (for example, '0.5 kilograms'), which filter should I use?

If the quantity of your inventory may include decimals, to ensure the accuracy of the comparison, you should usefloatFilter.integerThe filter will truncate the decimal part, which may lead to inaccurate judgments (for example, “0.5” will be converted to0)。UsefloatFilter, for example{{ archive.Weight|float > 0.0 }}It can correctly handle decimal numbers.

2. Ifarchive.Stock字段存储的是 “Sold Out” 这样的非数字字符串,会发生什么?

WhenintegerorfloatFilter attempts to convert a non-numeric string (such as "Sold Out") and will return0or0.0. This means that in such as{% if archive.Stock|integer > 0 %}In such comparisons, "Sold Out" will be considered as out of stock and will triggerelsethe logic of branches, which is usually the expected behavior.

3. I can putintegerorfloatDo filters work with other filters?

Yes, the filters of Anqi CMS support chained calls. You canintegerorfloatFilter combined with other filters. For example, you may use it first.replaceFilter removes non-numeric characters from the number string, then use.integerFor conversion:{% set cleaned_stock = archive.Stock|replace:"件," %} {% if cleaned_stock|integer > 0 %}But please ensure the sequence logic of chained calls is correct to avoid unexpected results.