In the template development of Anqi CMS, how to present boolean (true/false) status or handle unknown (empty) values in a直观, 简洁 way is an important aspect for improving user experience and code readability.yesnoThe filter is designed for this purpose, it can simplify complex logical judgments into a single line of code, and allow you to customize the output results, such as displaying as 'Yes/No/Unknown'.
yesnoFilter: Intelligent converter for boolean and null values
In content management systems, we often encounter situations where we need to display whether a project is enabled, whether a feature is turned on, or whether a field has a value. Traditional practices may be to use lengthyif/elseHowever, the statement to judge. However, the Anqi CMS providedyesnoThe filter provides a more elegant and concise solution for us.
This filter's core function is to map a variable's 'true', 'false', or 'nil/empty' value to the text output you have预设预设.It is just like an intelligent translator, converting the internal data state of the system into natural language that users can understand easily.
Why chooseyesnoFilter?
Its advantage lies in simplifying template code, improving readability and maintainability.Imagine if each time you display a boolean state, you need to write three or four lines of conditional judgment statements, then the template file will become very bloated and difficult to manage when there are multiple such judgments on the page.yesnoThe filter encapsulates this repetitive work, allowing the template to focus more on content presentation rather than complex logic.
yesnoBasic usage of the filter
In the templates of Anqi CMS, useyesnoThe basic syntax of the filter is very direct, it follows the general rules of the security CMS template filter:{{ 变量 | yesno }}.
When you use it like this,yesnoThe filter will output the corresponding default text based on the actual state of the variable:
- If the value of the variable is
trueit will outputyes. - If the value of the variable is
falseit will outputno. - If the value of the variable is
nil(i.e., a null value or undefined), it will outputmaybe.
For example, you may have aarchive.IsPublishedField indicating whether the article has been published.{{ archive.IsPublished | yesno }}May be displayed.yes/noormaybe.
Customize the display of 'Yes/No/Unknown'.
Default.yes/no/maybeMay not be intuitive in certain scenarios, especially in the Chinese context.yesnoThe strength of the filter lies in its ability to fully customize the output text for these three states.
You need to pass three comma-separated strings to the filter after the key, which correspond to the display text for 'true', 'false', and 'empty' values. For example, to display as 'Yes/No/Unknown', you can write:
{{ 变量 | yesno:"是,否,未知" }}
Let us further illustrate through several practical examples:
Product inventory status:Suppose there is a product in your product data:
product.IsInStockField (boolean), indicating whether the product has stock. You want to display "In stock", "Sold out", or "Pending" on the front end.<p>库存状态:{{ product.IsInStock | yesno:"有货,售罄,待定" }}</p>Article pin mark:If your article list has a
article.IsPinnedField indicating whether the article is pinned. You want to display “Pinned”, “Not Pinned”, or “Status Unknown”.<p>置顶状态:{{ article.IsPinned | yesno:"已置顶,未置顶,状态不明" }}</p>User account enable status:“When managing users, you may need to display
user.IsActiveThe value indicates whether the user account is active.<p>账号状态:{{ user.IsActive | yesno:"启用中,已禁用,未设置" }}</p>
In this way, you can make the template output more expressive and better meet the localization requirements of the website. When the value of the variable istruewhen it is, it will display the first custom string; when the value isfalseit will display the second; while the variable isnil(empty value) it will display the third custom string.
It is worth noting that even if the variable is not a strict boolean type, the template engine of Anqi CMS will try to evaluate it as a boolean value for processing.For example, non-zero numbers or non-empty strings are typically considered 'true', while zero values or empty strings may be considered 'false'.yesnoThe variable of the filter itself is an explicit boolean type or may benil.
In short,yesnoThe filter is a small but powerful tool in the Anqi CMS template, which can effectively enhance the neatness of template code and the presentation effect of content, making the display of boolean values and null values more humanized and easy to understand.
Common Questions and Answers (FAQ)
1. If I only provide two strings when I customize the parameters, for example,{{ 变量 | yesno:"是,否" }}what will be displayed when the variable is empty?Answer: If you only provide two parameters,yesnoThe filter will use the first parameter for 'true' and the second parameter for 'false'. When the variable is empty, it will still fallback to its default third state output, which is to displaymaybeSuggest to avoid confusion and maintain consistency, always provide three parameters to cover all possible logical branches (true, false, empty).
2.yesnoCan the filter handle non-boolean variables, such as numbers or strings?Answer:yesnoThe filter mainly targets boolean values (true/false) and null values (nilDesign.When you pass a non-boolean variable, the AnQi CMS template engine will try to implicitly convert it to a boolean value for evaluation.true, while zero value (0) and empty string ("") may be consideredfalse. However, to ensure the clarity of the template's logic and expected output, it is recommended to ensure thatyesnothe variable of the filter is already a clear boolean type.
3.yesnoFilter and Usage{% if ... %}{% else %}{% endif %}What is the difference between using tags for conditional judgment?Answer: The main difference between them lies in their purpose and conciseness.{% if ... %}{% else %}{% endif %}Labels are used to control the rendering logic of larger code blocks, displaying one piece of content when conditions are met and another when they are not.yesnoFilters are more focused oninline outputConvert a variable's boolean state or null value directly to a brief text. For scenarios where only simple states like "yes/no/unknown" need to be displayed,yesnoThe filter greatly simplifies the code, making it more concise and readable, and avoids unnecessary lengthy conditional statements.