In Anqi CMS template development, 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-elseConditional statements make template code look long-winded. Fortunately, AnQiCMS provides a concise and efficient solution—yesnoA filter that can help us easily convert boolean values to custom text states, even elegantly handling 'unknown' situations.
yesnoThe core role of the filter
yesnoA filter is a tool used to map the boolean nature of a variable to a specific string. Its main purpose is to maptrue/falseornil/The value (often considered as 'empty' or 'undefined' in programming) is converted to our preset text output. This greatly enhances the readability and conciseness of the template.
The principle of operation: intelligent mapping of boolean values
yesnoThe filter intelligently judges the 'true', 'false', or 'empty' status of variables and returns the corresponding text based on these judgments. Its default mapping rules are as follows:
- When the value of the variable is evaluated astruethen the filter defaults to returning a string
"yes". - When the value of the variable is evaluated asfalsethen the filter defaults to returning a string
"no". - when the value of 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 trinary mapping mechanism is very flexible and can cover most of our daily development needs.
How to useyesnoFilter
UseyesnoThe filter is very simple, it follows the general syntax of the filters in the AnQiCMS template:{{ 变量 | 过滤器名称 }}.
1. Default usage
If you do not specify any custom text,yesnoThe filter will use its default “yes”, “no”, “maybe” as output.
{# 假设 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. Custom output text
yesnoThe real strength of the filter lies in its ability to customize the output text for these three states. You just need to use a colon after the filter name.:Pass a comma-separated string,representing the text for "true", "false", and "null".
For example, if we want to display "yes", "no", or "unknown":
{{ 变量 | yesno:"真值文本,假值文本,空值文本" }}
Let us take the example of whether an article is pinned:
{# 假设 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 at the same timefalseandnil/The case of null values. If it is necessary to distinguishfalseandnil/If null values, please be sure to provide three parameters.
Examples of actual 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.
Display of article publishing status:
<p>发布状态: {{ archive.Published | yesno:"已发布,草稿,待定" }}</p>User VIP or membership status:
<p>会员等级: {{ user.IsVip | yesno:"尊贵会员,普通用户,状态异常" }}</p>Product inventory status:(Assuming there is)
product.InStockfield)<p>库存状态: {{ product.InStock | yesno:"有货,缺货,未知" }}</p>Result of checkboxes or radio buttons in the simplified form:When the form data obtained from the backend is a boolean value, it can be quickly converted to user interface display.
Compared with the traditional{% if ... %}{% else %}{% endif %}statements,yesnoThe filter handles simple boolean judgments and returns the corresponding text, which results in less code and is easier to maintain and understand, especially when frequently displaying such states in templates.
Summary
yesnoThe filter is a seemingly insignificant but powerful tool in the AnQiCMS template engine.It achieves an elegant transformation from boolean values to custom text through concise syntax, effectively enhancing the neatness and readability of template code.Master this filter to help you present dynamic content more efficiently in AnQiCMS development.
Frequently Asked Questions (FAQ)
Q1:yesnoWhat is the default output text of the filter?
A1: yesnoWhen the filter does not have custom parameters, it defaults to convertingtrueto “yes”,falseto “no”, andnilIf empty, it will be converted to 'maybe'.
Q2: If I only want to display 'yes' and 'no' states and not distinguishfalseandnilHow should the empty value be set?
A2:You can only provide two custom parameters. For example{{ 变量 | yesno:"是,否" }}In this case,trueIt will display "Yes", whilefalseandnilEmpty values will display "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 in Go language (for example, non-empty strings, non-zero numbers are usually considered "true", empty strings, zero numbers are considered "false").However, practice is to apply it to explicit boolean types or data that you want to be handled with boolean semantics to avoid unexpected results.If you need to perform more complex conditional judgments on non-boolean data,ifTags would be a more suitable choice.