In AnQiCMS template development, we often need to handle situations where data may be empty to ensure the stability of page display and user experience. AnQiCMS provides a variety of template filters to meet such needs, wheredefaultanddefault_if_noneThese are two very practical tools when dealing with null values.They are all intended to provide a fallback value for missing or empty data, but there are subtle and crucial differences in the definition of "empty" between the two. Understanding these differences can help us control the template rendering logic more precisely.
defaultFilter: Broad placeholder for null values
defaultFilter is a general missing value processing tool.When the variable it acts on is evaluated as 'empty' in the template engine, the default value you provide will be used.
- Variable that does not exist (nil): If a variable has not been assigned at all, or at the level of Go language is
nil. - Empty string ("")An empty string.
- Zero (0): Whether it is an integer, a floating-point number or other numeric type, as long as the value is zero, it will be
defaultconsidered as empty. - Boolean value
false:If the boolean value of the variable isfalse. - an empty slice, map, or array:When a collection type of data does not contain any elements.
In short,defaultThe filter checks if the variable is a 'falsy' value, and any falsy value is replaced with the default value.
Example of usage scenarios:
If we want to display the article title on the page, but some articles may not have filled in the title. In this case, we hope to display a generic placeholder:
{{ article.Title|default:"无标题文章" }}
Ifarticle.Titleis an empty string.""/nil,or any values considered as “empty”, will display “No Title Article” on the page.”}]
For example, if the view count field is 0, we may want to display “No data available”:
{{ article.Views|default:"暂无数据" }}
At this point,article.ViewsYes0the page will display “No data available”.
default_if_none过滤器:精确的nilJudgment
Compared todefault的宽泛,default_if_none过滤器对“空”的定义则更为严格和精确。它只会在变量的值是nil(即真正的不存在或空引用)时才提供默认值.
This means that if a variable is empty, but it isEmpty string"", or a number0, boolean valuefalse, or it isan empty slice, map, or array,default_if_noneFilterwill nottreated as 'no value', it will output these empty but nonnilthe value.
Example of usage scenarios:
Assume you have a user contact phone fielduser.PhoneIn your business logic, the user may have selected "Do not provide a phone number" (corresponding to the value)nil), or explicitly filled in an "empty string"""(表示用户不想展示电话号码)。这两种情况可能需要不同的处理方式。
If you want 'Not filled' to be displayed only when the phone number is completely 'not provided', while an empty string""is directly displayed as empty when it is a valid input
{{ user.Phone|default_if_none:"未填写" }}
- If
user.PhoneYesnil,页面将显示“未填写”。 - If
user.Phoneis an empty string."",页面将显示一个空值(即什么都不显示)。 - If
user.PhoneYes"13800138000",页面将显示“13800138000”。
This is particularly important in scenarios where it is necessary to distinguish between 'No data' and 'Data is empty but exists'. For example, a product's stock level,0may mean 'Sold out', andnilIt may mean “No inventory information entered”,default_if_noneCan help us achieve this distinction.
Core difference and selection strategy
The core difference lies in the definition of “empty”:
default:As long as the variable is evaluated as a “falsy value”nil、empty string、0/falseEnglish set, etc.), and provide a default value.default_if_none:Only variablesIs clear thatnilProvide a default value only when0/falseOr empty set, etc. non-nilThe value of “empty” will be output as is.
In practical development, the choice of filter depends on your business understanding and display requirements for the “empty” state of data:
- Tending to use
default: - Tending to use
default_if_none:When your business logic requires a strict distinction between “No data provided (nil)” and “Data is provided but empty (""/0/falseWhen these two states occur. It provides finer control, allowing you to treat empty strings and other values as valid data.
Summary
defaultanddefault_if_noneThe filter is a powerful tool for handling null values in AnQiCMS template engine.Understanding the differences in "null" value judgment can help you write more robust and business logic-compliant template code.When you build a website, please choose the appropriate filters wisely according to specific business needs to provide better content display and user experience.
Common Questions (FAQ)
1. Why is itdefaultanddefault_if_noneDo the filters behave identically?This is because when the value of the variable is actuallynilAt any time, whether it isdefaultOrdefault_if_noneit will be recognized as 'empty' and apply the default value. Their difference lies only in the handling of empty strings, the number zero, or the booleanfalseetc.nilbut the case of 'falsy'.
2. If I have a numeric field, what will0these two filters do?
defaultthe filter will0considered as “empty” and usedefault_if_nonethe filter will not consider0Treated as “empty”, it will output numbers as is0unless the variable itself isnil.
3. Can I combine these filters, or use other methods to judge the variable’s “empty” state?Can. For example, you can useiflabels combined with variables themselves to determine if they exist or are not empty, or combined withlengthFilter to determine the length of a string or collection.In some complex scenarios, it is even possible to achieve more flexible control by passing data processed by custom functions or on the backend to the frontend template for rendering.