In AnQiCMS template development, in order to better control the display logic of content, we often need to judge the 'true or false' state of a variable. At this time,yesnoFilter is a very practical tool. It can help us output different texts in a concise way based on the status of variables, even dealing with numeric and string type variables.
yesnoThe filter aims to provide a straightforward way to handle ternary states: yes, no, and uncertain (or no value).Its basic working principle is to parse the input variable into a boolean value (true or false), or identify that the variable does not have an explicit boolean state (for example, the variable is empty or does not exist).
yesnoThe basic working principle of a filter
By default,yesnoA filter outputs three states based on the Boolean interpretation of variables:
true(True): When a variable is evaluated as true, output"yes".false(False)When the variable is evaluated as false, output"no".nilor null: The variable isnil(null pointer) or other states considered 'empty', output"maybe".
It is noteworthy that the AnQiCMS template engine follows a set of 'truthy' and 'falsy' rules when handling non-boolean variables. This is particularly crucial for the evaluation of numeric and string variables:
True or False judgment of a numeric variable:
0(Zero): Any number that is0is considered a variable, whether it is an integer or a floating-point number,false.- not
0Number: Any non0numbers (including positive, negative, and decimal numbers) are consideredtrue.
boolean judgment of string variables:
- empty string
"": empty strings are considered asfalse. - non-empty stringsAny string containing content, even if it includes
"false"/"0"/"nil"text strings that include, as long as itis not emptywill be considered astrue.
- empty string
Flexible applicationyesnoFilter
The most common use of this filter is in scenarios where different prompt texts need to be displayed based on a certain status.For example, whether a product has stock, whether a user is online, or whether an article has been published, etc.
Basic usage example:
{# 假设 simple.bool_true 是一个布尔值 true #}
状态:{{ simple.bool_true|yesno }} {# 输出:yes #}
{# 假设 simple.bool_false 是一个布尔值 false #}
状态:{{ simple.bool_false|yesno }} {# 输出:no #}
{# 假设 simple.nothing 是一个空值或不存在的变量 #}
状态:{{ simple.nothing|yesno }} {# 输出:maybe #}
the judgment of numeric and string variables:
{# 数字判断 #}
库存数量:{{ 0|yesno }} {# 输出:no #}
销量:{{ 100|yesno }} {# 输出:yes #}
温度:{{ -5|yesno }} {# 输出:yes #}
{# 字符串判断 #}
用户昵称:{{ ""|yesno }} {# 输出:no #}
文章标题:{{ "AnQiCMS发布文章"|yesno }} {# 输出:yes #}
状态标记 (字符串 "false"):{{ "false"|yesno }} {# 输出:yes,因为它是一个非空字符串 #}
状态标记 (字符串 "0"):{{ "0"|yesno }} {# 输出:yes,因为它是一个非空字符串 #}
Custom output text
yesnoThe filter also supports custom output text. You can specify the output text for the three states by adding a comma-separated string after the filter, in the order of: true value text, false value text, and null value text.
Custom output example:
{# 自定义布尔值输出 #}
文章状态:{{ article.is_published|yesno:"已发布,草稿,待审核" }}
{# 自定义数字输出 #}
商品库存:{{ product.stock_count|yesno:"有库存,已售罄,数量未知" }}
{# 自定义字符串输出 #}
用户反馈:{{ user.feedback_content|yesno:"已提交,未提交,无内容" }}
{# 如果只提供两个自定义值,第三个(maybe)状态将使用第二个值 #}
是否启用:{{ config.is_enabled|yesno:"启用,禁用" }} {# 如果 config.is_enabled 为空,也会输出 "禁用" #}
Through these examples, we can seeyesnoThe filter can not only handle boolean values, but also judge and output corresponding text according to the 'true or false' rules of numbers and strings.This greatly facilitates the dynamic content display of templates, making the code more concise and expressive.When designing and developing AnQiCMS templates, flexible applicationyesnoThe filter will effectively improve your work efficiency.
Frequently Asked Questions (FAQ)
1. Why string"false"or"0"InyesnoWill be output in the filter"yes"?This is becauseyesnoThe filter only cares about whether the string is empty when judging a string. Any non-empty string, even if its content is a text indicating "false", such as"false"or numbers"0"It will be considered 'true', therefore it will output withtrueThe corresponding text (default is"yes")。Only when the string is completely empty ("") will it be considered 'false' or 'empty'.
2.yesnoHow does the filter handle numeric variables, for example0and100?For numeric variables,yesnoThe filter will convert it to boolean logic:0(whether it is an integer0or a floating-point number0.0) be consideredfalse, thus output withfalseThe corresponding text (default is"no"). Any non-zero number, whether positive, negative, or with decimal points, will be consideredtrue, output withtrueThe corresponding text (default is"yes")
3.yesnoCan the filter be used withifAre tags used to build more complex logic?
yesnoThe filter is mainly used to output different text based on the true or false state of variables, it does not directly control the logical flow of the template. If you need to make conditional judgments to render different HTML structures or code blocks, you should still use{% if ... %}Label. For example, you can first useyesnoa filter to get a text result, then in aiflabel, use a boolean variable to control display. If the variable itself is of boolean type, use it directly inifIt will be more concise when used in tags. For example,{% if article.is_published %}Than{% if article.is_published|yesno == "yes" %}More direct.yesnoMore focused on the output difference at the text level.