In the template development of AnQi CMS, we often need to display different text in a user-friendly way on the front page based on the boolean (true/false) status of the background data, such as "Yes" or "No". Display directlytrueorfalseMay seem too rigid, and writing complexif-elsejudgment statements will make the template code long-winded. Fortunately, AnQiCMS provides a concise and efficient solution -yesnoFilter, it can help us easily convert boolean values to custom text states, and even elegantly handle "unknown" situations.
yesnoThe core function of the filter
yesnoFilter is a tool used to map a variable's boolean property to a specific string. Its main purpose is totrue/falseornil/Empty value (often considered as 'null' or 'undefined' in programming) is converted to our predefined text output. This greatly enhances the readability of the template and the conciseness of the code.
工作原理:布尔值的智能映射
yesnoThe filter intelligently determines the 'true', 'false', or 'empty' status of variables and returns the corresponding text accordingly. Its default mapping rules are as follows:
- When the value of the variable is evaluated astruethe filter returns the string by default
"yes". - When the value of the variable is evaluated asfalsethe filter returns the string by default
"no". - When the variable isnil or empty value(for example, an unset variable, an empty string)
"", or a number0etc.), the filter defaults to returning a string"maybe".
This tristate mapping mechanism is very flexible, covering most of our daily development needs.
How to useyesnoFilter
UseyesnoThe filter is very simple, following the general syntax of filters in the AnQiCMS template.{{ 变量 | 过滤器名称 }}.
1. Default usage
If you do not specify any custom text,yesnothe filter will use its default outputs of “yes”, “no”, “maybe”.
{# 假设 article.IsPublished 为 true #}
<p>文章发布状态:{{ article.IsPublished | yesno }}</p> {# 输出: 文章发布状态:yes #}
{# 假设 user.IsVip 为 false #}
<p>用户VIP状态:{{ user.IsVip | yesno }}</p> {# 输出: 用户VIP状态:no #}
{# 假设 archive.Status 为 nil 或未定义 #}
<p>归档状态:{{ archive.Status | yesno }}</p> {# 输出: 归档状态:maybe #}
2. English custom output text
yesnoThe real strength of the filter lies in its ability to allow us to customize the output text for these three states. You simply need to use a colon after the filter name.:Pass a comma-separated string,The text representing the true, false, and null values.
For example, if we want to display 'Yes', 'No', or 'Unknown':
{{ 变量 | yesno:"真值文本,假值文本,空值文本" }}
Let us take the example of an article being pinned at the top:
{# 假设 article.IsTop 为 true #}
<p>是否置顶:{{ article.IsTop | yesno:"是,否,未知" }}</p> {# 输出: 是否置顶:是 #}
{# 假设 article.IsTop 为 false #}
<p>是否置顶:{{ article.IsTop | yesno:"是,否,未知" }}</p> {# 输出: 是否置顶:否 #}
{# 假设 article.IsTop 变量不存在或为 nil #}
<p>是否置顶:{{ article.IsTop | yesno:"是,否,未知" }}</p> {# 输出: 是否置顶:未知 #}
If you only need to handle the two states of "yes" and "no", you can only provide two parameters:
{# 假设 comment.IsApproved 为 true #}
<p>评论审核通过:{{ comment.IsApproved | yesno:"已通过,待审核" }}</p> {# 输出: 评论审核通过:已通过 #}
{# 假设 comment.IsApproved 为 false #}
<p>评论审核通过:{{ comment.IsApproved | yesno:"已通过,待审核" }}</p> {# 输出: 评论审核通过:待审核 #}
{# 假设 comment.IsApproved 为 nil (同样会输出“待审核”,因为它会匹配第二个参数作为所有非真值的处理) #}
<p>评论审核通过:{{ comment.IsApproved | yesno:"已通过,待审核" }}</p> {# 输出: 评论审核通过:待审核 #}
Note:If only two parameters are provided, the second parameter will be processed simultaneouslyfalseandnil/The case of empty values. If you need to distinguish clearlyfalseandnil/Empty values, please make sure to provide three parameters.
Examples of practical application scenarios
yesnoThe filter is widely used in AnQiCMS templates, it can replace many long-windedif-elsestructures, making the template code clearer and easier to read.
Article publishing status display:
<p>发布状态: {{ archive.Published | yesno:"已发布,草稿,待定" }}</p>用户VIP或会员状态:English
<p>会员等级: {{ user.IsVip | yesno:"尊贵会员,普通用户,状态异常" }}</p>产品库存状态:English(假设有English)
product.InStockField)<p>库存状态: {{ product.InStock | yesno:"有货,缺货,未知" }}</p>简化表单中的复选框或单选按钮结果:EnglishWhen the form data retrieved from the background is a boolean value, it can be quickly converted for display in the user interface.
Compared with the traditional{% if ... %}{% else %}{% endif %}statement,yesnoThe filter has less code to handle simple boolean judgments and return corresponding text, making it easier to maintain and understand, especially when such states need to be frequently displayed in templates.
Summary
yesnoThe filter is a small but powerful tool in AnQiCMS template engine.It achieves an elegant conversion from boolean values to custom text through a concise syntax, effectively enhancing the tidiness and readability of template code.Master this filter to help you present dynamic content more efficiently in AnQiCMS development.
Common Questions (FAQ)
Q1:yesnoWhat is the default output text of the filter?
A1: yesnoThe filter will default totruewhen converting to “yes”,falsewhen converting to “no”,nilQ2: If it is empty, it should be converted to 'maybe'.
Q2: If I only want to display 'yes' and 'no' states and do not distinguishfalseandnilHow should it be set if it is empty?
A2:You can only provide two custom parameters. For example{{ 变量 | yesno:"是,否" }}In this case,trueIt will show “Yes”,falseandniland empty values will show “No”.
Q3:yesnoCan the filter handle non-boolean data types? For example, a number or a string?
A3: yesnoThe filter is mainly used to process boolean values or data that can be evaluated as boolean by the Go language (for example, non-empty strings, non-zero numbers are usually considered 'true', empty strings, zero numbers are considered 'false').However, **the practice is to apply it to explicit boolean types or data that you want to be handled with boolean semantics to avoid unexpected results.ifLabel would be a more appropriate choice.