How to ensure that the AnQiCMS template does not cause a page rendering error when the variable is `nil`?

When building a website with AnQiCMS, we often encounter a situation where template variables may be empty (nilThe situation of ).For example, a document may not have a thumbnail set, or a custom field may not be filled in.If the template code does not properly handle these potential null values, errors may occur during page rendering, affecting user experience and even rendering some website features unusable.nilto ensure the stability and smoothness of the page.

AnQiCMS's template syntax is similar to the Django template engine, it uses double curly braces{{变量}}to output variable content, as well as single curly braces and the percent sign{% 标签 %}to control the logical flow. Understanding the basic syntax and how to use filters and tags in conjunction with them is essential to avoidnilThe key that causes rendering errors.

First, useiftags for conditional judgments

The most direct and effective method is to useifLabel variables for conditional judgments. In AnQiCMS templates,{% if 变量 %}the syntax will judge if the variable is "has a value". Usually, this means the variable is neithernilnor an empty string""not a number0or a boolean valuefalse.

For example, we want to display the description of the article, but some articles may not have filled in the description. If we output directly{{ archive.Description }}When the description is empty, the page may display a blank space, or may cause errors in some strict environments. We can handle it like this:

{% if archive.Description %}
    <p>{{ archive.Description }}</p>
{% else %}
    <p>暂无文章简介。</p>
{% endif %}

If we need to ensure a certain image field (such as)archive.Logoorarchive.ThumbRendering exists only if it<img>Label, thenifJudgment is very practical:

{% if archive.Logo %}
    <img src="{{ archive.Logo }}" alt="{{ archive.Title }}" />
{% else %}
    <!-- 方案一:显示一个默认占位图 -->
    <img src="/static/images/default-logo.png" alt="默认图片" />
    <!-- 方案二:或者干脆不显示图片 -->
{% endif %}

For boolean variables (such asitem.HasChildren)ifThe label can also well judge its truth or falsity, thus deciding whether to render child elements:

{% if item.HasChildren %}
    <ul>
        <!-- 渲染子分类列表 -->
    </ul>
{% endif %}

Pass{% if not 变量 %}We can also conveniently check whether a variable is, for example.nilor a null value, and provide alternative content, such as when handling 'previous/next' document navigation:

{% prevArchive prev %}
上一篇:
{% if not prev %}
  <span>没有了</span>
{% else %}
  <a href="{{ prev.Link }}">{{ prev.Title }}</a>
{% endif %}
{% endprevArchive %}

Secondly, utilizingdefaultanddefault_if_noneThe filter provides a default value

AnQiCMS template engine providesdefaultanddefault_if_noneFilter that provides a default value when the variable is empty. This is very convenient for handling null values inline.

defaultThe filter will be triggered when the variable isnilAn empty string"", or a number0or a boolean valuefalseprovides a default value. For example, ifarchive.Titleis empty, we can make it display “No Title Article”:

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

For imagessrcattributes,defaultThe filter can also be useful, ensuring that even if the image path is empty,srcthe property can also have a valid default value, to prevent the browser from displaying a broken image icon:

<img src="{{ archive.Thumb|default:"/static/images/placeholder.jpg" }}" alt="{{ archive.Title|default:"文章图片" }}" />

whiledefault_if_noneThe filter is more precise, it will only be provided when the variable is strictly ofnil(i.e., in Go language),nilthe default value. If the variable is an empty string"", or a number0or a boolean valuefalsebut notnil, thendefault_if_noneWill not interfere. This is very useful in scenarios where it is necessary to distinguish between 'not set' and 'set to zero/empty'.

For example, a numeric fieldarchive.Pricemay be set to0[Free symbol], may also benil[Price unknown]. If0is a valid value, thennilit needs a default, thendefault_if_noneis more suitable:

价格:{{ archive.Price|default_if_none:"价格待定" }} 元

Loop in theemptyHandling empty lists

When we need to traverse a list (array or slice), if the list is empty, use directly{% for %}The loop may cause no content to be displayed on the page, confusing the user. It is part of AnQiCMS.forThe loop tag provides{% empty %}a statement to solve this problem.

In{% for %}use{% empty %}When the traversed list is empty, {% empty %}and{% endfor %}the content between them will be rendered, providing a friendly prompt.

For example, when displaying a list of articles:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
        <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
    {% empty %}
        <li>目前还没有任何文章。</li>
    {% endfor %}
{% endarchiveList %}

In this way, even though archivesThe list is empty, and the page will not display a blank space, but will show a prompt saying 'There are no articles available at the moment.'.

IV. Combination Use: Build a More Robust Template

In actual development, we often need to combine the aforementioned strategies to build more robust and less error-prone templates.

For example, a content block may include a title, description, and image. We want to display the entire content block if either the title or description exists, and the image has a default value:

`twig {% if archive.Title or archive.Description %}

<div class="article-summary">
    <h2>{{ archive.Title|default:"最新动态"