When using AnQiCMS for website content management, we often encounter scenarios where we need to make judgments and process data. Especially when the data comes from user input or external interfaces, and its type is uncertain, how to process it in the template'sifPass correctly identified in condition judgmentfloatorintegerThe number converted by the filter, which is the key to ensuring logical accuracy
AnQiCMS built-in template engine is powerful, supporting GoLang'sfloatandintegerType conversion. Understanding the behavior of these conversion filters in different situations is crucial for writing robust and reliable template logic.
Understand the 'true' and 'false' in AnQiCMS templates
In AnQiCMS template logic, like many programming languages, certain non-boolean values are implicitly considered true or false during condition checks.This feature is usually called 'truthiness'.
The judgment rules of the AnQiCMS template engine for numeric types are very intuitive:
- Integer (integer):
0It is considered "false" (false), while any non-zero integer (including positive and negative numbers) is considered "true" (true) - float number (float):
0.0It is considered "false" (false) and any non-zero floating-point number is considered 'true' (true)
This means that if you use aifnumber variable directly in a condition, for example{% if myNumber %}The system will automatically judge its truthfulness according to the above rules.
floatwithintegerThe key behavior of the filter
AnQiCMS providesfloatandintegerThese filters are used to convert variables to their corresponding numeric types. They are very simple to use:
{{ value|float }}Try to convertvalueto a floating-point number.{{ value|integer }}Try to convertvalueto an integer.
The 'magic' of these filters lies in how they handleconversion failedthe behavior when inputtingfloatorintegerWhen the value of the filter cannot be effectively converted to a number, it will not throw an error but will return aDefault zero value:
value|floatIt will return when the conversion fails0.0.value|integerIt will return when the conversion fails0.
This default behavior of returning zero is cleverly combined with the true value judgment rules of AnQiCMS template engine, providing us with a reliable conditional judgment mechanism.
Practice exercise: Ensure the accuracy of conditional judgments
Assuming we have a variable obtained from an external sourceuser_scoreIts value may be a numeric string (such as "100"), a floating-point number string (such as "99.5"), or an invalid string (such as "N/A").We want to decide whether to display certain content based on this score.
Handle valid numeric stringsIf
user_scoreIt is '100', we convert it to an integer or floating-point number:{% set score_int = user_score|integer %} {% if score_int %} <p>用户分数为有效非零值:{{ score_int }}</p> {% endif %}at this point,
score_intwill100, inifIs recognized astrueThe content will be displayed normally.Handle floating-point number conversionIf
user_scoreIs '99.5', we will convert it to a floating-point number:{% set score_float = user_score|float %} {% if score_float %} <p>用户分数为有效非零浮点值:{{ score_float }}</p> {% endif %}at this point,
score_floatwill99.5, inifIs recognized astrueThe content will be displayed normally.Handle invalid stringIf
user_scoreIs 'N/A' or unknown, we try to convert it:{% set score_int = user_score|integer %} {% if score_int %} <p>用户分数为有效非零值:{{ score_int }}</p> {% else %} <p>用户分数无效或为零。</p> {% endif %}When
user_scoreWhen 'N/A',score_intwill0(Due to conversion failure). Inifcondition,0is recognized asfalseTherefore, it will be executedelseBranch, display 'User score is invalid or zero.'Handle meaningful zero valuesIf
user_scoreIs "0" or "0.0", and this is a meaningful zero score, we still hope it is recognized as "false":{% set score_int = user_score|integer %} {% if score_int %} <p>用户分数为有效非零值:{{ score_int }}</p> {% else %} <p>用户分数是零或无效。</p> {% endif %}at this point,
score_intwill0, inifIs recognized asfalseIt will also be executed,elseBranch. This conforms to our intuitive judgment of zero.
As can be seen from the above examples,floatandintegerThe filter in AnQiCMS template not only can complete data type conversion, but also cleverly returns zero value when the conversion fails, and alsoifConditional judgments provide implicit boolean logic, making the judgment of "invalid is false" extremely concise and efficient.
Summary
In the AnQiCMS template, make sure tofloatorintegerthe converted number is inifCan be correctly identified in the conditional judgmenttrueorfalseThe core lies in understanding the following two points:
- Zero value principle: Integer
0And floating point number0.0InifConsidered in the conditionfalseAny non-zero number is consideredtrue. - Filter bottom:
floatandintegerThe filter will return by default when the conversion fails0.0or0.
Therefore, just pass the value to be judged through firstfloatorintegerThe filter converts, regardless of whether the original value is a valid numeric string or an invalid non-numeric string, the final result is always inifJudged correctly according to the zero principle in the condition. This greatly simplifies the conditional logic in the template and improves development efficiency.
Frequently Asked Questions (FAQ)
How to distinguish between a 'true zero' and a 'zero caused by conversion failure'?Under this default behavior,
0and0.0(a meaningful zero) and the one after conversion failure0and0.0Inifwill be regarded in the judgment.false. If you need to distinguish between these two cases, for example, to display 'User has no score' instead of 'Data is invalid', then you may need to first go through other ways (such as regular expressions orcontainA filter to check if the original string conforms to the numeric format.If the original string is not a number, it can be explicitly considered as "invalid data";Otherwise, it is a 'true zero'.Does this 'truthy' judgment rule also apply to strings and arrays?Yes, AnQiCMS template engine also has a similar 'truthy' judgment for other data types:
- stringAn empty string
""is treated asfalse, any non-empty string (even if the content is "0" or "false") is treated astrue. - array/slice: an empty array/slice is treated as
falseAn array/slice containing any elements is consideredtrue. These rules also help in writing concise conditional judgment logic.
- stringAn empty string
If I have a string
"0", but inifIn the condition, I hope it is considered astruebecause"0"It is a valid ID for me, how should I handle it?Use it directly in this specific case{% if some_string %}Will"0"astruebecause it is a non-empty string. But if you useintegerconvert,{% if some_string|integer %}it will be considered asfalse. If you want to convert a string"0"astrue, the best way isAvoid implicit type conversionInstead performExplicit comparisonFor example,{% if some_string == "0" %}or{% if some_string == "0" or some_string|integer > 0 %}This allows for more precise control of your judgment logic.