In the development of templates in the content management system, we often encounter situations where variable values are uncertain.Sometimes, the data may not be filled in; sometimes, the data may exist but its value is an empty string, a number zero, or a boolean false.To ensure the integrity and user experience of the website page display, it is particularly important to provide a friendly default display for these 'empty' values.

AnQiCMS provides us with two very practical filters to handle such situations:defaultFilters anddefault_if_noneFilters. They can all set default values for variables under certain conditions, but they have different focuses when judging whether a variable is "empty". Understanding these differences can help us control the display logic of templates more accurately.

defaultFilter: wide range of 'empty' value judgment

defaultThe filter is the most commonly used one. Its judgment logic is quite broad, treating various 'seemingly empty' values as cases that need to be replaced.In simple terms, as long as a variable's value is evaluated asfalse,defaultThe filter will replace it with the default value we specify.

These "empty" values usually include:

  • nil(null/pointer): The variable is completely unset or pointing to null.
  • An empty string ("")The variable is an empty text.
  • Number zero (0)The value of the variable is zero.
  • Boolean false (false)The logical value of the variable is false.
  • Empty list or array ([])A variable is a collection that does not contain any elements.
  • Empty dictionary or mapping ({})A variable is a collection that does not contain any key-value pairs.

Usage scenario:When you want any variable on the page that does not have actual content to display a unified default prompt,defaultThe filter is an ideal choice. For example, if the article title is not set, the product price is zero, or the user nickname is empty, you would like to display "No data available","Negotiable" or "Anonymous user", thendefaultThe filter can be used.

Example:

{# 如果 username 为 nil, "", 0, false, 或空列表/map,都会显示 "匿名用户" #}
<p>用户昵称:{{ username|default:"匿名用户" }}</p>

{# 如果 product_price 为 0 或 nil 或 "",都会显示 "待议价" #}
<p>商品价格:{{ product_price|default:"待议价" }}</p>

{# 如果 content_status 为 false 或 nil 或 "",都会显示 "内容待审核" #}
<p>内容状态:{{ content_status|default:"内容待审核" }}</p>

In these examples, whether the variable is truly 'missing' or its value itself represents some kind of 'empty' state (such as a price of zero, or a false state),defaultWill intervene and provide default values.

default_if_noneFilter: More precise judgment of 'no value'

default_if_noneThe filter is more accurate in the logic of judgment thandefaultThe filter is more strict. It mainly focuses on whether the variable is truly 'undefined' or 'not set' in the sense. In the implementation of AnQiCMS,default_if_noneThe filter handles two cases:

  • nil(null/pointer): The variable is completely unset or pointing to null.
  • An empty string ("")The variable is an empty text.

It should be noted that it strictly distinguishes fromnilThe behavior of (None) and empty strings is different in AnQiCMS'sdefault_if_noneIt will also consider an empty string as a 'null' state and replace it. But the key distinction is, itwon'tReplace the number zero (0) or a boolean false (false) and the like.

Usage scenario:When you need to distinguish between “No data provided” (nilor"") and “Data explicitly zero/false” (0orfalse),default_if_noneThe filter is particularly important. For example, you might want the user's points to be displayed as0Instead of 'No points', if the user's avatar path is empty or not set, a default placeholder image will be displayed.

Example:

{# 如果 username 为 nil 或 "",会显示 "未知用户"。但如果 username 赋值为 0 或 false,则会显示 0 或 false #}
<p>用户昵称:{{ username|default_if_none:"未知用户" }}</p>

{# 如果 user_score 为 nil 或 "",会显示 "暂无积分"。但如果 user_score 为 0,则会显示 0 #}
<p>用户积分:{{ user_score|default_if_none:"暂无积分" }}</p>

{# 如果 is_featured 为 nil 或 "",会显示 "否"。但如果 is_featured 为 false,则会显示 false #}
<p>是否推荐:{{ is_featured|default_if_none:"否" }}</p>

In this example, ifuser_scorethe value of the variable is0(indicating the user has 0 points),default_if_noneThe filter will be retained.0Not changing because0Is a clearly set number. Only whenuser_scoreIsnilor""It will be replaced with "No points".

Guide to Core Differences and Selection

In short,defaultThe filter is a 'big and comprehensive' null value handling solution, which treats all values logically considered 'empty' or 'false' equally. Anddefault_if_noneThe filter is more 'fine', it mainly targets the true 'no value' state (niland empty strings), while it will retain explicit assignments like0orfalsesuch as this.

Select a recommendation:

  • UsedefaultFilterWhen you want all variables that lack actual content or have false logic to display a default value. This is the most common default value setting requirement.
  • Usedefault_if_noneFilterWhen you need to strictly distinguish between a variable being "unassigned/empty string" or "explicitly assigned zero/false", if you wish to0andfalseRetained as a meaningful value, rather than being overwritten by the default value, thendefault_if_nonemore suitable.

Understanding the subtle differences between these two filters allows us to write more robust, more logically predictable code in AnQiCMS template development, thereby improving the display quality of website content and user experience.


Frequently Asked Questions (FAQ)

1.defaultFilters anddefault_if_noneThe filter will replace empty strings""?