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 an empty value. To handle these scenarios simply and efficiently, AnQiCMS provides a very practical built-in filter -yesno. It can elegantly identify the three core states of variables and output preset or custom text based on these states, greatly simplifying the conditional logic in templates.
yesnoThe principle of the filter: tri-state judgment
yesnoThe core of the filter lies in its three-state judgment mechanism of "OR and NOT", which makes it very flexible in handling various forms of variables. Specifically, it will judge the state of the variable according to the following rules:
The variable is true (
true) when When the value of the variable is evaluated as a boolean typetruethen,yesnoThe filter outputs 'yes'. This is typically used for explicit boolean fields, or implicitly converted to true by the AnQiCMS template engine for non-empty strings, non-zero numbers, etc.The variable is false (
false) when Conversely, if the value of the variable is evaluated as a boolean typefalseWhen, the filter will output "no". This includes explicit booleanfalsenumbers0an empty string""etc.The variable is an empty value or undefined (
nil) when: This isyesnoA filter is particularly useful here. When the variable is a null value (commonly represented as in Go language,)nil)is not defined in the template context, or is an empty array, slice, or dictionary (map)yesnoThe filter will judge it as the third state, and output "maybe" by default.This judgment ability allows us to easily distinguish between "false" and "non-existent or empty", which is very critical in many business scenarios.
Basic usage method
yesnoThe filter usage is very intuitive, just add it to the variable and use the pipe character|to connect.
For example, if we have a file namedarchive.StatusThe variable, it may represent the publishing status of the article:
<p>文章状态:{{ archive.Status | yesno }}</p>
Based onarchive.StatusThe actual value, which may be displayed here:
- If
archive.StatusWithtrueOutput:文章状态:yes - If
archive.StatusWithfalseOutput:文章状态:no - If
archive.StatusWithnilOr undefined, output:文章状态:maybe
Custom output text
The default "yes", "no", "maybe" may not be intuitive or consistent with the page style in some cases.yesnoThe filter allows us to customize the output text for these three states. You just need to provide three commas after the filter,The string that corresponds to 'true', 'false', and 'empty or undefined' outputs.
<p>文章发布状态:{{ archive.Status | yesno:"已发布,草稿,待审核" }}</p>
In this example:
- If
archive.StatusWithtrueOutput:文章发布状态:已发布 - If
archive.StatusWithfalseOutput:文章发布状态:草稿 - If
archive.StatusWithnilOr undefined, output:文章发布状态:待审核
It should be noted thatIf you provide custom text, it must be provided in the order of 'true text,false text,empty text'.If only two are provided, then if the variable is empty, it will still output the default 'maybe'.
Application scenarios in practice
yesnoThe filter can be applied to various scenarios in the AnQiCMS template, especially when it is necessary to simply display variable states:
Check if the content is recommended or pinned:Suppose there is a custom boolean field in the article model
is_featured.<p>是否推荐:{{ archive.is_featured | yesno:"是,否,未设置" }}</p>Determine whether a certain feature of the website is enabled:In the system settings, we may have defined a parameter
SiteCloseto control whether the website is closed.<p>网站状态:{{ system.SiteClose | yesno:"已关闭,正常运行" }}</p>Here only two parameters are provided, when
system.SiteCloseWithtrueshow "Closed" when (closed), asfalseshow "Normal operation" when (not closed). Ifsystem.SiteCloseNot set or empty, the default 'maybe' will be displayed.Processing possibly empty user information: When displaying user information, if a field (such as the user's email address) may be empty or undefined.
<p>用户邮箱:{{ user.Email | yesno:user.Email+",暂无邮箱,未注册" }}</p>In this example, if
user.EmailIf there is a value, it will display the email address directly; ifuser.EmailWithfalse(For example, an empty string) will display 'No email'; ifuser.EmailWithnilundefined, it will display 'Not registered'. This flexible combination of usage can help us target information display.
In short,yesnoThe filter is a simple yet powerful tool in the AnQiCMS template that helps us handle multiple states of variables in an elegant way, avoiding complexif-elseNested, making the template code more tidy and readable, improving development efficiency and the maintainability of the template.
Frequently Asked Questions (FAQ)
1.yesnoWhat types of variables can the filter handle?
yesnoThe filter is mainly used to handle boolean values (true/false). But it is also intelligent, as for non-boolean types, it will try to convert it to a boolean context for judgment (for example, non-empty strings, non-zero numbers are usually considered astrue; empty string, number0is consideredfalsemost importantly, it can recognizenil(empty value) or undefined variables, which is the key difference from simple boolean judgments.
2. If the variable is neithertruenorfalse, but completely does not exist (for example, a spelling error),yesnohow will it perform?
When a variable does not exist at all in the template context (for example, if the variable name is spelled incorrectly, or the data model does not have this field at all),yesnoThe filter will judge it as the third state - null or undefined.At this moment, it will output the default 'maybe' or the third custom text value you provide.This is very useful for handling missing or unexpectedly non-existent data, which can avoid template rendering errors.
3.yesnoFilters andifWhat are the differences between logic judgment tags? When should which one be used?
ifLogic judgment tag ({% if ... %}Used to perform more complex conditional logic branches, such as displaying entire content blocks, controlling loops, or changing the template structure. AndyesnoThe filter focuses on directly and succinctly converting the three states of a variable into a string output. When you only need to display a piece of text, an icon, or a brief description based on the true or false or null state of a variable,yesnoThe filter is a more concise and efficient choice. When you need to render different large blocks of HTML code or perform more complex logic based on conditions,iftags are more suitable.