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

Calendar 👁️ 64

When building a website with AnQiCMS, the template often needs to handle some status values representing "yes" or "no."}For example, whether an article has been published, a feature has been enabled, or a certain setting is true.The system provided a very practicalyesnoThe filter, which helps us elegantly convert these boolean values or three-valued logic (true, false, null) into friendly text display.

ThisyesnoThe filter was originally designed to default totrueThe value is displayed as 'yes',falseIt displays as 'no'. For those that are neither true nor false, or are an empty value (nil/empty), it will display as 'maybe'.This default English display method may not meet our needs in some cases, especially when the website is mainly aimed at Chinese users, where we would prefer to see expressions like 'Yes', 'No', 'Unknown'.

Fortunately, AnQiCMS 'syesnoThe filter provides flexible customization features, allowing us to easily adjust these display texts according to our actual needs.The method is very intuitive, just provide a comma-separated string after the filter, which contains the text of the three states we want to display: 'True', 'False', and 'Unknown'.

For example, if we want to changetruedisplayed as 'yes',falseShow as 'No', and empty values or unknown statuses are shown as 'Unknown', we can use it like thisyesnoFilter:

{{ archive.IsPublished|yesno:"是,否,未知" }}

In this code block,archive.IsPublishedis the state value we want to judge. Following it|yesnomeans calling the filter, and:"是,否,未知"Then a custom display text is provided. At this point, ifarchive.IsPublishedis true, the template will output "Yes"; if it is false, it will output "No"; ifarchive.IsPublishedIs a blank value (for example, the field is not set or is null), it will output "unknown".

It is worth noting that if you only provide two custom texts, for example,|yesno:"启用,禁用"They will correspond respectivelytrueandfalseThe display text. For empty values or unknown states, the filter will default to displaying 'maybe'. For example:

{{ user.IsActive|yesno:"启用,禁用" }}

Here, ifuser.IsActiveIf true, it will display 'Enabled'; if false, it will display 'Disabled'; and ifuser.IsActiveIs empty, it will display "maybe". Therefore, to ensure that all states have clear Chinese display, it is recommended to always provide three custom text.

This custom feature is particularly useful in multilingual websites or scenarios that require specific business terminology.For example, on an e-commerce website, we can display the product inventory status as 'In stock', 'Out of stock', 'Waiting to be restocked';In a member system, the member status is displayed as 'normal', 'frozen', and 'pending review'.Adjustment passedyesnoThe filter's display text, we can make the website content more in line with user habits, enhancing the user experience.

In summary,yesnoThe filter is a small yet powerful tool that helps us display status information in templates in a more flexible and user-friendly manner.By using a simple string parameter, we can easily implement various custom displays such as 'Yes', 'No', 'Unknown', etc., making the template content of AnQiCMS more localized and personalized.


Frequently Asked Questions (FAQ)

  1. Ask: If I only giveyesnoThe filter provides a custom text, how will it be displayed? Answer:If you only provide a custom text, for example{{ some_value|yesno:"已完成" }}, then this text is usually used fortruesituations. Andfalseandnil/The case of empty values will fall back to displaying the default “no” and “maybe”. To avoid this inconsistency, it is recommended to provide at least two (correspondingtrueandfalseOr three (corresponding)true/falseandnilCustom text.)

  2. Can I set different display text foryesnofilter settings in different template areas or contexts? Answer:Absolutely.yesnoThe filter can be set independently each time its display text is used.This means you can configure it as This flexibility allows AnQiCMS templates to highly adapt to various content display needs.

  3. Question:yesnoWhat types of variables can the filter handle besides boolean values? Answer: yesnoThe filter is mainly designed to handle boolean values (true/falseA variable that can be evaluated as a boolean or an empty state by the Go language template engine. This means, in addition to explicit boolean values, things likenil、empty strings, zero-valued numbers (such as0)et al may also be recognized asfalseornil/ empty. Its behavior may depend on the actual type of the variable and its evaluation rules in the Go template, but its core logic is to be parsed into one of three states: true, false, or unknown/empty.

Related articles

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

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 null.To handle these scenarios concisely and efficiently, AnQiCMS provides a very practical built-in filter——`yesno`.It can elegantly recognize the three core states of variables and output preset or custom text based on these states, greatly simplifying the conditional logic in the template.### `yesno` filter working principle: three-state judgment `yesno`

2025-11-09

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

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

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