In the template development of Anqi CMS, flexibly handling various data states is the key to building dynamic pages.We often encounter situations where we need to display different content based on the true or false state of a variable.yesnoFilter has become a very practical tool. It can help us convert Boolean logic into more readable text output.

Deep understandingyesnoFilter

yesnoThe filter is a small but powerful template tool that serves as the core function to judge the status of a variable and returns a preset string based on this status. By default,yesnoFilter defines three states:

  1. When a variable is evaluated asTrueit will return"yes".
  2. When a variable is evaluated asFalseit will return"no".
  3. 而当变量处于 English空值(nil) English状态时,它的默认行为就显得尤为重要,这正是我们今天探讨的重点。 English

nilthe value inyesno过滤器中的默认行为 English

In the template environment of AnQi CMS,nilrefers to a variable not being assigned or pointing to an empty object, essentially representing a state of "non-existence" or "unknown". WhenyesnoFilter encounters thisnilWhat will it do?

The answer is: By default,yesnoThe filter will return"maybe".

This means, if you call a possibly empty variable in a template and applyyesnoFilter without providing any custom parameters, then when this variable is indeed empty, the page will display"maybe".

For example, we have a nameduser.IsOnlineThe variable, which may represent whether the user is online. If the current value of this variable isnil(For example, the system is temporarily unable to retrieve the user's online status, or the user data itself does not contain this field), then it will be used like thisyesnoFilter:

用户在线状态:{{ user.IsOnline|yesno }}

Inuser.IsOnlineresponse fornilIn this case, the final page will display:

用户在线状态:maybe

This default 'trinary' processing method provides a simple and direct way to deal with the uncertainty of data, avoiding simply displaying blank or causing template errors, thereby enhancing user experience and code robustness.

Flexible CustomizationyesnoFilter Output

AlthoughyesnoFilters Provide"yes"/"no"/"maybe"As the default output, but in actual application, we often need more contextually appropriate descriptions.yesnoThe filter allows us to customize the return values of these three states.

You can specifytrue_value,false_value,nil_valuein this order.

For example, if you want to display “Yes”, “No”, or “Not set”:

文件上传状态:{{ file.IsUploaded|yesno:"是,否,未设置" }}

Iffile.IsUploadedresponse fortruedisplay “Yes”.falsedisplay “No”.nildisplay “Not set”.

It is worth noting that even if you only provide two custom values, such as:

特色功能:{{ item.HasSpecialFeature|yesno:"已启用,未启用" }}

Whenitem.HasSpecialFeatureresponse fornilwhenyesnoThe filter will still intelligently return its built-in third default state -"maybe"Instead of blank or error. Only when you explicitly provide a custom value (as the third parameter) for the status, will it output according to your settings.nilIt will output according to your settings only when you provide a custom value (as the third parameter) for the status.

This mechanism ensures that your template can always provide an understandable status indication regardless of how incomplete the data is, which is crucial for building a reliable and user-friendly CMS website.


Common Questions (FAQ)

Q1:yesnoFilter can recognize whether Go language contains?falseBoolean value?A1: Yes,yesnoFilter can accurately identify whether Go language contains?trueandfalseBoolean value. When the variable's value isfalseWhen it is, it will default return"no", or when you customize the output and return the one you customizedfalseThe second string set for the status.

Q2: Can IyesnoOnly customize in the filtertrueandfalsethe output, but not customizenilWhat is the output?A2: It is okay. If you only provide two custom parameters (such as{{ some_value|yesno:"真,假" }})yesnoThe filter will map the first parameter totrue, and the second parameter tofalse. When encounteringnil值时,它会回退到其默认的 Englishnil输出,也就是 English"maybe".

Q3: Besidesnil,yesno过滤器还会将哪些值视为“未知”或“空”状态? EnglishA3:yesno过滤器主要针对 Go 语言中的 EnglishnilType is processed specially. For other values evaluated as "empty" but notnil(such as empty strings"", or a number0or empty arrays), they are usually evaluated asfalse, and therefore return"no"(or one you define)falseStatus corresponding string), rather than"maybe"."maybe"Status is not explicitlynilCases retained.