In AnQiCMS template development, we often encounter situations where we need to judge whether a variable is true, false, or even does not exist or is empty. In order to deal with these scenarios in a concise and efficient manner, AnQiCMS provides a very practical built-in filter—yesno。It can elegantly recognize the three core states of variables and output preset or custom text based on these states, greatly simplifying the conditional logic in the template.

yesnoFilter working principle: Three-state judgment

yesnoThe core of the filter lies in its three-state judgment mechanism of 'OR and NOT', which allows it to handle various forms of variables with ease. Specifically, it will judge the variable status according to the following rules:

  1. Variable is true (true) when When the value of the variable is evaluated as a boolean typetruewhenyesnoThe filter will output “yes”.This usually applies to explicit boolean fields, or non-empty strings, non-zero numbers, and other values that are implicitly converted to true by the AnQiCMS template engine.

  2. Variable is false (false) when Conversely, if the value of the variable is evaluated as a boolean typefalseWhen, the filter will output 'no'. This includes explicit booleanfalse, or a number0An empty string""etc.

  3. The variable is an empty value or undefined (nil) when: ThisyesnoFilter especially useful places. When the variable is a null value (commonly represented as null in Go language).nil)、Not defined in the template context, or is an empty array, empty slice, or empty dictionary (map).yesnoFilter will judge it as the third state, default output 'maybe'.This judgment ability allows us to easily distinguish between 'false' and 'non-existent or empty', which is very critical in many business scenarios.

Basic usage method

yesnoThe usage of the filter is very intuitive, simply add it to the end of the variable, and use the pipe character.|Connect them.

For example, if we have a variable namedarchive.StatusThe variable may represent the publication status of the article:

<p>文章状态:{{ archive.Status | yesno }}</p>

Based onarchive.StatusThe actual value, which may be displayed here:

  • Ifarchive.Statusresponse fortrue,output文章状态:yes
  • Ifarchive.Statusresponse forfalse,output文章状态:no
  • Ifarchive.Statusresponse fornilor undefined, output文章状态:maybe

Custom output text

The default "yes", "no", "maybe" may not be intuitive or consistent with the page style in some scenarios.yesnoThe filter allows us to customize the output text for these three states. You only need to provide three separated by commas after the filter.,The string is separated, corresponding to the output when the value is 'true', 'false', and 'empty or undefined'.

<p>文章发布状态:{{ archive.Status | yesno:"已发布,草稿,待审核" }}</p>

In this example:

  • Ifarchive.Statusresponse fortrue,output文章发布状态:已发布
  • Ifarchive.Statusresponse forfalse,output文章发布状态:草稿
  • Ifarchive.Statusresponse fornilor undefined, output文章发布状态:待审核

It should be noted thatIf you provide custom text, you must provide three values in the order of 'true text,false text,empty text'.If only two are provided, then a default 'maybe' will be output when the variable is null.

Actual application scenarios

yesnoThe filter can be applied to various scenarios in the AnQiCMS template, especially when it is necessary to simply display the variable status:

  • Check if the content is recommended or pinned:The article model has a custom boolean field.is_featured.

    <p>是否推荐:{{ archive.is_featured | yesno:"是,否,未设置" }}</p>
    
  • Check if a feature of the website is enabled:In system settings, we may define a parameterSiteCloseto control whether the website is closed.

    <p>网站状态:{{ system.SiteClose | yesno:"已关闭,正常运行" }}</p>
    

    Here are only two parameters provided, whensystem.SiteCloseresponse fortrue(Closed) is displayed as "Closed", forfalse(Not closed) is displayed as "Normal operation". Ifsystem.SiteCloseThe content is not set or is empty, and the default “maybe” will be displayed.

  • Processing may be empty user information:When displaying user information, if a field (such as the user's email address) may be empty or undefined.

    <p>用户邮箱:{{ user.Email | yesno:user.Email+",暂无邮箱,未注册" }}</p>
    

    In this example, ifuser.EmailIf there is a value, it will directly display the email address; ifuser.Emailresponse forfalse(For example, an empty string), then display 'No email'; ifuser.Emailresponse fornilor undefined, then display 'Not registered'. This flexible combination usage can help us target information display.

In short,yesnoThe filter is a simple yet powerful tool in AnQiCMS templates, which helps us handle various states of variables in an elegant way, avoiding complexif-elseNested, making template code more neat and readable, enhancing development efficiency and maintainability.


Common Questions (FAQ)

1.yesnoWhat types of variables can filters handle?

yesnoFilters are mainly used to process boolean values (true/false). But it is also intelligent, as for non-boolean types, it will try to convert them to a boolean context for judgment (for example, non-empty strings, non-zero numbers are usually considered as true; empty string, number0is consideredfalse). Most importantly, it can identifynil(null) or undefined variables, which is the key difference from simple boolean judgments.

2. If the variable does not existtrueNor is it.false(for example, a spelling error),yesnohow will it be displayed?

When the variable is completely absent in the template context (for example, if the variable name is spelled incorrectly, or if this field does not exist in the data model),yesnoThe filter will judge it as the third state - null or undefined.At this moment, it will output the default 'maybe', or the third text value you customize.This is very useful for handling data that may be missing or unexpectedly not existent, which can avoid template rendering errors.

3.yesnofilters andifWhat are the differences between logical judgment tags? When should which one be used?

ifLogical judgment tag ({% if ... %}) Mainly used to execute more complex conditional logic branches, such as displaying the entire content block, controlling loops, or changing the template structure.yesnoThe filter focuses on directly and succinctly converting the three states of a variable into a string output. When you only need to display a piece of text, an icon, or a brief description based on the true/false or null state of a variable,yesnoThe filter is a more concise and efficient choice. When you need to render different large HTML blocks based on conditions or execute more complex logic,ifthe tag is more appropriate.