How to use the `yesno` filter of AnQiCMS to display 'yes', 'no', or 'unknown' status based on boolean values in the template?

Calendar 👁️ 67

In Anqi CMS template development, we often need to display different text in a user-friendly way on the front page based on the boolean (true/false) status of the background data, such as "yes" or "no". Display directlytrueorfalseMay seem too rigid, and writing complexif-elseConditional statements make template code look long-winded. Fortunately, AnQiCMS provides a concise and efficient solution—yesnoA filter that can help us easily convert boolean values to custom text states, even elegantly handling 'unknown' situations.

yesnoThe core role of the filter

yesnoA filter is a tool used to map the boolean nature of a variable to a specific string. Its main purpose is to maptrue/falseornil/The value (often considered as 'empty' or 'undefined' in programming) is converted to our preset text output. This greatly enhances the readability and conciseness of the template.

The principle of operation: intelligent mapping of boolean values

yesnoThe filter intelligently judges the 'true', 'false', or 'empty' status of variables and returns the corresponding text based on these judgments. Its default mapping rules are as follows:

  • When the value of the variable is evaluated astruethen the filter defaults to returning a string"yes".
  • When the value of the variable is evaluated asfalsethen the filter defaults to returning a string"no".
  • when the value of the variable isnil or empty value(for example, an unset variable, an empty string)""or a number0etc.), the filter defaults to returning a string"maybe".

This trinary mapping mechanism is very flexible and can cover most of our daily development needs.

How to useyesnoFilter

UseyesnoThe filter is very simple, it follows the general syntax of the filters in the AnQiCMS template:{{ 变量 | 过滤器名称 }}.

1. Default usage

If you do not specify any custom text,yesnoThe filter will use its default “yes”, “no”, “maybe” as output.

