In the template creation process of AnQi CMS, flexibly using variables and their default values to output is the key to improving template robustness and user experience. When you handle the possibility that some may not exist (nil)or when the content is empty,defaultanddefault_if_noneThese filters are the tools you often use.Although they all provide an alternative output when the variable has no value, the standards for determining 'no value' have subtle yet important distinctions.
To understand the difference between these two, we first need to clarify the meaning of the states 'empty' and '' in the template context:nilThe meaning of these two states:
- 'Empty'In most programming and template languages, a variable is considered "empty" in a broad sense. It may be an empty string (like
""), a numeric zero (like0), or a boolean valuefalseAn empty array or slice ([]), an empty mapping or object ({}), and the most thorough 'no value' statenilEnglish: 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 typically 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'.nilrepresented by the zero value of pointer, interface, slice, map, channel, or function types, meaning the 'non-existence' of these type variables.
Understood these two states, let's take a look at how these two filters work.
defaultFilter: Widespread 'empty' processing
defaultFilter is one of the most commonly used ones.Its design philosophy is quite “tolerant”: as long as a variable is judged as “empty” by the template engine, it will output the default value you set.
- 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 an empty 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' handling
In contrast,default_if_noneThe filter is more stringent and precise. It only checks if the value of the variable is strictly judged asnilThis is effective only when the value is truly 'non-existent' or 'null', and then it outputs the default value you set.
This means that if the variable is an empty string""or a number0or a boolean valuefalse,default_if_noneIt will consider these as "having a value" but "empty content", it will not trigger but will output these values as is.
Continuing with the example of the 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 finely, distinguishing between the two different situations of 'not set' and 'set but content is empty.'
Summary of core differences and selection
In simple terms,defaultThe filter acts like a lenient gatekeeper, providing a default value as long as the variable looks like it has 'nothing'.default_if_noneIt is a strict inspector, it only checks those that are indeed 'non-existent'.nilThe variable is taken out when it exists but the content is empty, and it stands by idly for those that exist but have no content.
When to choosedefault?When you want to take out all non-meaningful values (includingnilWhen an empty string, 0, false, or an empty array/object are displayed, a default alternative is used.defaultThis is the most common requirement, which ensures that your page will not appear blank or erroneous due to missing data. For example