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.