{# 假设 article.IsPublished 为 true #}
<p>文章发布状态:{{ article.IsPublished | yesno }}</p> {# 输出: 文章发布状态:yes #}

{# 假设 user.IsVip 为 false #}
<p>用户VIP状态:{{ user.IsVip | yesno }}</p> {# 输出: 用户VIP状态:no #}

{# 假设 archive.Status 为 nil 或未定义 #}
<p>归档状态:{{ archive.Status | yesno }}</p> {# 输出: 归档状态:maybe #}

2. Custom output text

yesnoThe real strength of the filter lies in its ability to customize the output text for these three states. You just need to use a colon after the filter name.:Pass a comma-separated string,representing the text for "true", "false", and "null".

For example, if we want to display "yes", "no", or "unknown":

{{ 变量 | yesno:"真值文本,假值文本,空值文本" }}

Let us take the example of whether an article is pinned:

{# 假设 article.IsTop 为 true #}
<p>是否置顶:{{ article.IsTop | yesno:"是,否,未知" }}</p> {# 输出: 是否置顶:是 #}

{# 假设 article.IsTop 为 false #}
<p>是否置顶:{{ article.IsTop | yesno:"是,否,未知" }}</p> {# 输出: 是否置顶:否 #}

{# 假设 article.IsTop 变量不存在或为 nil #}
<p>是否置顶:{{ article.IsTop | yesno:"是,否,未知" }}</p> {# 输出: 是否置顶:未知 #}

If you only need to handle the two states of 'yes' and 'no', you can only provide two parameters:

{# 假设 comment.IsApproved 为 true #}
<p>评论审核通过:{{ comment.IsApproved | yesno:"已通过,待审核" }}</p> {# 输出: 评论审核通过:已通过 #}

{# 假设 comment.IsApproved 为 false #}
<p>评论审核通过:{{ comment.IsApproved | yesno:"已通过,待审核" }}</p> {# 输出: 评论审核通过:待审核 #}

{# 假设 comment.IsApproved 为 nil (同样会输出“待审核”,因为它会匹配第二个参数作为所有非真值的处理) #}
<p>评论审核通过:{{ comment.IsApproved | yesno:"已通过,待审核" }}</p> {# 输出: 评论审核通过:待审核 #}

Note:If only two parameters are provided, the second parameter will be processed at the same timefalseandnil/The case of null values. If it is necessary to distinguishfalseandnil/If null values, please be sure to provide three parameters.

Examples of actual application scenarios

yesnoThe filter is widely used in AnQiCMS templates, it can replace many long-windedif-elsestructures, making the template code clearer and easier to read.

  1. Display of article publishing status:

    <p>发布状态: {{ archive.Published | yesno:"已发布,草稿,待定" }}</p>
    
  2. User VIP or membership status:

    <p>会员等级: {{ user.IsVip | yesno:"尊贵会员,普通用户,状态异常" }}</p>
    
  3. Product inventory status:(Assuming there is)product.InStockfield)

    <p>库存状态: {{ product.InStock | yesno:"有货,缺货,未知" }}</p>
    
  4. Result of checkboxes or radio buttons in the simplified form:When the form data obtained from the backend is a boolean value, it can be quickly converted to user interface display.

Compared with the traditional{% if ... %}{% else %}{% endif %}statements,yesnoThe filter handles simple boolean judgments and returns the corresponding text, which results in less code and is easier to maintain and understand, especially when frequently displaying such states in templates.

Summary

yesnoThe filter is a seemingly insignificant but powerful tool in the AnQiCMS template engine.It achieves an elegant transformation from boolean values to custom text through concise syntax, effectively enhancing the neatness and readability of template code.Master this filter to help you present dynamic content more efficiently in AnQiCMS development.


Frequently Asked Questions (FAQ)

Q1:yesnoWhat is the default output text of the filter? A1: yesnoWhen the filter does not have custom parameters, it defaults to convertingtrueto “yes”,falseto “no”, andnilIf empty, it will be converted to 'maybe'.

Q2: If I only want to display 'yes' and 'no' states and not distinguishfalseandnilHow should the empty value be set? A2:You can only provide two custom parameters. For example{{ 变量 | yesno:"是,否" }}In this case,trueIt will display "Yes", whilefalseandnilEmpty values will display "No".

Q3:yesnoCan the filter handle non-boolean data types? For example, a number or a string? A3: yesnoThe filter is mainly used to process boolean values or data that can be evaluated as boolean in Go language (for example, non-empty strings, non-zero numbers are usually considered "true", empty strings, zero numbers are considered "false").However, practice is to apply it to explicit boolean types or data that you want to be handled with boolean semantics to avoid unexpected results.If you need to perform more complex conditional judgments on non-boolean data,ifTags would be a more suitable choice.

Related articles

How does the AnQiCMS `wordwrap` filter implement intelligent automatic line breaks for long English paragraphs?

In daily content operations, we often encounter such a scenario: when long English paragraphs are published, they may exceed the container width on different devices or screen sizes, causing horizontal scroll bars to appear, which greatly affects the user's reading experience and the beauty of the page.AnQiCMS (AnQiCMS) fully understands this pain point and has provided a very practical template filter - `wordwrap`, which can cleverly solve the problem of automatic line breaking for long text.

2025-11-07

How to count word numbers with the `wordcount` filter of AnQiCMS when processing mixed Chinese-English text?

In Anqi CMS template design, the `wordcount` filter is a practical tool used to count the number of words in the text.For operation personnel and content creators, understanding the working principle, especially the statistical logic when dealing with mixed Chinese and English text, can help us accurately assess content length, optimize article structure, and better meet the needs of search engine optimization (SEO) and user reading experience.### `wordcount` filter basic usage The `wordcount` filter is very straightforward to use

2025-11-07

How to URL encode query parameters in AnQiCMS template to avoid conflicts with special characters?

When building dynamic links in the AnQiCMS template, we often need to pass variables as query parameters in the URL.For example, a search results page may need to pass the user's search term as a parameter;A category filter page may need to include the selected category ID or multiple filter conditions.However, these dynamic contents often contain special characters, such as spaces, `&`, and `?`Backticks, equals, slash, hash, etc., they have specific meanings in URLs.If these special characters are not processed, the browser or server may not be able to correctly parse the URL, resulting in a page error

2025-11-07

AnQiCMS `trim` family filter can delete which custom characters besides spaces?

In AnQi CMS template design, we often need to process the displayed data to ensure that the content presented to the user is both beautiful and accurate.Among them, string processing is an indispensable part of content operation.AnQi CMS provides a series of flexible filters that help us easily complete these tasks, and the `trim` family of filters is one of the very practical ones. At first, we might think that the `trim` filter is mainly used to remove whitespace characters from the beginning and end of a string, such as extra spaces or newline characters.

2025-11-07

How to get different thumbnail image addresses by using a filter in AnQiCMS template?

When using AnQiCMS for website content management, image management and optimization is a key factor in improving user experience, accelerating page loading speed, and enhancing SEO performance.Especially in the process of template development, we often encounter the need to obtain addresses of different size thumbnails.AnQi CMS provides a straightforward way to handle these issues with its concise and efficient design concept.Today we will delve deeply into how to cleverly use built-in filters in AnQiCMS templates to obtain different thumbnail addresses for the images we upload

2025-11-07

How to ensure that the AnQiCMS website content can adapt to display on different devices?

With the popularity of mobile internet, the types of devices used by users to access websites are increasingly diverse, ranging from desktop computers to tablet computers, and various sizes of smartphones. Whether the content of a website can be displayed smoothly and beautifully on different devices has become an important standard for evaluating the quality of a website.AnQiCMS (AnQiCMS) was designed with this need in mind, offering flexible and diverse solutions to ensure that website content can easily achieve adaptive display on multiple devices.

2025-11-07

What front-end display modes does AnQiCMS support to manage PC and mobile websites?

In the digital age, it is crucial to provide an excellent user experience whether visitors browse your website on a computer or mobile phone.A high-efficiency content management system should be able to flexibly meet this diverse display demand.AnQiCMS is designed for this, it provides various front-end display modes, allowing you to finely manage the display effects of PC and mobile websites according to your actual business needs.AnQiCMS provides three main display modes on the website frontend, each with unique advantages and applicable scenarios, helping you build a modern website adaptable to different devices

2025-11-07

How to customize the display layout and template file path for articles (or other content) in AnQiCMS?

In AnQiCMS, your website content display has high flexibility.Whether it is an article, product details, or an independent page, you can tailor the display layout and template file path according to the brand image, content characteristics, or specific operational needs.This design concept of AnQiCMS aims to help users break free from the constraints of traditional CMS and have more freedom to control the visual presentation and user experience of the website.

2025-11-07