In AnQiCMS template development, we often need to handle the case where data may be empty to ensure the stability of page display and user experience. AnQiCMS provides a wealth of template filters to meet such needs, among whichdefaultanddefault_if_noneTwo very practical tools for handling null values. They are both aimed at providing a fallback value for missing or empty data, but there are subtle and critical differences in the definition of 'empty', understanding these differences can help us control the template rendering logic more accurately.

defaultFilter: Broad null value substitution

defaultThe filter is a general-purpose null value handling tool. It will use the default value you provide when the variable it acts on is evaluated as 'empty' in the template engine.This 'empty' is a relatively broad concept, including various situations:

  • Non-existent variable (nil): If a variable has not been assigned a value, or isnil.
  • Empty string (A string of zero length.
  • The number zero (0): Whether it is an integer, a floating-point number or other numeric type, as long as the value is zero, it will bedefaultconsidered empty.
  • BooleanfalseIf the boolean value of the variable isfalse.
  • Empty slice, map, or arrayWhen the collection type 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 Use Case:

Suppose 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""/nilOr other values considered as "empty", the page will display "No title article".

For example

{{ article.Views|default:"暂无数据" }}

At this time, ifarticle.ViewsIs0, the "No data available" will be displayed on the page.

default_if_noneFilter: Precisenilthe judgment.

Compared todefaultBroad,default_if_noneThe filter's definition of "empty" is more strict and precise. Itwill only provide a default valuenil(i.e., truly non-existent or null reference) when the variable's value is.

This means that if a variable is empty but it is aempty string""numbers0and a boolean valuefalseor it is aEmpty slice, map, or array,default_if_noneFilterwon'tit will be treated as 'no value', instead of outputting these empty but nonnilthe value.

Example of Use Case:

Assuming you have a user contact phone fielduser.PhoneIn your business logic, the user may have chosen to "not provide a phone number" (corresponding to the valuenil), or explicitly filled in an "empty string"""Phone number not displayed. Two different handling methods may be required.

If you want to display 'Not filled' only when the phone number is completely 'Not provided', and an empty string""Then it will display empty directly as a valid input

{{ user.Phone|default_if_none:"未填写" }}
  • Ifuser.PhoneIsnilThe page will display “Not filled in”.
  • Ifuser.Phoneis an empty string""The page will display a blank value (i.e., nothing will be displayed).
  • Ifuser.PhoneIs"13800138000"The page will display “13800138000”.

This is particularly important in scenarios where it is necessary to distinguish between “no data” and “data is empty (but exists)”.0It may mean “sold out”, andnilIt may mean that “stock information has not been entered”,default_if_noneIt can help us achieve this distinction.

The core distinction and selection strategy

The core distinction lies in the definition of “empty”:

  • default:As long as the variable is evaluated as a “falsy” value(nilis an empty string or0/falseand empty sets, etc.), provide a default value.
  • default_if_none:only variablesis explicitlynilonly provide a default value when, for empty strings,0/falseor empty sets, etc. nonnilThe 'empty' value will be output as is.

In practical development, which filter to choose depends on your business understanding and display requirements for the 'empty' state of the data:

  • Tend to usedefault:When you want all forms of 'empty' data to display a unified alternative content, without distinguishing whether it is completely missing or an empty string, zero, etc.This is usually used to quickly fill and beautify page content, avoiding blank or unfriendly zero values.
  • Tend to usedefault_if_none:When your business logic needs to strictly distinguish between “data not provided (“nil)” and “data provided but empty (""/0/falseThese two states. It provides finer control, allowing you to treat empty strings as valid data.

Summary

defaultanddefault_if_noneThe filter is a powerful tool for handling null values in the AnQiCMS template engine.Understanding the differences in "empty" value judgments can help you write more robust and business logic compliant template code.When you are building a website, please choose the appropriate filters wisely according to specific business needs to provide better content display and user experience.


Frequently Asked Questions (FAQ)

1. Why is it sometimesdefaultanddefault_if_noneDoes the filter behave exactly the same?This is because when the value of the variable is actuallynilwhen, whetherdefaultOrdefault_if_noneit will be recognized as 'empty' and apply the default value. Their difference lies only in handling empty strings, numeric zero, or booleanfalseetc. nonnilbut 'false value' situations.

2. If I have a numeric field, its value is0how will these two filters handle? defaultThe filter will also treat0considered as 'empty' and use the default value you provided. Butdefault_if_nonethe filter will not consider0Considered as 'empty', it will output the number as is0unless the variable itself isnil.

3. Can I combine these filters, or use other methods to determine the 'empty' status of the variable?Can. For example, you can useifTags combined with variables themselves to determine whether they exist or are not empty, or combinedlengthA filter to determine the length of a string or collection. In some complex scenarios, it can even be achieved by using a custom function or processing data on the backend before passing it to the frontend template for rendering, in order to achieve more flexible control.