In Anqi CMS template development, 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. At this time,yesnoThe filter has become a very practical tool. It can help us convert boolean logic into more readable text output elegantly.
Deep understandingyesnoFilter
yesnoThe filter is a small but powerful template tool, its core function is to judge the status of a variable and return the preset string according to this status. By default,yesnoThe filter defines three states:
- When the variable is evaluated asTruethen it will return
"yes". - When the variable is evaluated asFalsethen it will return
"no". - while the variable isa null (nil) valueWhen in state, its default behavior is particularly important, which is exactly what we are discussing today.
nildisplay the value inyesnoThe default behavior in the filter.
In the template environment of Anqi CMS,nilIt refers to a variable not being assigned or pointing to an empty object, essentially representing a state of 'non-existence' or 'unknown'. Whenyesnothe filter encounters thisnilvalue, how will it handle it?
The answer is: by default,yesnothe filter will return"maybe".
This means that if you call a variable that may be empty in a template and applyyesnoThe filter is provided without any custom parameters, then when this variable is indeed empty, it will be displayed on the page"maybe".
For example, we have a nameduser.IsOnlineThe variable, which may represent whether the user is online. If the current value of this variable isnilFor example, if the system temporarily cannot retrieve the user's online status or the user data itself does not contain this field, then it is used like thisyesnoFilter:
用户在线状态:{{ user.IsOnline|yesno }}
Inuser.IsOnlineWithnilIn this case, the final page will display:
用户在线状态:maybe
This default 'triple-state' processing method provides a simple and direct way to deal with data uncertainty, avoiding merely displaying blank or triggering template errors, thereby improving user experience and code robustness.
Flexible customizationyesnoOutput of the filter
AlthoughyesnoThe filter provides"yes"/"no"/"maybe"As the default output, but in practice, we often need more contextually appropriate descriptions.yesnoThe filter allows us to customize the return values of these three states.
You can followtrue_value,false_value,nil_valueto provide custom strings in order.
For example, if you want to display "Yes", "No" or "Not set":
文件上传状态:{{ file.IsUploaded|yesno:"是,否,未设置" }}
Iffile.IsUploadedWithtruedisplay "Yes";falsedisplay "No";nildisplay "Not set".
It is noteworthy that even if you only provide two custom values, such as:
特色功能:{{ item.HasSpecialFeature|yesno:"已启用,未启用" }}
Whenitem.HasSpecialFeatureWithnilthen,yesnoThe filter will still intelligently return its built-in third default state--"maybe"Instead of blank or error. Only when you provide a custom valuenilfor the state as the third parameter will it output according to your setting.
This mechanism ensures that regardless of how incomplete the data is, your template can always provide an understandable state indicator, which is crucial for building a reliable, user-friendly CMS website.
Frequently Asked Questions (FAQ)
Q1:yesnoCan the filter recognize Go language infalseBoolean values?A1: Yes,yesnoThe filter can accurately recognize Go language intrueandfalseBoolean values. When the variable's value isfalseit will default return"no"Or when you return the one you customized in your custom outputfalseThe second string set in the state.
Q2: Can I useyesnoOnly customize in the filtertrueandfalseThe output, not the customizationnilDon't customize the output?A2: It's 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 encounteringnilWhen a value is missing, it will fallback to its default.nilThat's the output, also known as."maybe".
Q3: Besidesnil,yesnoWhich values will the filter consider as 'unknown' or 'empty' states?A3:yesnoThe filter is mainly aimed at Go language.nilSpecial handling is performed for types that are evaluated as "empty" but notnilvalues (such as empty strings""numbers0or empty arrays), they are usually evaluated asfalse, and therefore return"no"(or one you define)falseThe string corresponding to the status is not"maybe"."maybe"The status is not explicitlynilThe situation is retained.