In website operation, we often need to display corresponding text prompts on the front page based on the status of some content, such as whether it is enabled, recommended, online, etc. We directly output the boolean values returned by the backendtrueorfalseMay not be intuitive and friendly. The template engine provided by AnQiCMS (AnQiCMS) offers a concise and powerful tool.yesnoA filter that helps us elegantly convert the existence status of these boolean values or fields into easily understandable custom text, such as 'Enabled/Disabled/Pending'.

UnderstandingyesnoThe core function of the filter

yesnoThe core function of the filter lies in its ability to automatically convert the data status returned from the backend into the text output we preset. It mainly identifies three states:

  1. Logical Truth (True Value): Usually corresponds to a boolean valuetrue.
  2. Logical Falsity (False Value): Usually corresponds to a boolean valuefalse.
  3. Empty/Undefined (Nil/Empty Value)When a variable isnil, an empty string ("") or not defined at all.

Through 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, suppose we have a variableuserActive:

  • IfuserActivehas a value oftrueThe output will be:yes.
  • IfuserActivehas a value offalseThe output will be:no.
  • IfuserActivehas a value ofnilor undefined, the output will bemaybe.

In the template, you can use it 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, we hope to output text with more business significance.

Customized output: Implementing 'Enabled/Disabled/Pending'

To meet more diverse display requirements,yesnoThe filter allows us to customize the output text for these three states. You just need to provide a comma-separated string after the filter, corresponding totrue/falseandnil/emptythe output.

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.

Practical scenario: ToyesnoApply filter to AnQiCMS template

In the actual application of AnQiCMS,yesnoThe filter can be applied to various aspects, such as the management of articles, products, categories, and more.

Scenario one: Display whether articles or products are enabled

Assuming in your content model (such as an article model)archive), you have customized a boolean fieldIsActiveto represent whether the content is enabled. When editing an article, this field may 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 page,yesnoFilter 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, whileisActiveStatusis the value obtained from the custom fieldIsActive.

Scenario two: Determine whether a feature configuration is enabled

The system settings in the Anqi CMS (help-setting-system.mdIt usually includes some configuration options, whose values may be boolean or determined by their existence. For example, you might have a custom system parameterEnableCommentControl whether comments are enabled.

In a certain functional area of the page, determine whether to display the comment feature based on the system settings # In a certain functional area of the page, determine whether to display the comment feature based on the system settings #

{% if enableCommentSetting %}

<div