In the world of AnQi CMS templates, we often encounter situations where variables may not have values.For example, a user's nickname may be empty, the introduction of an article may not be filled in, or a certain configuration item may not exist at all.At this time, in order to make the page display more friendly and provide complete information, we need to set a 'backup' for these variables - that is, the default value.The Anqi CMS template engine (which cleverly draws on the essence of Django template syntax) provides us with two powerful filters to handle this situation:defaultanddefault_if_none.

As an experienced website operations expert, I am well aware of the importance of the robustness of variables in templates for content presentation.Today, let's delve into the subtle yet crucial differences between these two seemingly similar filters that handle default variable values.Understanding the differences can help us control the display logic of page content more accurately, improving the user experience.

defaultFilter: It appears when the variable "empty"

Let's talk about firstdefaultFilter. Its design philosophy is very intuitive: If a variable is considered 'empty' by the template engine, it will display the default value you set.The term 'empty' here does not simply refer to the absence of a variable.

In the template context of AnQi CMS, a variable isdefaultconsidered as 'empty' under the filter conditions including:

  • Variable isnil(orNone): This is the most common 'no value' state.
  • The variable is an empty string"": Even if the variable exists, but its content is empty,defaultit will intervene.
  • The variable is a number0: For numeric types,0is usually also considered as an 'empty' value.
  • The variable is a booleanfalse: Logical false values are also considered as 'empty'.
  • The variable is an empty list or mapping (array or dictionary): If a collection type variable is empty,defaultit will also provide a default value.

Let's look at some actual examples and feel itdefaultwith wide applicability:

Suppose we have the following variable states:

  • user_name = "张三"
  • post_abstract = ""(empty string)
  • view_count = 0(zero)
  • is_featured = false(Boolean false)
  • settings_value = nil(or if the variable is not defined at all, it is equivalent to)nil)

Using the templatedefaultFilter:

{{ user_name|default:"匿名用户" }}
{# 输出:张三 #}

{{ post_abstract|default:"暂无简介" }}
{# 输出:暂无简介 #}

{{ view_count|default:"0次阅读" }}
{# 输出:0次阅读 #}
{# 注意:如果想区分'0'和'未定义',这里可能不是**选择 #}

{{ is_featured|default:"不推荐" }}
{# 输出:不推荐 #}

{{ settings_value|default:"配置未设置" }}
{# 输出:配置未设置 #}

From these examples, it can be seen thatdefaultThe filter is very accommodating when handling various "empty" values, it's like a hardworking substitute player, as soon as it finds an empty position on the field, whether the main player is absent or not in good condition, it will immediately step in.

default_if_noneFilter: accurately identify the 'null' value status

Next, we focus ondefault_if_noneFilter. AnddefaultDifferent from the general,default_if_noneThe design philosophy isAccurately identify whether the variable isnil(i.e.),None).

This means,default_if_noneOnly check if the variable is in a state of 'completely without value'. If the variable exists, but its content is an empty string""numbers0or a boolean valuefalse,default_if_noneThese are considered to have a value, and the value of the variable itself will be displayed, rather than applying a default value.

Let's continue using the variable state we just had, and seedefault_if_nonePerformance:

{{ user_name|default_if_none:"匿名用户" }}
{# 输出:张三 #}

{{ post_abstract|default_if_none:"暂无简介" }}
{# 输出:(空字符串,不会显示"暂无简介") #}
{# 注意:这里与default过滤器行为不同! #}

{{ view_count|default_if_none:"未统计" }}
{# 输出:0 #}
{# 注意:这里与default过滤器行为不同! #}

{{ is_featured|default_if_none:"未知状态" }}
{# 输出:false #}
{# 注意:这里与default过滤器行为不同! #}

{{ settings_value|default_if_none:"配置未设置" }}
{# 输出:配置未设置 #}

By comparison, we can clearly seedefault_if_noneThe rigor of it. It does not consider an empty string, a number 0, or a boolean false as a 'null' state that requires a default value. It only cares whether a variable truly 'does not exist' or not.nil.

Application scenario: When to choose which one?

Understanding their differences makes it simple and strategic to choose the appropriate filter:

  • SelectdefaultFilterWhen you want to include all forms of "empty" values such asnil, an empty string, a number0and BooleanfalseA list or map that triggers the default value whendefaultis your preference. For example, displaying the publish time of a blog post, if the post does not have an explicit publish time, you want to display 'Unknown publish date';Or an optional field, if nothing is filled in (including manually deleting and clearing), a unified prompt will be displayed.

  • Selectdefault_if_noneFilter: When you need to strictly distinguish between a variable that is truly emptynil) or one that is intentionally set to a null value (such as an empty string)""or numbers0),default_if_noneIt comes into play. For example, if a product's price field is not set (nil), it displays "Price to be determined"; but if the price is explicitly set to0Free, it will be displayed directly0instead of 'Price to be determined'. For example, if a user has never set a personal signature,nil), show 'TA is mysterious'; if the user has set but left it blank(""), then it may be desired that the page does not display any signature, or display another paragraph of text to distinguish.}

In summary,defaultA filter provides a more general and more error-tolerant default value handling mechanism,default_if_noneThen it provides a more accurate and strict 'has or not' judgment. As an Anqi CMS operator, flexibly using these two filters will make your website content display more logical and expressive, and reduce poor page experience caused by data missing.


Frequently Asked Questions (FAQ)

  1. Question:defaultA filter will treat numbers0or booleanfalseas 'empty'? Answer:Yes, in the Anqi CMS template engine,defaultA filter will treat numbers0and Booleanfalsean empty string""as well asnilAll are treated as 'empty' values, and in this case, the default value you set will be displayed.

  2. Question: I want to show no content when the variable is an empty string""and when the variable is not empty.nilWhat should be done when the default value 'No data' is displayed? Answer:In this case, you need to usedefault_if_nonea filter. It only takes effect onnilthe value. If the variable is an empty string"",default_if_noneIt will be treated as 'having a value', so it will directly display an empty string (i.e., nothing will be displayed). For example:{{ variable|default_if_none:"暂无" }}.

  3. Question: Will these filters change the original type of the variable? For example, by changingnilDoes the variable become a string type after it is set to a default string? Answer:No.defaultanddefault_if_noneThe filter only affects the 'display' style of variables in the template.They are the logical processing of the view layer, and they will not change the actual data type or value of the variable in the backend program.The variable retains its original type and value after being processed by the filter in the template.