In AnQiCMS template development, in order to better control the display logic of content, we often need to judge the 'true or false' status of a variable.yesnoA filter is a very practical tool. It can help us output different texts in a concise way based on the status of variables, and it can even handle numeric and string type variables.

yesno

yesnoThe basic working principle of a filter

By default,yesnoThe filter will output three states based on the boolean interpretation of variables:

  • true(True): Output when the variable is evaluated as true"yes".
  • false(False): Output when the variable is evaluated as false"no".
  • nilOr empty value: Variable isnil(null pointer) or other states considered as "empty" when output"maybe".

It is worth noting that the AnQiCMS template engine follows a set of 'truthy' and 'falsy' rules when handling non-boolean variables. This is especially crucial for the judgment of numeric and string variables:

  • The truth value judgment of a numeric variable:

    • 0(Zero): Any number of0is considered asfalse.
    • Not0Number: Any non0The number (including positive numbers, negative numbers, and decimals) will be consideredtrue.
  • Boolean judgment of a string variable:

    • Empty string"": An empty string is considered asfalse.
    • Non-empty string:Any string containing content, even one that contains"false"/"0"/"nil"text such as this, as long as itis not empty, will be consideredtrue.

Use flexiblyyesnoFilter

The most common use of this filter is in the scenario where it is necessary to display different prompt texts based on some status.For example, whether a product has stock, whether a user is online, or whether an article has been published, and so on.

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 #}

for 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: text for true value, text for false value, text for null value.

Custom output example:

{# 自定义布尔值输出 #}
文章状态:{{ article.is_published|yesno:"已发布,草稿,待审核" }}

{# 自定义数字输出 #}
商品库存:{{ product.stock_count|yesno:"有库存,已售罄,数量未知" }}

{# 自定义字符串输出 #}
用户反馈:{{ user.feedback_content|yesno:"已提交,未提交,无内容" }}

{# 如果只提供两个自定义值,第三个(maybe)状态将使用第二个值 #}
是否启用:{{ config.is_enabled|yesno:"启用,禁用" }} {# 如果 config.is_enabled 为空,也会输出 "禁用" #}

By these examples, we can seeyesnoThe filter can not only handle boolean values, but also judge according to the 'true/false' rules of numbers and strings and output the corresponding text.This provides great convenience for dynamic content display of the template, making the code more concise and expressive.yesnoThe filter will effectively improve your work efficiency.

Common Questions (FAQ)

1. Why string"false"or"0"InyesnoThe filter will output in"yes"?This is becauseyesnoWhen the filter is judging a string, it only pays attention to whether the string is empty. Any non-empty string, even if its content is a text indicating "false", such as"false"or numeric"0"auto will be considered as “true”, so it will output withtruethe corresponding text (default is"yes")。Only when the string is completely empty ("") will it be considered as “false” or “empty”.

2.yesnoHow does the filter handle numeric variables, such as0and100?For numeric variables,yesnoThe filter will convert it to boolean logic:0(whether it is an integer)0or a floating-point number0.0) Would be treatedfalse, thus outputting withfalsethe corresponding text (default is"no"). Any non-zero number, whether positive, negative, or a number with a decimal point, would be treated astrue, outputting withtruethe corresponding text (default is"yes").

3.yesnoWhether the filter can be combined withifTags are used to build more complex logic? yesnoThe filter is mainly used to output different texts based on the true or false state of variables, it does not directly control the logic flow of the template. If you need to perform 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, and then use aifboolean variable in a label to control display. If the variable itself is a boolean type, you can use it directly inifThe label will be more concise. For example,{% if article.is_published %}vs.{% if article.is_published|yesno == "yes" %}More direct.yesnoMore focused on the differences in text output.