How to judge if a variable is empty in a template and display default content in AnQiCMS?

In website content operation, the integrity of data and the elegance of display are crucial.We often encounter such situations: some fields of data may be missing for various reasons, such as an article may not have an image, a product may not have a detailed description, or a custom field may not be filled in.If the template code does not perform the corresponding null value judgment, it may cause the page to appear blank, affecting aesthetics, or even trigger template rendering errors, affecting user experience.

AnQiCMS thanks to its efficient architecture based on the Go language and its flexible design similar to the Django template engine, provides us with a variety of powerful and intuitive ways to determine whether a variable in the template is empty, and display default content accordingly, allowing the website to maintain a good user experience even when the data is incomplete.Let's explore these practical tips together.

The most intuitive and powerful: {% if ... %}Logical Judgment Tag

In AnQiCMS templates,ifLabels are the core tools we use for conditional judgment. They can determine whether a variable is a 'true value' or a 'false value'. When a variable is considered a 'false value' (for example, an empty string"", or a number0, boolean valuefalse, and in Go language,nilequivalent to other languages,nullorNone)ifstatement will execute itselsethe content in the block.

Basic usage:

Assume you have an article objectarchivewhich may includeTitle(Title) andDescription(description) fields, etc. You can determine if it is empty:Titlewhether it is empty:

{% if archive.Title %}
    <h1>{{ archive.Title }}</h1>
{% else %}
    <h1>暂无标题</h1>
{% endif %}

Ifarchive.TitleNot empty, the actual title will be displayed on the page; otherwise, it will display "No title". Similarly, you can also useif notto judge whether a variable is empty:

{% if not archive.Description %}
    <p>该文章暂无详细描述。</p>
{% else %}
    <p>{{ archive.Description }}</p>
{% endif %}

This method is suitable for scenarios that require displaying the entire HTML block or executing more complex logic.

Shortcut:defaultFilter

When you just need to provide a default value in the output without complex logical judgment structures,defaultThe filter is your **choice. It is concise and efficient, making the code more readable.

defaultThe filter will be applied when a variable is evaluated as a "falsy" value (including an empty string"", or a number0, boolean valuefalseandnil),display the default content you provided.

Example Usage:

<p>作者:{{ archive.Author|default:"佚名" }}</p>
<img src="{{ archive.Logo|default:'/static/images/default_logo.png' }}" alt="{{ archive.Title|default:'文章图片' }}" />
<p>价格:{{ product.Price|default:0 }} 元</p>

In the above example:

  • Ifarchive.Authoris empty, will display 'Unknown'.
  • Ifarchive.Logois empty (or invalid), will display/static/images/default_logo.pngas the default image.
  • Ifproduct.Priceis empty or0will display0English.

defaultFilter is very suitable for inline output, and can effectively avoid blank spaces or broken links on the page.

For the list:{% for ... %}Combine{% empty %}

When processing list data, especially when you need to judge whether the list is empty and display a prompt message,forLoopingemptyBlocks are very practical. They provide a graceful way to handle the case where a list is empty, avoiding redundancy.ifjudgment.

Example Usage:

If you want to display a list of related documents, but sometimes there may be no relevant content:

{% archiveList relatedArchives with type="related" limit="5" %}
    {% for item in relatedArchives %}
        <div class="related-item">
            <a href="{{ item.Link }}">{{ item.Title }}</a>
        </div>
    {% empty %}
        <div class="no-related-content">暂无相关文档。</div>
    {% endfor %}
{% endarchiveList %}

IfrelatedArchivesThe list is empty or does not exist,emptyThe content within the block will be rendered, prompting the user that there is no related content, rather than leaving an empty area.

More accurate judgment:default_if_noneFilter

In certain specific scenarios, you may need to distinguish between a variable that does not exist at all (nil) or is simply empty (such as an empty string"", or a number0). In this casedefault_if_noneThe filter comes into play.

default_if_nonethe filter will only be applied when the value of the variable isnilIt shows the default content at time. This means it does not affect empty strings""or numeric0take effect.

Example Usage:

Suppose there is a custom fieldproduct.SpecificationIt may benil(not set), it may also be""(set but content is empty).

<p>产品规格(default):{{ product.Specification|default:"未填写" }}</p>
<p>产品规格(default_if_none):{{ product.Specification|default_if_none:"未设置" }}</p>
  • Ifproduct.SpecificationYesnil:
    • The first line will display “Not filled in”.
    • The second line will display “Not set”.
  • Ifproduct.SpecificationYes""(Empty string):
    • The first line will display “Not filled in”.
    • The second line will display an empty string (i.e., no content will be displayed).

This filter is very useful in scenarios where it is necessary to strictly distinguish between 'unassigned' and 'assigned but empty'.

Practice combined with actual scenarios

  • Images and links:For imagessrcAttributes and linkshrefProperty, usedefaultThe filter provides an alternative image or link to prevent the appearance of a 'broken link' icon or inaccessible links.
  • Rich text content:For article details, product descriptions, and other rich text content, it is usually better to hide the area directly or display a prompt such as 'No content available' when the content is empty.{% if ... %}CombinesafeFilter (for safely rendering HTML content) would be a better choice: English
    
    {% if archive.Content %}
        <div class="article-content">{{ archive.Content|safe }}</div>
    {% else %}
        <div class="article-content">暂无详细内容。</div>
    {% endif %}
    
  • Custom field:AnQiCMS A powerful content model allows us to create various custom fields. When calling these fields in the template, it is also necessary to make empty value judgments.
    
    {% archiveDetail author with name="author" %}
    {% if author %}
        <p>作者:{{ author }}</p>
    {% else %}
        <p>作者信息待补充。</p>
    {% endif %}
    

By flexibly using these methods to determine if variables are empty, you can write more robust and user-friendly programs.