AnQiCMS (AnQiCMS) as our daily content operation assistant, its flexible template engine syntax makes content display diverse and efficient.However, in practice, we often encounter situations where variable values are empty, such as when the article title is not filled in, or when a custom field has no data.How can we elegantly handle these 'null' values to ensure that the website front-end presentation is always professional and friendly, which has become a topic that content operators must face.filter-defaultandfilter-default_if_noneWhat are the similarities and differences in handling variable null values, and how can they be skillfully used in different scenarios.

AnQiCMS's template system follows a syntax style similar to Django's template engine, a major feature of which is to process and handle variables through 'filters'.When data is passed from the backend to the frontend template, we may need to provide a fallback display content for those missing or 'seemingly' empty values.defaultanddefault_if_noneThese two filters come into play.

I. Broad 'null substitute':filter-default

filter-default

Imagine that you are a website editor, hoping to ensure that the publication date of each article is displayed in a friendly reminder even if it is not accurately recorded in special cases. If a document'sCreatedTimeThe field is null due to some reason or has been set to zero value (in many systems, the zero value of a timestamp may represent1970-01-01Such a date, but we may wish it to display "TBD", at this timefilter-defaultit can be put to use.

Its range of judgment includes but is not limited to:

  • in Go language,nil(indicating uninitialized or no value).
  • Empty string ("")
  • Number zero (0or0.0)
  • Booleanfalse.
  • Empty slice (slice) or map (map).

Template code example:

{# 假设 archive.Title 可能为空字符串或nil #}
<div>文章标题:{{ archive.Title|default:"无标题文章" }}</div>

{# 假设 article.Views 浏览量可能为0 #}
<div>浏览量:{{ article.Views|default:"暂无" }} 次</div>

{# 假设 user.Nickname 可能为nil #}
<div>用户昵称:{{ user.Nickname|default:"匿名用户" }}</div>

In the above example, whetherarchive.TitleIsnilor an empty string""will display "No Title Article";article.ViewsIf it is0then it will display "No Content";user.NicknameIf it isnilthen display "Anonymous User".filter-defaultLike a generous butler, as long as he sees a "gap", whether it is completely empty or just a shell, he will eagerly fill it.

Second, a strict "valueless substitute":filter-default_if_none

Compared tofilter-defaultof the "generous",filter-default_if_noneappears more rigorous and picky. It will only be used for thosestrictly equal tonilThe variable provides a default value. In other words, the filter will only intervene when the variable explicitly indicates a 'non-existent' or 'uninitialized' state.

Under the background of AnQiCMS based on the Go language,nilIt typically refers to the zero value of pointer, interface, slice, map, channel, or function types. It represents the state of 'not pointing to any value' rather than a specific but empty value (such as an empty string""or numbers0)

Template code example:

{# 假设 archive.Title 可能为nil或空字符串 #}
<div>文章标题:{{ archive.Title|default_if_none:"无标题文章" }}</div>

{# 假设 article.Views 浏览量可能为0 #}
<div>浏览量:{{ article.Views|default_if_none:"暂无" }} 次</div>

{# 假设 user.Nickname 可能为nil #}
<div>用户昵称:{{ user.Nickname|default_if_none:"匿名用户" }}</div>

Let's analyze the performance in different situations in detail:

  • Ifarchive.TitleIsnilIt will display 'No Title Article'.
  • Ifarchive.TitleIs an empty string"",default_if_noneWill think""Is a specific value (even though it is empty), so it willLeave empty string unchangedInstead of default value.
  • Ifarticle.ViewsIsnilAlthough numeric types usually are notnilBut in some special cases), it will display "No data."
  • Ifarticle.ViewsIs a number0,default_if_noneWill think0It is a specific value, so it willOutput as is0.
  • Ifuser.NicknameIsnilthen display "Anonymous User".

filter-default_if_noneIt is like a strict accountant, who only writes 'none' clearly in the ledger (i.e.nilIt will intervene only when necessary, and for those with specific records but empty content (such as empty strings, number 0), it will respect their original values.

Chapter 3: Application Scenarios and Selection Suggestions

Understanding the difference between the two, we can make more wise choices in content operation and template design:

  • Selectfilter-default:When you need to provide a default fallback for any 'look empty' value, it is a very practical choice.For example, if the image URL uploaded by the user is missing or empty, we would like to display a generic placeholder; or for any non-critical text information, regardless of its "empty" status, we would like to have a unified default prompt.This helps ensure the completeness and consistency of the page content, avoiding unnecessary blank or error display.

  • Selectfilter-default_if_none:It is particularly important when you have a more precise semantic requirement for the 'empty' state of a variable. For example, a user's score field,0May represent 'zero points' rather than 'not set points'; the stock quantity of a product,0means "sold out" rather than "inventory information missing". In this case, you only want when the variable is indeed not assigned (i.e.nilProvide a default value only when0or""It retains its original meaning. This helps you express business logic more accurately, avoiding misinterpretation.

In short,filter-defaultWith its inclusiveness, it provides comprehensive default support for null values on the website; andfilter-default_if_noneThen with its accuracy, we can finely control which 'blanks' need to be replaced, and which should retain their unique meanings.In the AnQiCMS template world, making good use of these two 'tools' will make our content display more flexible and robust.


Frequently Asked Questions (FAQ)

Q1:filter-defaultandfilter-default_if_noneWhat is different in handling empty strings?A1: When the variable is an empty string (""),filter-defaultIt will treat it as 'empty' and display the default value you have set. However,filter-default_if_noneIt will treat an empty string as""Consider an existing but empty valid value, therefore it will output an empty string unchanged, and will not apply the default value.

Q2: If the value of a variable is0(Number zero), how will these two filters perform?A2: For number zero(0)filter-defaultIt will treat it as "empty" and display the default value you have set. Whilefilter-default_if_noneit will consider0Is a specific numeric value, even if it is zero, the default value will not be applied, but it will be output as is0.

Q3: In the AnQiCMS template,nilWhat does it usually mean?A3: In the AnQiCMS environment based on Go language,nilA variable does not point to any memory address, meaning it is uninitialized or does not exist.Under template context, it usually appears when the backend data query fails to find the corresponding value or when an optional field is not set.default_if_noneIt is to handle this kind of 'no value' state in the strict sense.