When building a website with AnQiCMS, templates often need to handle some status values representing "yes" or "noFor example, whether an article has been published, whether a feature has been enabled, or whether a certain setting is true.yesnoFilter, which helps us elegantly convert these boolean values or trinary logic (true, false, null) into friendly text display.
ThisyesnoWhen the filter was initially designed, it would default totrue值显示为“yes”,false值显示为“no”。而对于那些既不是真也不是假,或者说是一个空值(nil/empty)的情况,它则会显示为“maybe”。This default English display method may not be very suitable for certain scenarios, especially when the website is mainly aimed at Chinese users. We would rather see expressions like 'Yes', 'No', 'Unknown'.
幸运的是,AnQiCMS 的yesnoThe filter provides flexible customization functions, allowing us to easily adjust these display texts according to our actual needs.The custom method is very intuitive, just provide a comma-separated string after the filter, which includes the text for the three states of "true
例如,如果我们希望将true显示为“是”,false显示为“否”,而空值或未知状态显示为“未知”,我们可以这样来使用yesnoFilter:
{{ archive.IsPublished|yesno:"是,否,未知" }}
In this code,archive.IsPublished是我们想要判断的状态值。紧随其后的|yesno便是调用过滤器,而:"是,否,未知"The custom display text is provided. At this time, ifarchive.IsPublishedis true, the template will output "Yes"; if it is false, it will output "No"; ifarchive.IsPublishedis an empty 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:"启用,禁用",then 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.IsActivetrue will display "Enabled"; false 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 texts.
This custom feature is particularly useful in multilingual websites or in scenarios that require specific business terminology.For example, on an e-commerce website, we can display the product inventory status as "In stockyesnoThe display text of the filter, we can make the website content more in line with user habits, improving the user experience.
In short,yesnoThe filter is a small yet powerful tool that helps us display status information in templates in a more flexible and user-friendly manner.Through simple string parameters, we can easily implement various custom displays such as 'Yes', 'No', 'Unknown', etc., making the template content of AnQiCMS more localized and personalized.
Common Questions (FAQ)
Q: If I only provide
yesnoFilter 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 fortruecases. Andfalseandnil/Empty value cases will fall back to displaying the default “no” and “maybe”. To avoid such inconsistency, it is recommended to provide at least two (correspondingtrueandfalse)or three (correspondingtrue/falseandnil)Custom text.Q: Can different display texts be set for different template areas or different contexts?
yesnoFilter settings? Answer:Absolutely.yesnoThe filter can be independently set to display text each time it is used.This means you can configure it as 'On, Off, Not Set' in one place, and use 'Approved, Rejected, Pending' in another, depending on the current context.This flexibility allows AnQiCMS templates to highly adapt to various content display needs.Q:
yesnoFilter can handle what types of variables besides boolean values? Answer:yesnoFilter is mainly designed to handle boolean values (true/false) and variables that can be evaluated as boolean or "empty" states in Go language templates. This means in addition to explicit boolean values, likenil、empty strings, zero-valued numbers (such as0)等也可能被识别为Englishfalseornil