In AnQiCMS template development, we often need to display different content based on the different states of the data.For example, an order is either “paid” or “unpaid”, an article is either “published” or “draft”.yesnoFilter.

yesnoThe filter can intelligently output the preset text content based on the boolean value (true, false) of variables and the empty value status, thus making your template logic clearer and page display more user-friendly.It follows the characteristics of the AnQiCMS template based on the Django template engine syntax, which is easy to learn and use.

Core Function Analysis: Judgment of Three States of Variables

yesnoThe core of the filter lies in its ability to judge the 'three states':

  1. True-like Value:When a variable is evaluated as true (for example, a boolean valuetrue, non-zero numbers, non-empty strings, etc.).
  2. False-like values:When a variable is evaluated as false (for example, a boolean valuefalseEnglish, zero, empty string, etc.).
  3. Null (Nil/Unknown):When the variable isnilor undefined.

By default,yesnoThe filter will output "yesThis design takes into account that data may exist in an "undetermined" or "unknown" state and provides more comprehensive feedback.

Of course, in practical applications, the default “yes/no/maybe” may not meet your specific business needs.yesnoThe filter allows you to customize the text for these three states, making the output more in line with actual scenarios.

Actual application scenarios

Imagine that in a content management system, you may need:

  • Display article publishing status:Published” or “Draft.”
  • Indicate user account activity:Active” or “Disabled.”
  • Show a feature switch:“Turn on” or “Turn off”.
  • Check if a data field exists:“Has data”, “No data” or “Undefined”.

Directly output in these scenarios.trueorfalse往往不够直观,而yesno过滤器正是解决这一问题的利器,它将技术性的布尔值转化为易于理解的业务描述。

How to useyesnoFilter

yesnoThe usage of the filter is very intuitive, you can choose different syntaxes based on whether you need to customize the output text.

  1. Basic syntax (using default text)Simply pass the variable through the pipe symbol|connected toyesnothe filter.

    {{ 变量名 | yesno }}
    
    • If变量名true, output "yes".
    • If变量名false, output "no".
    • If变量名empty (nilor undefined), output "maybe".
  2. custom text syntaxInyesnoFiltered, it provides three strings separated by English commas,Each string corresponds to true, false, and null value.

    {{ 变量名 | yesno:"真值文本,假值文本,空值文本" }}
    
    • If变量名If true, output真值文本.
    • If变量名If false, output假值文本.
    • If变量名If null, output空值文本.

    If only two custom texts are provided (for exampleyesno:"是,否"),then the third empty value status will default to “maybe”. This should be noted in actual use.

Code Example with Detailed Explanation

Let's see a few specific examples to understandyesnoApplication of Filters:

Suppose we have the following variables in the template:

{% set user_active = true %}
{% set user_suspended = false %}
{% set user_level = nil %} {# nil 值 #}
{% set user_description = "" %} {# 空字符串 #}
{% set product_stock = 10 %} {# 非布尔值 #}

Example 1: Using Default Text

<p>用户是否活跃? {{ user_active|yesno }}</p>
<p>用户是否被禁用? {{ user_suspended|yesno }}</p>
<p>用户等级是否设置? {{ user_level|yesno }}</p>
<p>用户是否有描述? {{ user_description|yesno }}</p>

Output result:

<p>用户是否活跃? yes</p>
<p>用户是否被禁用? no</p>
<p>用户等级是否设置? maybe</p>
<p>用户是否有描述? no</p>

Interpretation: user_activeYestrueTherefore, it isyes.user_suspendedYesfalseTherefore, it isno.user_levelis `