How to use the `yesno` filter to output custom text such as 'Enabled/Disabled/Pending' based on the boolean value or the existence of a field returned by the Anqi CMS backend?

Calendar 👁️ 70

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:

  1. Logical True Value Usually corresponds to a boolean valuetrue.
  2. Logical False Value Usually corresponds to a boolean valuefalse.
  3. Nil/Empty ValueWhen a variable isnilAnd 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:

  • IfuserActiveThe value istrueThe output will beyes.
  • IfuserActiveThe value isfalseThe output will beno.
  • IfuserActiveThe 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

Related articles

The `addslashes` filter in AnQi CMS, how to escape a string that may contain special characters to safely insert into JS or HTML attribute values?

In the daily operation of Anqi CMS, we often need to display the dynamic content stored in the database on the front end of the website.This content may come from user input, data scraping, or other channels, and it is inevitable that it will contain some special characters.If these special characters are not handled properly and directly inserted into JavaScript code or HTML attribute values, it may cause page layout chaos, functionality failure, and even serious security risks, such as cross-site scripting attacks (XSS).

2025-11-08

How does the `add` filter in the Anqi CMS template implement the mixing of numbers and strings and how to handle type mismatches?

AnQiCMS (AnQiCMS) provides flexible and powerful tools for content creators and website developers with its Django-like template engine syntax.When building dynamic web content, we often need to combine different types of data (such as numbers and strings) together to form the final display effect.At this time, the `add` filter in the Anqi CMS template can come in handy.

2025-11-08

How to efficiently remove specific punctuation marks or spaces from a string using the `cut` filter in the Anqi CMS template to clean up the output content?

In the daily operation of AnQi CMS, we often encounter situations where we need to refine the content output by templates.To make the page display cleaner, improve the user reading experience, or generate URLs that are more beneficial for SEO, removing unnecessary punctuation or extra spaces from strings is a very practical skill.The powerful template engine of Anqi CMS provides a rich set of filters to help us achieve these goals, among which, the `cut` filter is a simple yet extremely efficient tool.### `cut` filter

2025-11-08

How to use `upper`, `lower`, `capfirst`, and `title` filters to unify the English title case format of the CMS frontend page?

In website operation, the professionalism and consistency of content display are crucial for improving user experience and brand image.Especially when dealing with English titles, consistent capitalization not only makes the page look neater, but also indirectly affects the readability of the content.AnQiCMS (AnQiCMS) relies on the powerful features of the Django template engine and provides several very practical filters to help us easily format the case of English titles on the front-end page.

2025-11-08

How to implement `striptags` and `removetags` filters in Anqicms, one for removing all HTML tags and another for removing specified tags?

In Anqi CMS, we often encounter scenarios where we need to handle HTML tags.To ensure the purity of content, meet display requirements, or ensure safety, it is an important ability to flexibly control HTML tags.AnQiCMS's powerful template engine provides the `striptags` and `removetags` filters, which are very useful and can help us easily remove all HTML tags or only remove specified tags.Next, we will delve into how these two filters work together

2025-11-08

In Anqi CMS template, how does the `random` filter implement the random selection of an element from an array or string to display and enhance the dynamic nature of the content?

Make the Anqi CMS website content vivid: explore the `random` filter, and uncover the mystery of dynamic display In the era of information explosion, an efficient and flexible website content management system is an indispensable tool for operators.The AnQi CMS is a system developed based on the Go language, dedicated to providing a high-performance, easily scalable content management solution. It uses a syntax similar to the Django template engine in template design, greatly simplifying the complexity of development and content presentation.But besides content publishing, we all hope that the website can maintain its freshness

2025-11-08

How to use the `first` and `last` filters to quickly get the title of the first or last article in the Anqi CMS article list?

In website operation, we often need to quickly extract some specific information from the article list, such as the homepage may need to display the latest article title, or in some special modules, it is necessary to obtain the title of the first or last entry in the article list.AnQiCMS (AnQiCMS) can easily meet these needs with its flexible template engine and rich filter functions.AnqiCMS's template system draws on the syntax of mainstream template engines like Django

2025-11-08

What are the application scenarios and encoding differences of the `urlencode` and `iriencode` filters in the AnQi CMS template when encoding URL parameters?

In AnQiCMS template development, when handling URL parameters, we often encounter the need to encode them.This is mainly to ensure the legality of the URL, avoid special characters from destroying the URL structure, and correctly transmit data containing non-ASCII characters (such as Chinese).AnQiCMS provides the `urlencode` and `iriencode` filters to help us complete this task, but their application scenarios and encoding differences are not the same.

2025-11-08