In everyday website content management, we often encounter such situations: some variables may be empty due to uncompleted content, missing data, or other reasons (or in programming terms, asnil). If not handled, these variables may appear as blank areas on the front-end page, which not only affects the aesthetics of the page but may also confuse visitors and reduce the professionalism of the website.AnQiCMS (AnQiCMS) is well aware of this pain point, and therefore provides in the template enginedefaultanddefault_if_noneThese very practical filters help us elegantly handle cases where variables are empty, ensuring the integrity and user experience of the website content.

Why would a variable be 'blank', and what is the impact?

When building a website page, many content fields are optional.For example, the subtitle of an article, certain additional parameters of a product, the avatar URL in user comments, etc.When this optional field data is not entered in the background or cannot be obtained due to some logical judgment, if these variables are output directly in the template, the browser will display an empty string or nothing, resulting in ugly blank spaces on the page.This is like a book with missing paragraphs, which greatly reduces the reading experience.It is particularly important to set a reasonable default value to avoid this situation.

defaultFilter: General and flexible default value setting

defaultFilter is a very intuitive tool when you want a variable to be inAnything that is considered (Whether it is an empty string""numbers0and a boolean valuefalse, or represents a null valuenil), then it can have a backup value, sodefaultIt is your first choice. It will check if the variable is in these 'empty' states, and if so, it will output the default value you specified; otherwise, it will output the actual content of the variable.

For example, you may have an article author field, and if the author information is not filled in, you do not want the page to display a blank space, but instead show "Anonymous" or "No Author", which is where this comes in handy:

{{ article.author|default:"佚名" }}

Assumearticle.authorThe current string is empty, then the page will display “Anonymity”. Ifarticle.authorhas a value ofnilit will also display “Anonymity”. Even ifarticle.authoris a numeric variable, with a value of0,defaultIt will also treat it as empty and display the default value.

default_if_noneFilter: Precise processingnilValue

withdefaultThe difference is,default_if_noneThe filter is moreAccurate. It will only be provided when the value of the variable isnil(usually in programming means the variable has not been assigned or points to an empty reference)

This means, if the value of a variable is an empty string (""), a number0or a boolean valuefalse,default_if_noneThese will be consideredValidValues and will be displayed normally, without using the default values you have set. This is very useful in certain situations because empty strings, zero, orfalseIt may be a state that needs to be expressed in business logic, rather than the true 'absence'.

For example, you have a picture URL field, which will display the URL if the user uploads a picture; if the user deletes the picture but the field is updated to an empty string""(Intentionally left blank), you may want it to display as blank (or handled by CSS), rather than displaying a default placeholder. But if this image URL field is not assigned at all, it isnilThe status, then displaying "No image" as a default prompt will be more reasonable.

The followingdefault_if_noneUsage examples:

{{ product.imageUrl|default_if_none:"/static/images/no-image.png" }}

In this example, ifproduct.imageUrlIsnilIt will display/static/images/no-image.png. But ifproduct.imageUrlIs an empty string""It will output directly"", There may be blank spaces on the page, which exactly meets our expectation of 'intentionally leaving blank'.

How to choose the appropriate filter?

SelectdefaultOrdefault_if_none, It mainly depends on your definition of 'empty' and the needs of the business scenario:

  • Usedefault:When you want all forms of 'empty' values (including empty strings, 0, false ornilWhen all display default content. This applies to the general scenario where 'display content if available, placeholder if not'.

  • Usedefault_if_none:When you need to distinguish between 'intentionally empty' (such as an empty string""or numbers0and 'completely missing' (nilIn these two states. If you want an empty string or zero to be considered as a valid, displayable state, and only provide a default value when the variable is not assigned at all, thendefault_if_noneIs a more precise choice.

By reasonably using these two filters, your AnQiCMS website template will become more robust, avoiding visual confusion caused by missing data, and providing visitors with a more professional and smooth browsing experience.


Frequently Asked Questions (FAQ)

Q1:defaultanddefault_if_noneCan they be used simultaneously, or is there a more complex default value logic?

A1: Yes, they can be combined as needed, or coordinatedifLogical judgment to implement more complex default value logic. For example, you may want to first check if it isnil, then check if it is an empty string. In some cases, you can also combineifThis statement can control accurately. For example:

{% if article.content %}
    {{ article.content|safe }}
{% else %}
    <p>该文章暂无内容。</p>
{% endif %}

This method gives you greater control, and you can display different default content or structure according to different null value situations.

Q2: If I don't want to display any default text but instead completely hide the blank area (such as an emptydivlabel), what should I do?

A2: The best way to ensure that a section does not render at all when a variable is empty instead of displaying default text is to useifLogical judgment. The HTML structure is rendered only when the variable has a value. For example:

{% if product.description %}
    <div class="product-description">
        {{ product.description|safe }}
    </div>
{% endif %}

so ifproduct.descriptionis empty, the wholedivTags do not appear on the page, thus avoiding unnecessary blank areas.

Q3: Do these filters also work for numeric variables? If a numeric variable is0how will they handle it?

A3: Yes, they are also valid for numeric variables, but the handling will be different:

  • defaultFilter:Will treat numbers0As 'empty' values. If your numeric variable is0,defaultThe default value you set will be output.
  • default_if_noneFilter:Numbers are usually not considered.0asnil. Therefore, if your numeric variable is0,default_if_noneit will be output directly.0It will not use a default value. This emphasizesdefault_if_noneonly for variables that are completely missingnil)characteristics.

Understanding this is very important for handling default values in numeric fields such as counting, prices, for example, when the product price is0at this point, would you like to display "free" or display it directly?0.