In the development of templates in content management systems, 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, the 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.

The AnQiCMS provides us with two very practical filters to handle this kind of situation:defaultfilters anddefault_if_noneFilter.They can all set default values for variables under certain conditions, but they differ in focus when judging whether a variable is 'empty'. Understanding these differences can help us more accurately control the display logic of the template.

defaultFilter: Broad "empty" value judgment

defaultFilter is one of the most commonly used ones.Its judgment logic is relatively broad, treating various values that 'look empty' as cases that need to be replaced.false,defaultThe filter will replace it with the default value we specify.

These 'empty' values usually include:

  • nil(No value/null pointer): The variable is completely unset or points to an empty.
  • 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.

Use cases:When you want any variable on the page that does not have actual content to display a unified default prompt,defaultThe filter is a good 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 availabledefaultThe 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 "missingdefaultEnglish will intervene and provide a default value.

default_if_noneFilter: More accurate judgment of 'no value'

default_if_noneFilter is more accurate in terms of judgment logic thandefaultThe filter is more stringent. It mainly focuses on whether the variable is truly a 'no value' or 'not set' state. In the implementation of AnQiCMS,default_if_noneThe filter handles two cases:

  • nil(No value/null pointer): The variable is completely unset or points to an empty.
  • Empty string (""):The variable is an empty text.

It should be noted that the behavior of (None) and an empty string is different from some other template engines that strictly distinguishnilin AnQiCMS'sdefault_if_noneWill also treat empty strings as "no value" status and replace them. But the key difference is,will notReplace the number zero (0) or boolean false (false) and so on.

Use cases:when you need to distinguish between “data not provided”nilor""and “data explicitly zero/false”0orfalsedefault_if_noneThe filter is particularly important. For example, you may 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_scorevariable's value is0(indicating the user has 0 points),default_if_noneThe filter will retain0The content remains unchanged, because0is a value set explicitly. It will only be replaced with "No points"user_scoreYesnilor""when this condition is met.

Core Differences and Selection Guide

In short,defaultThe filter is a "big and comprehensivedefault_if_noneThe filter is more 'fine', mainly targeting the true 'no value' stateniland empty strings), while retaining explicit assignments like0orfalsesuch as this.

Suggestion for selection:

  • UsedefaultFilterWhen you want all variables that are 'lacking in actual content' or 'logically false' to display a default value. This is the most common default value setting requirement.
  • Usedefault_if_noneFilterWhen you need to strictly distinguish whether a variable is an 'unassigned/empty string' or 'explicitly assigned zero/false'.0andfalseAs a meaningful value is retained rather than overwritten by the default value,default_if_noneit is more suitable.

Understanding the subtle differences between these filters can help us write more robust, more logical code in AnQiCMS template development, thereby improving the quality of website content display and user experience.


Common Questions (FAQ)

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