In AnQiCMS template, can the `yesno` filter determine the true or false state of a numeric or string variable?

Calendar 👁️ 68

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.
    • not0Number: 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.

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.

Related articles

How to set the internationalized text for three states in the `yesno` filter of AnQiCMS template for a multilingual website?

How to ensure that the dynamic text in the template of a multilingual website can be displayed correctly according to the visitor's language settings is a challenge often faced by website operators.AnQiCMS (AnQiCMS) relies on its flexible template engine and strong multilingual support to provide an elegant solution.Today, let's delve into how the `yesno` filter of the AnQiCMS template can be combined with the internationalization mechanism in a multilingual website to accurately present three status texts.### Get to know `yesno`

2025-11-09

What state does the `yesno` filter return by default when handling null values in AnQiCMS templates?

In Anqi CMS template development, flexibly handling various data states is the key to building dynamic pages.We often encounter situations where we need to display different content based on the true or false state of a variable.This is when the `yesno` filter becomes a very practical tool.It can help us elegantly convert boolean logic into more readable text output.### Deep understanding of `yesno` filter The `yesno` filter is a small but powerful template tool, its core function is to judge the status of a variable

2025-11-09

How to use the `yesno` filter in AnQiCMS templates to indicate whether content has been published or is in draft status?

In website content management, clearly identifying the publishing status of content is a crucial link, which can help operators and visitors quickly understand the timeliness of the information.AnQiCMS provides a flexible template system, allowing us to conveniently meet this need.Today, we will discuss how to skillfully use the `yesno` filter built into the AnQiCMS template to elegantly present the published or draft status of content.Understanding content publication status in AnQiCMS

2025-11-09

How to use the AnQiCMS template `yesno` filter to elegantly display the user's membership status (such as: member, non-member, pending review)?

In website operations, clearly displaying a user's membership status is a key element in improving user experience and guiding user behavior.We often encounter situations where we need to display different texts or styles based on the different states of users (such as whether they are members, their membership level, whether their account is active, etc.)AnQiCMS provides rich and powerful template tags and filters, among which the `yesno` filter is a very practical tool that can help us handle conditional judgments in a concise and elegant way, making the template code cleaner and easier to read.### Get to know AnQiCMS's

2025-11-09

How does the `yesno` filter combine with the data list in AnQiCMS templates to display the specific status of each piece of data?

During the process of building a website with AnQiCMS, we often need to display various data on the front-end page, which often has different states.For example, is an article published or a draft, a product listed or not, or is a user active or not.How to clearly and intuitively present these states to the user while keeping the template code concise and readable is a common concern for content operations and template developers.Today, let's delve into a very practical tool in AnQiCMS——the `yesno` filter

2025-11-09

How to implement multi-condition logical judgment with the `if` tag in AnQiCMS template (OR/&&, AND/||, NOT/!)?

In AnQiCMS template development, mastering conditional logic judgment is the key to building dynamic and intelligent web pages.Among them, the `if` tag is the core of controlling content display, not only supporting simple boolean judgments, but also being able to flexibly implement logical combinations of multiple conditions, such as "and" (`&&`), "or" (`||`), and "not" (`!`)`etc. Understanding and skillfully using these logical operators can help us control the display and hiding of template elements more finely, thereby creating a more interactive and customizable user experience.### Basic Conditional Judgment: `if`

2025-11-09

How to use the `if` tag in AnQiCMS templates to determine if a variable is empty, exists, or has a specific value?

In AnQiCMS template development, the `if` tag is the core tool for building dynamic page content.It allows us to flexibly control the display of content based on different conditions, thereby providing users with a more intelligent and personalized browsing experience.Whether you want to judge whether a data exists, whether it is empty, or hope to adjust the layout according to a specific value, the `if` tag can help you easily achieve it.AnQiCMS's template engine syntax is very similar to the Django template engine, therefore, developers familiar with this syntax will feel very亲切。`if`

2025-11-09

How to construct complex conditional display logic in AnQiCMS templates using `if`, `elif`, and `else` structures?

In AnQiCMS template design, dynamically displaying content is a key factor in enhancing website interactivity and user experience.When we need to decide what and how to display on the page based on specific data conditions, the `if`, `elif` (short for else if), and `else` conditional tags are particularly important.They have given the template flexible logical control capabilities, allowing our website to meet various complex display needs.The condition judgment syntax of the AnQiCMS template engine is similar to many programming languages, it is very intuitive and easy to understand

2025-11-09