During the template creation process in Anqi CMS, flexibly using variables and their default values to output is the key to enhancing template robustness and user experience. When you handle situations that may not exist (nilOr when a variable is empty,defaultanddefault_if_noneThese filters are the tools you commonly use. Although they can all provide a substitute output when the variable has no value, they have subtle and important differences in the criteria for determining 'no value'.
To understand the difference between the two, we first need to clarify the meaning of the two states of "empty" in the template context:nil"""
- Empty"In most programming and template languages, a variable is considered to be 'empty' in a broad sense. It may be an empty string (such as
""), a numeric zero (such as0), or a boolean valuefalseAn empty array or slice ([]) An empty map or object ({}) And the most extreme 'no value' state -nil. You can imagine it as an empty container, the container itself exists, but there is nothing inside. - "
nil"This is a more specific and stricter 'no value' state.It usually refers to a variable that has not been initialized, or the memory address it points to is empty, indicating that there is no such 'container'.In the context of Go language (the development language of AnQiCMS),nilRepresents the zero value of pointer, interface, slice, map, channel, or function types, meaning the 'non-existence' of these type variables.
After understanding these two states, let's take a look at how these two filters work.
defaultFilter: Widespread 'empty' processing
defaultThe filter is one of the most commonly used. Its design philosophy is relatively "lenient": as long as a variable is judged as "empty" by the template engine, it will output the default value you set.This 'empty' encompasses all the cases mentioned previously:
- When the variable is
nilhour - When the variable is an empty string
""hour - When the variable is a number
0hour - When the variable is a boolean value
falsehour - When the variable is an empty array or object
For example, if you have a variable that displays the user's nicknameuser.nickname:
{# 如果 user.nickname 为 nil、""、0、或 false,都会显示“访客” #}
{{ user.nickname|default:"访客" }}
{# 如果 article.views 为 nil 或 0,都会显示“0 次阅读” #}
{{ article.views|default:"0 次阅读" }}
in most cases,defaultThe filter meets our need for all variables that 'look like they have no content' to have a friendly default output, eliminating complex condition checks.
default_if_noneFilter: Precise 'no value' processing
Compared with that,default_if_noneThe filter is more rigorous and precise. It only evaluates the value of the variable when it is strictly judged to benilThis will take effect only when it is the true 'non-existent' or 'no value' state, and output the default value you set.
This means, if the variable is an empty string""or a number0or a boolean valuefalse,default_if_noneIt is considered that these are "valuable" but "contentless" states, it will not trigger but will output these values as is.
Let's continue with the example of a user nickname:
{# 如果 user.nickname 是 nil,显示“访客”;如果 user.nickname 是空字符串 "",则原样显示 "" #}
{{ user.nickname|default_if_none:"访客" }}
{# 如果 article.last_editor 是 nil,显示“未知编辑者”;如果 article.last_editor 是空字符串 "",则原样显示 "" #}
{{ article.last_editor|default_if_none:"未知编辑者" }}
Through this filter, you can control the output behavior of the template more precisely, distinguishing between 'not set' and 'set but content is empty' two different situations.
Summary and selection of core differences
In simple terms,defaultThe filter acts like a lenient goalkeeper, as long as the variable looks like 'nothing', it passes on the default value. Anddefault_if_noneit is a strict inspector, it only checks for those that are truly 'non-existent'(nilOnly variables that are closed will be handled, for those that exist but are empty, it will stand aside.
When to choosedefault?When you want to handle all non-meaningful values (includingnilWhen a default alternative is displayed for empty strings, 0, false, empty arrays/object)defaultThis is the most common requirement, which can ensure that your page will not appear blank or erroneous due to missing data. For example