How to set a default display value for a possibly empty variable in AnQiCMS templates?

When building a website with AnQiCMS, we often encounter such situations: certain content fields may not have values every time, for example, articles may not have thumbnails, products may not have detailed descriptions, or some contact information may not have been filled in.If variables that may be empty are directly called in the template, ugly blank spaces may appear on the page, or even program errors, which will greatly affect the user experience and the professionalism of the website.

It is fortunate that the AnQiCMS template system is based on syntax similar to Django and Blade, providing a powerful and flexible mechanism to handle these situations, allowing us to set elegant default display values for variables that may be empty.

1. UtilizedefaultFilter settings basic default value

The most direct and most commonly used method is to use the built-in AnQiCMS templatedefaultFilter. Its function is very simple: it automatically displays the default value you set when the variable does not exist or its value is empty. Here, "empty" usually includes empty strings, boolean valuesfalse, or a number0as well as in Go language.nil.

The usage method is very intuitive, just use the pipe symbol after the variable name.|ConnectdefaultFilter, and specify your default text with double quotes.

For example, if your article summaryarchive.DescriptionMay be empty, and you wish to display "No introduction" in this case, you can write it like this:

<p>{{ archive.Description|default:"暂无简介" }}</p>

Similarly, for some contact methods that may not exist, such as WhatsApp, if the background is not filled in, we can give it a default prompt:

<p>WhatsApp: {{ contact.WhatsApp|default:"未设置" }}</p>

In this way, even though archive.Descriptionorcontact.WhatsAppIn the database, it is an empty value, and it will not appear blank on the page, but instead, it will display "No introduction" or "Not set" in a friendly manner, ensuring the integrity of the content.

2. Precisely handle empty values:default_if_noneFilter

In certain specific scenarios, you may need to make a more accurate judgment, only for the variable value ofnilThe situation where a variable does not exist or is not assigned (not just an empty string) is set to a default value, while an empty string is retained as is. AnQiCMS providesdefault_if_noneFilter.

This filter is similar todefaultThe main difference lies in that it is only fornilvalue takes effect. If the value of the variable is an empty string ("")default_if_noneit will consider it a valid value and display it, whiledefaultit will be considered as 'empty' and the default value will be displayed.

The method of use isdefaultthe same as the filter:

<p>文章作者: {{ archive.Author|default_if_none:"匿名读者" }}</p>

Assumearchive.AuthorFields:

  • If it isnil: displays "Anonymous Reader".
  • If it is""(Empty String):Displays an empty string (i.e., no content is displayed).
  • If it is"张三":Displays "Zhang San".

Understand the subtle differences between these two filters can help you control the rendering logic of the template more accurately.

3. Using conditional judgment{% if %}Rendering different structures with tags

In addition to directly setting default text, sometimes you may need to display a completely different content structure based on whether a variable exists, or even not display certain blocks. AnQiCMS templates support syntax similar to Django.{% if 条件 %}Labels, which allows us to flexibly perform conditional rendering.

When a variable has a value, display a segment of HTML; when the variable is empty, display a different HTML structure, or a placeholder image.This is especially useful when dealing with images, ad slots, and optional content modules.

For example, set a default thumbnail for the article: First, we get the logo image of the article usingarchiveDetailthe label{% if %}for judgment.

{% archiveDetail articleLogo with name="Logo" %}
{% if articleLogo %}
    <img src="{{ articleLogo }}" alt="文章图片">
{% else %}
    <img src="/public/static/images/default-thumbnail.jpg" alt="默认缩略图">
{% endif %}

The logic of this code is:“articleLogoThe variable has a value (i.e., the article has an image), then display this image; otherwise, display a preset default thumbnail.This can avoid page layout errors or broken placeholders due to missing images.

For example, some contact information only displays the corresponding link when there is a value:

{% contact email with name="Email" %}
{% if email %}
    <p>联系邮箱:<a href="mailto:{{ email }}">{{ email }}</a></p>
{% endif %}

IfemailIf the variable is empty, the wholeptags will not be rendered, keeping the page simple.

4. Advanced Techniques: Predefined Variables and Flexible Reuse{% set %}or{% with %})

In some scenarios, you may wish to set an initial default value for a variable at the top of a template file or within some logical block, and then refer to this variable in multiple places. AnQiCMS provides{% set %}and{% with %}Label to achieve this.

  • {% set %}Define a local variable: {% set %}Labels are used to define a new variable in the current template file, which can be used anywhere after its definition.

    {% set articleTitle = archive.Title|default:"无标题文章" %}
    <h1>{{ articleTitle }}</h1>
    <p>页面标题:{{ articleTitle }}</p>
    

    Here,articleTitleThe variable has been assignedarchive.Titlevalue, ifarchive.TitleThe title is empty, then use 'No Title Article' as the default value. After that, we can reuse it multiple timesarticleTitle.

  • {% with %}Define scope variables: {% with %}Labels are used to define one or more variables within a specified block, these variables are only valid in{% with %}and{% endwith %}. It can also pass default values in{% include %}tags.

    {% with defaultDesc="此内容暂无详细描述。请等待后续更新。" %}
        <p>{{ archive.Description|default:defaultDesc }}</p>
    {% endwith %}
    

    In this example,defaultDescOnly{% with %}block, used forarchive.DescriptionSet default value.

Summary

Setting a default display value for possibly empty variables in the AnQiCMS template is a key part of building a robust and user-friendly website.

  • For simple text content missing, use directlydefaultFilteris the most convenient way.
  • Need to differentiate strictlynilAnd empty string when,default_if_noneFilterProvided finer control.
  • When you need to display different HTML structures based on whether a variable exists,{% if %}Conditional judgmentis indispensable.
  • while{% set %}or{% with %}tagsIt can help you manage and reuse default variables more effectively, making template code clearer.

Master these skills, and you will be able to create more stable and elegant AnQiCMS website templates