In Anqi CMS template design, properly handling variables that may be empty is the key to ensuring the completeness and smoothness of website content display and user experience.When a template variable has no value or is in an 'empty' state, we usually do not want to display blank or error messages on the page, but rather show a preset default content.At this time, the AnQi CMS provideddefaultanddefault_if_noneTwo filters come into play. They can both provide default values for variables, but there are subtle and important differences in the criteria for determining 'empty'.

defaultFilter: Flexible 'null' handling

defaultThe filter is a relatively lenient default value setting tool. Its design philosophy is: whenever a variable looks "empty", the default value is applied. Specifically,defaultIt will judge the following situations as "empty":

  • The variable does not exist or isnil(No value):If the variable does not exist in the template context or is explicitly set tonil.
  • empty string"":The value is an empty string with no characters.
  • Number0:The value is the number zero.
  • Booleanfalse:The value is a boolean false.
  • Empty array, empty slice, or empty map:If a variable is a collection type but contains no elements.

Therefore, when you want any form of 'content missing' to be replaced by a default value,defaultThe filter is your first choice. It can conveniently provide a fallback solution to avoid unnecessary blank spaces or make the information on the page seem incomplete.

Example of applicable scenarios:

  • Article Title or Summary:If the editor forgets to fill in the article title or summary, 'No Title' or 'No Summary' can be displayed.
    
    {{ article.Title|default:"无标题" }}
    {{ article.Description|default:"暂无简介" }}
    
  • Product Price:If the product has not set the price, display "Price to be discussed".
    
    {{ product.Price|default:"价格待议" }}
    
  • User nickname:If the user has not set a nickname, display "Anonymous user".
    
    {{ user.Nickname|default:"匿名用户" }}
    

default_if_noneFilter: Accurate "No value" judgment.

withdefaultThe inclusiveness of the filter is different,default_if_noneThe filter is more 'strict' when determining whether a variable needs to apply a default value. It only focuses on one case: whether the variable isnil(or is in the Go language of thenil, indicating that the variable has not been assigned or pointed to anything).

This means, for an empty string""numbers0or a boolean valuefalse,default_if_noneThe filter will consider them as valid values and will output them as they are without applying default values. It will only intervene and provide default content when the variable is explicitlynilThen it will intervene and provide default content.

Example of applicable scenarios:

  • Optional image link:Assuming a product image variableproduct.ImageUrlIf the database has not set this image, the value may benil. But if it is set, even if it is an empty string""It may also represent the intentional non-display of images.
    
    {{ product.ImageUrl|default_if_none:"/static/images/placeholder.webp" }}
    
    In this example, ifproduct.ImageUrlIsnilIt will display a placeholder; if it is an empty string.""This will display an empty string (the browser will consider the image address to be empty and not load the image), which may be the behavior we want.
  • Inventory quantity:If the inventory quantity of a product isproduct.StockIs0This usually indicates a meaningful value (out of stock), not 'no inventory information'. Only when the inventory information is completely missing (nilOnly when this condition is met will the default prompts such as 'Inventory sufficient' be displayed.
    
    {{ product.Stock|default_if_none:"库存充足" }}
    
    Ifproduct.StockIs0The page will display,0If it isnilThen it will display 'Inventory sufficient'.

Core differences and selection criteria

In simple terms, the core difference between these two filters lies in their definition of "empty":

  • defaultThe filter has a broader definition of "empty", coveringnil, an empty string, 0,falseand various other situations.
  • default_if_noneThe filter defines 'empty' more strictly, only fornilthe case of no value.

In the actual template development, we recommend you choose the appropriate filter based on the specific meaning of the variable and the behavior you wish to display when content is missing:

  • Selectdefault:When you encounter most common text content (such as article titles, descriptions, common fields), you tend to usedefaultIt can provide the most common default value protection, ensuring that the page can display friendly when the information is incomplete.
  • Selectdefault_if_none:When0/falseor an empty string""It is meaningful business data, you hope to be displayed accurately when they exist, rather than being overwritten by default values, you should choosedefault_if_noneFor example, the product inventory is0Status isfalseOr the image link is intentionally left blank"".

Understanding the difference between these two will allow you to control the data display logic of the Anqi CMS template more accurately, enhancing the robustness and user experience of the website. In the development process, it is advisable to use it firstdefaultTry it, if you find0or""Incorrectly replaced, consider switching todefault_if_if_nonefor more precise control.


Frequently Asked Questions (FAQ)

Q1: Why is my article abstract field empty,{{ archive.Description|default_if_none:"暂无简介" }}Does not display the default value?

A1: This is becausearchive.DescriptionThe value is very likely to be an empty string""instead ofnil.default_if_noneThe filter treats empty strings as valid values and does not apply the default value. If you also want to display the default value in this case, you should usedefaultFilter:{{ archive.Description|default:"暂无简介" }}.

Q2: Can I use these two filters to set default values for a complex object (such as a struct)?

A2: These two filters are mainly used to handle the default values of a single variable or variable attribute. If you try to use it on a complete complex object (such as{{ article|default:"没有文章信息" }}), it is usually only when thisarticleAn object isnileffective onlyYou should handle the properties inside the objectUse these filters to ensure that each field that may be empty has a default display. For example,{{ article.Title|default:"无标题" }}and{{ article.Author|default:"佚名" }}.

Q3: Besides these two filters, are there other methods to handle empty variables in templates?

A3: Of course. The most basic and direct way is to useifLogic judgment label:

{% if article.Description %}
    {{ article.Description }}
{% else %}
    暂无简介
{% endif %}

In addition, for iterating over a list, you can usefor emptystructure, to display specific content when the list is empty:

{% for item in newsList %}
    <!-- 显示新闻内容 -->
{% empty %}
    目前没有最新新闻
{% endfor %}

These methods provide more flexible control, and the most suitable processing method can be selected according to different business logic.