How does the `yesno` filter in the AnQiCMS template determine if a variable exists or is empty (nil)?

Calendar 👁️ 66

In AnQiCMS template development, we often encounter situations where we need to determine whether a variable is true, false, or simply does not exist or is an empty value. To handle these scenarios simply and efficiently, AnQiCMS provides a very practical built-in filter -yesno. It can elegantly identify the three core states of variables and output preset or custom text based on these states, greatly simplifying the conditional logic in templates.

yesnoThe principle of the filter: tri-state judgment

yesnoThe core of the filter lies in its three-state judgment mechanism of "OR and NOT", which makes it very flexible in handling various forms of variables. Specifically, it will judge the state of the variable according to the following rules:

  1. The variable is true (true) when When the value of the variable is evaluated as a boolean typetruethen,yesnoThe filter outputs 'yes'. This is typically used for explicit boolean fields, or implicitly converted to true by the AnQiCMS template engine for non-empty strings, non-zero numbers, etc.

  2. The variable is false (false) when Conversely, if the value of the variable is evaluated as a boolean typefalseWhen, the filter will output "no". This includes explicit booleanfalsenumbers0an empty string""etc.

  3. The variable is an empty value or undefined (nil) when: This isyesnoA filter is particularly useful here. When the variable is a null value (commonly represented as in Go language,)nil)is not defined in the template context, or is an empty array, slice, or dictionary (map)yesnoThe filter will judge it as the third state, and output "maybe" by default.This judgment ability allows us to easily distinguish between "false" and "non-existent or empty", which is very critical in many business scenarios.

Basic usage method

yesnoThe filter usage is very intuitive, just add it to the variable and use the pipe character|to connect.

For example, if we have a file namedarchive.StatusThe variable, it may represent the publishing status of the article:

<p>文章状态:{{ archive.Status | yesno }}</p>

Based onarchive.StatusThe actual value, which may be displayed here:

  • Ifarchive.StatusWithtrueOutput:文章状态:yes
  • Ifarchive.StatusWithfalseOutput:文章状态:no
  • Ifarchive.StatusWithnilOr undefined, output:文章状态:maybe

Custom output text

The default "yes", "no", "maybe" may not be intuitive or consistent with the page style in some cases.yesnoThe filter allows us to customize the output text for these three states. You just need to provide three commas after the filter,The string that corresponds to 'true', 'false', and 'empty or undefined' outputs.

<p>文章发布状态:{{ archive.Status | yesno:"已发布,草稿,待审核" }}</p>

In this example:

  • Ifarchive.StatusWithtrueOutput:文章发布状态:已发布
  • Ifarchive.StatusWithfalseOutput:文章发布状态:草稿
  • Ifarchive.StatusWithnilOr undefined, output:文章发布状态:待审核

It should be noted thatIf you provide custom text, it must be provided in the order of 'true text,false text,empty text'.If only two are provided, then if the variable is empty, it will still output the default 'maybe'.

Application scenarios in practice

