During the process of building a website with AnqiCMS, we often encounter such situations: a variable needs to be displayed in the template, but it may be empty for various reasons, such as not setting a thumbnail, a list without content, or optional information not filled in.If these empty variables are not processed, the page may display ugly blank areas, error messages, or misaligned layout, which undoubtedly affects the user experience.
AnqiCMS's powerful template engine provides various mechanisms to elegantly handle these potentially empty variables, ensuring that the website maintains good display effects and user experience even in cases of incomplete data.We will discuss several practical strategies in detail.
Handle empty arrays or lists:{% for ... empty %}The Art of Elegance
When you need to loop through a list in a template (such as an article list, image gallery, etc.), but are not sure if the list will always have content, in the AnqiCMS template engine,{% for ... empty %}Structure is your powerful assistant. It allows you toforhandle data normally within a loop, and at the same timeemptydefine the content to be displayed when the list is empty.
For example, on a category page, it is necessary to display a list of articles, but there may be no articles under this category:
{# 假设 archives 是一个可能为空的文章列表 #}
<div>
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<div class="article-item">
<a href="{{item.Link}}">
<h5>{{item.Title}}</h5>
<div>{{item.Description}}</div>
</a>
</div>
{% empty %}
<div class="no-content-message">
抱歉,当前分类下暂无文章。
</div>
{% endfor %}
{% endarchiveList %}
</div>
In this example, ifarchivesThe list has content, the system will normally render the title and description of each article. But ifarchivesIt is empty,{% empty %}The text
Handle empty strings or single values:defaultThe wonder of filters
For a single variable (such as a string, number, or object property), if it may be empty or undefined, usedefaultA filter is a concise and efficient method. This filter provides a default value for display when the variable value is empty or undefined.
For example, you want to display the filing number of the website, but the background may not be filled in, or the thumbnail of an article may not exist:
{# 显示网站备案号,如果为空则显示“暂无备案信息” #}
<p><a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">{% system with name="SiteIcp" %}{% else %}{{'暂无备案信息'}}</a> ©2021 AnQiCMS. All Rights Reserved</p>
The better way to do it is to use a filter:{{ variable|default:"默认值" }}
For example, when you need to display a thumbnail of an article, but some articles may not have uploaded a thumbnail:
<img src="{{ item.Thumb|default:'/static/images/default-thumb.jpg' }}" alt="{{ item.Title|default:'无标题文章' }}" />
Here, ifitem.Thumbis empty, the image will display as/static/images/default-thumb.jpgthis default image; ifitem.TitleIt is empty,altThe attribute will display “No Title Article”.
It is worth mentioning that the AnqiCMS template engine also providesdefault_if_nonefilter.defaultThe filter will treat empty strings, the number 0, and boolean valuesfalseas well asnil(Null pointer) are treated as empty. Anddefault_if_nonethen only fornilThe value is effective. In the Go language template environment, this can more accurately handle the case where the data is a null pointer rather than an empty string. Generally speaking,defaultThe filter is sufficient to handle most scenarios.
Masterfully control the display logic:{% if ... %}Conditional judgment
Besides the aforementioned methods of setting default values,{% if ... %}Conditional tags provide finer control.You can check if a variable exists, whether it is non-empty, even perform more complex logical judgments, thus deciding whether to display an element or display some alternative content.
For example, a page may have a canonical link (Canonical URL), but not all pages need to be set, or only displayed when set<link>Tags:
{%- tdk canonical with name="CanonicalUrl" %}
{%- if canonical %}
<link rel="canonical" href="{{canonical}}" />
{%- endif %}
Here, {%- if canonical %}Will checkcanonicalDoes the variable have a value. Ifcanonicalis empty, the whole<link>The tag will not be rendered on the page. This method is very useful in handling some SEO tags that may not exist, optional social sharing buttons, and other scenarios.
For example, you want to display the tags of an article, but some articles may not have tags:
{% tagList tags with itemId=item.Id limit="10" %}
{% if tags %} {# 检查 tags 列表是否非空 #}
<div class="article-tags">
标签:
{% for tag in tags %}
<a href="{{tag.Link}}">{{tag.Title}}</a>
{% endfor %}
</div>
{% endif %}
{% endtagList %}
Comprehensive application, create a robust template
In actual template development, these methods can be flexibly combined according to specific requirements. You can first use{% if %}Determine whether a large block contains content, if not, the entire block does not display; within the block with content, for individual variables that may be empty, usedefaultFilter default settings; for lists that may be empty, use{% for ... empty %}the structure.
By these strategies, your AnqiCMS website template will become more robust, whether the data is complete or incomplete, it can present to the user in an elegant and friendly manner, greatly enhancing the usability and professionalism of the website.
Frequently Asked Questions (FAQ)
1.defaultFilters anddefault_if_noneWhat are the differences between filters in AnqiCMS templates?
defaultFilters check if a variable is an empty value (including empty strings, number 0, boolean value)falseas well asnil/nullIf it is empty, use the default value you provided.default_if_noneIt is more strict, it will only occur when the variable is.nilWhen a null pointer is used, the default value is used. For empty strings, numbers 0, and other cases where there is a 'value' but it is 'empty', it will not trigger the default value.In the AnqiCMS Go language backend environment, understanding this helps to handle data types more accurately.
2. Why does my{% for ... empty %}The code has not taken effect, and the list is still blank when it is empty.This usually has several reasons. First, make sure that yourforloop variable is indeed an array, slice, or an iterable object. If it is a single value,emptyThe block will not be triggered. Next, check the one you pass toforDoes the loop variable really contain nothing? Sometimes, a variable may contain an empty object instead of an empty array, or it may be nothing itself.nilIn this case,forthe loop may skip it directly without entering.emptyblock. In these cases, you may need to{% for %}use the tag outside first{% if %}to make a more comprehensive judgment of the variable.
3. How to set the default display value for custom model fields in the background?AnqiCMS allows you to set default values for fields when customizing content models (as mentioned in the "Content Model Help" document).If the backend has already set a default value for a custom field, you can directly call the field in the template, and the system will automatically use the default value set by the backend.defaultFilter, for example{{ item.custom_field|default:'这里是自定义字段的默认值' }}.