In Anqi CMS template development, we sometimes encounter situations where we need to compare data such as product inventory quantities. These quantities may be stored in the content model as strings, for example, a custom field of a product modelStockMay contain text such as '100', '50', or even '0.5'.However, simple string comparison does not meet our needs when performing numerical comparisons, for example, the string "10" will be smaller than the string "2" in string comparison (because '1' is smaller than '2'), which is obviously inconsistent with the actual size of the number.

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

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

UseintegerThe filter is used for type conversion

integerThe filter attempts to convert a string to an integer. It uses a very concise syntax, you just need to add it to the variable you want to convert.|integerJust do it.

For example, suppose there is a document variable on your product detail pagearchiveThe stock 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 this.integerFilter:

{% 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 an 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 filter tries to convert a string that 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, it will become0so in{% if ... > 0 %}In the judgment, it will not be considered as having inventory, thus automatically categorized into the situation of "Inventory shortage or sold out."

Consider the floating-point number situation

If your stock quantity may contain decimals, such as "0.5" (indicating half a kilogram or half a box), 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, if:floatThe filter tries to convert a string that is not a valid floating-point number, and it will return0.0.

By reasonable applicationintegerorfloatFilter, you can easily make accurate numeric comparisons of string-form inventory quantities in Anqi CMS templates, thereby realizing more flexible and intelligent content display logic.

Frequently Asked Questions (FAQ)

1. If my inventory quantity may contain decimals (such as "0.5 kg"), which filter should I use?

If your inventory quantity may contain 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 }}and can correctly handle decimal numbers.

2. Ifarchive.StockWhat happens if the field stores a non-numeric string like 'Sold Out'?

WhenintegerorfloatThe filter attempts to convert a non-numeric string (such as "Sold Out") and returns0or0.0. This means that in cases such as{% if archive.Stock|integer > 0 %}In such a comparison, "Sold Out" is considered to have no stock and triggerelseBranch logic, which is usually the expected behavior

3. I canintegerorfloatfilters with other filters?

Yes, AnQi CMS filters support chained calls. You can useintegerorfloatfilters in combination with other filters. For example, you might first usereplaceThe filter removes non-numeric characters from the quantity string and then usesintegerPerform the conversion:{% set cleaned_stock = archive.Stock|replace:"件," %} {% if cleaned_stock|integer > 0 %}But make sure the order of the chained calls is correct to avoid unexpected results.