In website operation, we often need to display corresponding text prompts on the front page according to the status of the content, such as whether it is enabled, recommended, or online, and directly output the boolean value returned by the backend.trueorfalseIt may not be intuitive and friendly. AnQiCMS (AnQiCMS) provides a simple and powerful tool for the template engine——yesnoA filter that can help us elegantly convert the existence status of these boolean values or field states into easily understandable custom text, such as 'Enabled/Disabled/Pending'.
UnderstandingyesnoThe core role of the filter
yesnoThe core function of the filter is to automatically convert the data status returned by the backend into the text output we preset. It mainly identifies three states:
- Logical True Value Usually corresponds to a boolean value
true. - Logical False Value Usually corresponds to a boolean value
false. - Nil/Empty ValueWhen a variable is
nilAnd an empty string (""), or is not defined at all.
By this filter, we can avoid writing complexif-elselogic to handle these common state judgments, making the code cleaner and more efficient.
Basic usage: default output
yesnoThe simplest way to use a filter is to apply it directly to a variable, which will output 'yes', 'no', or 'maybe' by default.
For example, assume we have a variableuserActive:
- If
userActiveThe value istrueThe output will beyes. - If
userActiveThe value isfalseThe output will beno. - If
userActiveThe value isnilOr undefined, the output will bemaybe.
You can use it in the template like this:
{# 假设 userActive 为 true #}
<span>用户状态:{{ userActive|yesno }}</span> {# 输出: 用户状态:yes #}
{# 假设 userActive 为 false #}
<span>用户状态:{{ userActive|yesno }}</span> {# 输出: 用户状态:no #}
{# 假设 userActive 为 nil 或未定义 #}
<span>用户状态:{{ userActive|yesno }}</span> {# 输出: 用户状态:maybe #}
This default output may be sufficient in some scenarios, but more often than not, we hope to output text with more business meaning.
Custom output: Implement 'Enabled/Disabled/Pending'
To meet more diverse display needs,yesnoThe filter allows us to customize the output text of these three states. You just need to provide a comma-separated string after the filter, corresponding totrue/falseandnil/emptyoutput.
The syntax format is as follows:
{{ 变量名|yesno:"自定义True值,自定义False值,自定义Nil值" }}
Now, let's tackle the specific requirement of 'Enabled/Disabled/Pending':
{# 假设 userActive 为 true #}
<span>用户状态:{{ userActive|yesno:"已启用,未启用,待定" }}</span> {# 输出: 用户状态:已启用 #}
{# 假设 userActive 为 false #}
<span>用户状态:{{ userActive|yesno:"已启用,未启用,待定" }}</span> {# 输出: 用户状态:未启用 #}
{# 假设 userActive 为 nil 或未定义 #}
<span>用户状态:{{ userActive|yesno:"已启用,未启用,待定" }}</span> {# 输出: 用户状态:待定 #}
In this way, we can easily convert backend data into user-friendly display text.
Scenario: TransferyesnoFilter applied to AnQiCMS template
In actual application of AnQiCMS,yesnoThe filter can be applied to various aspects, such as the management of articles, products, categories, and other content.
Scenario one: Display whether articles or products are enabled.
Assuming in your content model (such as an article modelarchive) you have customized a boolean fieldIsActiveto indicate whether the content is enabled. When editing an article, this field might betrueorfalseIf not set (for example, a new custom field has not been initialized), it may benilor empty.
You can use in the template of the article detail page or list pageyesnoA filter to visually display its status:
{# 在文章详情页模板中 (例如 archive/detail.html) #}
{% archiveDetail currentArchive with name="Title" %}
{% archiveDetail isActiveStatus with name="IsActive" %}
<article>
<h1>{{ currentArchive }}</h1>
<p>启用状态:<span class="status-indicator">{{ isActiveStatus|yesno:"已启用,未启用,待定" }}</span></p>
{# ...其他内容... #}
</article>
here,currentArchiveAssuming it is the title of the article on the current page, whileisActiveStatusIt is then from the custom fieldIsActiveThe value obtained.
Scenario two: Determine whether a feature configuration is enabled
Safe CMS system settings (help-setting-system.mdIt usually includes some configuration items, whose values may be boolean or determined by their existence. For example, you may have a custom system parameterEnableCommentControl whether comments are enabled.
”`twig {# In a certain functional area of the page, determine whether to display the comment feature based on system settings #} {% system enableCommentSetting with name=“EnableComment” %}
{% if enableCommentSetting %}
<div