yesnoThe filter can be applied to various scenarios in the AnQiCMS template, especially when it is necessary to simply display variable states:

  • Check if the content is recommended or pinned:Suppose there is a custom boolean field in the article modelis_featured.

    <p>是否推荐:{{ archive.is_featured | yesno:"是,否,未设置" }}</p>
    
  • Determine whether a certain feature of the website is enabled:In the system settings, we may have defined a parameterSiteCloseto control whether the website is closed.

    <p>网站状态:{{ system.SiteClose | yesno:"已关闭,正常运行" }}</p>
    

    Here only two parameters are provided, whensystem.SiteCloseWithtrueshow "Closed" when (closed), asfalseshow "Normal operation" when (not closed). Ifsystem.SiteCloseNot set or empty, the default 'maybe' will be displayed.

  • Processing possibly empty user information: When displaying user information, if a field (such as the user's email address) may be empty or undefined.

    <p>用户邮箱:{{ user.Email | yesno:user.Email+",暂无邮箱,未注册" }}</p>
    

    In this example, ifuser.EmailIf there is a value, it will display the email address directly; ifuser.EmailWithfalse(For example, an empty string) will display 'No email'; ifuser.EmailWithnilundefined, it will display 'Not registered'. This flexible combination of usage can help us target information display.

In short,yesnoThe filter is a simple yet powerful tool in the AnQiCMS template that helps us handle multiple states of variables in an elegant way, avoiding complexif-elseNested, making the template code more tidy and readable, improving development efficiency and the maintainability of the template.


Frequently Asked Questions (FAQ)

1.yesnoWhat types of variables can the filter handle?

yesnoThe filter is mainly used to handle boolean values (true/false). But it is also intelligent, as for non-boolean types, it will try to convert it to a boolean context for judgment (for example, non-empty strings, non-zero numbers are usually considered astrue; empty string, number0is consideredfalsemost importantly, it can recognizenil(empty value) or undefined variables, which is the key difference from simple boolean judgments.

2. If the variable is neithertruenorfalse, but completely does not exist (for example, a spelling error),yesnohow will it perform?

When a variable does not exist at all in the template context (for example, if the variable name is spelled incorrectly, or the data model does not have this field at all),yesnoThe filter will judge it as the third state - null or undefined.At this moment, it will output the default 'maybe' or the third custom text value you provide.This is very useful for handling missing or unexpectedly non-existent data, which can avoid template rendering errors.

3.yesnoFilters andifWhat are the differences between logic judgment tags? When should which one be used?

ifLogic judgment tag ({% if ... %}Used to perform more complex conditional logic branches, such as displaying entire content blocks, controlling loops, or changing the template structure. AndyesnoThe filter focuses on directly and succinctly converting the three states of a variable into a string output. When you only need to display a piece of text, an icon, or a brief description based on the true or false or null state of a variable,yesnoThe filter is a more concise and efficient choice. When you need to render different large blocks of HTML code or perform more complex logic based on conditions,iftags are more suitable.

Related articles

How does the `yesno` filter in the AnQiCMS template display different texts based on boolean true or false values?

In AnQiCMS template development, we often need to display different content based on the different states of the data.For example, an order is 'paid' or 'unpaid', an article is 'published' or 'draft'.To handle the conversion of such boolean data to user-friendly text more concisely and efficiently, the AnQiCMS template engine provides a very practical tool—the `yesno` filter.The `yesno` filter can intelligently output predefined text content based on the boolean value (true, false) and null status of the variable

2025-11-09

The `wordwrap` filter in AnQiCMS, does it have compatibility issues with long text containing special characters?

In AnQiCMS, the `wordwrap` filter is a practical tool for handling long text formatting in templates, aimed at improving the reading experience of the content.However, when it comes to long text that includes special characters, many users are concerned about its compatibility issues, especially in today's increasingly prevalent multi-language content. The `wordwrap` filter in AnQiCMS, its core working principle is to **wrap lines based on spaces between words**.This means, it will treat each word group separated by spaces as a separate unit and will stop when it reaches the specified length

2025-11-09

How to set the default word wrap length in AnQiCMS?

In AnQiCMS, managing website content often involves scenarios where it is necessary to display long texts, such as article summaries, product descriptions, or sidebar announcements.If this text is not properly processed, it may cause layout confusion, even exceeding the preset container width, seriously affecting the user experience.Fortunately, AnQiCMS provides the `wordwrap` filter to help us elegantly solve this problem, allowing long text content to automatically wrap to the desired length.How to use AnQiCMS

2025-11-09

Does the `wordwrap` filter support pre-processing text on the AnQiCMS backend?

During the use of AnQiCMS, we often encounter the need to format long texts to present them with better visual effects on the page.Among them, automatic line breaking in text is a common scenario. Many users may be curious about the specific position of the `wordwrap` filter in the content processing chain, especially in the "backend text preprocessing" stage.In order to better understand the role of the `wordwrap` filter in AnQiCMS, we first need to clarify the difference between "backend preprocessing" and "front-end template rendering"

2025-11-09

How to customize the display text of the 'yes', 'no', and 'unknown' three states in the AnQiCMS template `yesno` filter?

When using AnQiCMS to build a website, templates often need to handle some status values representing "yes" or "no".For example, is an article published, a feature enabled, or a certain setting item is true.The system provides a very practical `yesno` filter that can help us elegantly convert these boolean values or three-valued logic (true, false, null) into friendly text display.This `yesno` filter was initially designed to display `true` values as 'yes' and `false` as 'no'

2025-11-09

What are the main differences and application scenarios between the `yesno` filter of AnQiCMS and the `if` statement when handling boolean values?

In the practice of template development for Anqi CMS, flexible handling of Boolean data is a part of daily work.When facing a state value representing "true" or "false", we usually use two main template syntaxes to deal with it: one is the conditional judgment `if` statement, and the other is the `yesno` formatting filter.Although they are all related to boolean values, their original intention, mechanism of action, and applicable scenarios are obviously different.Understanding these differences can help us build website content more accurately and efficiently.### `if` statement

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