How to set default display content for possibly empty variables using the `default` filter in AnQiCMS templates?

In the template design of AnQi CMS, we often encounter situations where variable values may be empty.For example, an article may not have a thumbnail set, or a product field may not be filled in temporarily.If the template directly displays these empty values, ugly blank spaces will appear on the page, affecting user experience, and even possibly confusing visitors.defaultThe filter is the key tool to ensure the coherence of content display.

defaultThe filter: the considerate guardian of empty variable values.

defaultThe filter allows us to set a predefined, friendly alternative content for template variables that may be empty.When the actual value of the variable is empty, the template does not display blank, but elegantly presents the default content we preset, thus maintaining the integrity and aesthetics of the page information.

Its working principle is:If a variable is considered to be “empty” (for example, its value isnilan empty string"", or a number0, or a boolean valuefalse)defaultThe filter will use the fallback value you specified. Otherwise, it will display the original value of the variable.

The usage method is very intuitive:

{{ 你的变量名|default:"默认显示内容" }}

For example, if you want to display the title of an article but worry that it may be empty, you can set it like this:

<h1>{{ archive.Title|default:"此文章暂无标题" }}</h1>

In this way, ifarchive.Titlethere is a value, it will be displayed normally; ifarchive.TitleEmpty (for example, in a database), the page will display “This article has no title”, avoiding blank or error messages, making the page look more professional and friendly.nilOr an empty string), the page will display “This article has no title”, avoiding blank or error messages, making the page look more professional and friendly.

Similarly, for missing descriptive text or image URLs,defaultthe filter can also be quite useful:

<p>{{ archive.Description|default:"暂无内容简介,敬请期待。" }}</p>
<img src="{{ archive.Thumb|default:"/static/images/default_thumb.webp" }}" alt="{{ archive.Title|default:"默认图片" }}">

Here, ifarchive.Descriptionis empty, a default introduction will be displayed; ifarchive.ThumbIt is also the thumbnail address, if it is empty, a preset default placeholder image will be displayed.

default_if_noneFilter: Distinguish the subtle difference between 'empty value' and 'non-existent'.

In some cases, we may need to judge more accurately whether a variable is truly absent(nil) rather than an empty string""or numeric0。Because in some business logic, an empty string and0can be meaningful valid values. At this time,default_if_noneThe filter becomes particularly important.

default_if_nonethe filter will only act on values that arenil(In Go language templates, it represents a null pointer or undefined) variables take effect. If the variable is an empty string""or a number0,default_if_noneIt will consider it a valid value and will not trigger the default content display.

its usage is similar todefaultsimilar:

{{ 你的变量名|default_if_none:"默认显示内容" }}

Let us understand the differences between them through a comparison:

Suppose there is a variableuserStatus:

  1. WhenuserStatusThe value ofnilAt that time:

    • {{ userStatus|default:"未设置状态" }}→ Display 'Not Set State'
    • {{ userStatus|default_if_none:"未设置状态" }}→ Display 'Not Set State'
  2. WhenuserStatusThe value is an empty string""At that time:

    • {{ userStatus|default:"未设置状态" }}→ Display 'Not Set State'
    • {{ userStatus|default_if_none:"未设置状态" }}→ Display an empty string""(because)""it is a valid value for it)
  3. WhenuserStatusThe value is a number0At that time:

    • {{ userStatus|default:"未设置状态" }}→ Display 'Not Set State'
    • {{ userStatus|default_if_none:"未设置状态" }}→ Display0(because)0it is a valid value for it)

Therefore, if you need to clearly distinguish between the variable "not assigned"nil) and the variable assigned an empty value (such as an empty string or zero),default_if_noneProvided finer control.

Why are these filters so important?

  • Enhance user experience:Eliminate blank areas or incomplete information on the page, providing users with a smoother and more professional browsing experience.Visitors will not see broken pages or missing data, but will see meaningful alternative content.
  • Enhanced template robustness:Effectively prevent page display errors or code exceptions caused by data missing. Even if the backend data occasionally has issues, the frontend page can maintain stable operation.
  • Simplify template logic:Avoided the need to check for null for each possible variable in the template