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'.In order to handle the conversion of such boolean data to user-friendly text more concisely and efficiently, the AnQiCMS template engine provides a very practical toolyesnofilter.

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

Core function analysis: Determine the three states of variables

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

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

By default,yesnoThe filter will output 'yes', 'no', and 'maybe' for these three states.This design takes into account the possibility that data may exist in an "undetermined" or "unknown" state, providing more comprehensive feedback.

Of course, in practice, the default 'yes/no/maybe' may not meet your specific business needs.yesnoThe filter allows you to customize the text corresponding to these three states, making the output more fitting for the actual scenario.

Application scenarios in practice

Imagine, in a content management system, you might need:

  • Display the article publishing status:"Published" or "Draft".
  • Indicate the activity of the user account:"Active" or "Disabled".
  • Show a certain feature switch:“Open” or “Close”.
  • Check if a data field exists:“Data available”, “No data” or “Undefined”.

Direct output in these scenarios.trueorfalseIt is often not intuitive, butyesnoThe filter is the tool to solve this problem, it converts technical boolean values into business descriptions that are easy to understand.

How to useyesnoFilter

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

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

    {{ 变量名 | yesno }}
    
    • If变量名If true, output 'yes'.
    • If变量名If false, output 'no'.
    • If变量名If empty (nilor undefined), output 'maybe'.
  2. Custom text syntaxInyesnoAfter the filter provides three strings separated by English commas,Each string corresponds to a truth value, a false value, and a null value.

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

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

Code Example with Detailed Explanation

Let's take a look at several specific examplesyesnoApplication 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_activeIstrueTherefore it isyes.user_suspendedIsfalseTherefore it isno.user_levelIt is `