In website content management, we often encounter a situation where a certain data variable may have no value on some pages or under certain conditions, that is, "empty".If the template directly outputs these blank variables, there will be ugly blank spaces on the page, which not only affects the aesthetics but may also confuse the visitors.defaultA filter that helps us set a graceful default display value for these potentially empty variables.
Why do we need to set a default display value for variables?
Imagine that your website displays a series of product information.Some products may not have uploaded thumbnails, some article authors may have chosen to publish anonymously, and some contact information may be temporarily missing.nil/falseThis not only makes the website look unprofessional, but may also miss the opportunity to provide useful information to users.
defaultThe function of the filter is to solve this kind of problem.It allows us to automatically replace a variable with a preset default value when it is empty, ensuring that your website always maintains complete content and a smooth user experience.
UnderstanddefaultFilter: Say goodbye to blank content.
defaultThe filter is a very intuitive and powerful feature in the AnQi CMS template engine. Its basic function is to output the default value we specify when a variable has no value.
Basic usage:
UsedefaultThe filter is very simple, just add the pipe symbol after the variable|anddefault:"默认显示内容".
For example, if you have a variableuserNameIt may be empty, you can use it like this:
{{ userName|default:"匿名用户" }}
IfuserNameIf the variable has a value (such as "Zhang SanuserNameit is empty, the page will display "Anonymous user".
defaultHow does the filter judge "empty"?
UnderstandingdefaultThe filter how to identify "empty" is very important. In the template environment of Anqi CMS,defaultThe filter will judge the following situations as "empty":
- in Go language,
nil(equivalent to other programming languages)null): This is the most typical 'null' value. - empty string (
""): Even if the variable exists, but its value is an empty string,defaultit will also be considered as empty. - Boolean value
falseIf the variable isfalse,defaultit will also consider it as empty. - Number
0If the variable is a number0,defaultit will also consider it as empty.
Example:
{{ simple.nothing|default:"暂无数据" }} {# 如果simple.nothing是nil,显示 "暂无数据" #}
{{ ""|default:"空字符串替代" }} {# 如果变量是空字符串,显示 "空字符串替代" #}
{{ false|default:"不确定" }} {# 如果变量是false,显示 "不确定" #}
{{ 0|default:"数量未知" }} {# 如果变量是0,显示 "数量未知" #}
{{ 42|default:"默认值" }} {# 42不是空,显示 42 #}
{{ "安企CMS"|default:"内容管理系统" }} {# "安企CMS"不是空,显示 "安企CMS" #}
default_if_noneFilter: fornilexact judgment of
Exceptdefault, and the Anqi CMS also providesdefault_if_noneFilter. This filter is more focused on determining whether a variable isnil(which is the true "null"), and will not consider empty strings, boolean valuesfalseor numeric0as empty.
Basic usage:
{{ variable|default_if_none:"无" }}
default_if_noneJudgment criteria:
- Only when the variable is
nilwhendefault_if_noneWill apply the default value. - If the variable is an empty string (
""),falseor0,default_if_noneIt will retain its original value and will not use the default value.
Example:
{{ simple.nothing|default_if_none:"暂无数据" }} {# 如果simple.nothing是nil,显示 "暂无数据" #}
{{ ""|default_if_none:"空字符串替代" }} {# 如果变量是空字符串,显示 "" (空字符串本身) #}
{{ false|default_if_none:"不确定" }} {# 如果变量是false,显示 "false" #}
{{ 0|default_if_none:"数量未知" }} {# 如果变量是0,显示 "0" #}
When to choosedefaultand when to choosedefault_if_none?
- If you wish to set a variableEntirely missing (
nil)、empty string,falseor0default value is displayed, then please usedefault. This is usually applicable to most scenarios and can more comprehensively cover various "no content" situations. - If you only want to display the variable确实是
nil(即不存在或未赋值)时才显示默认值,而空字符串、falseor0本身就是有效信息,那么default_if_none会是更精确的选择。
defaultThe practical application of filters
In the actual operation of website content,defaultFilters can play a role in many places, making your website information display more flexible.
Article or product title:When the article or product title is not filled in, a general prompt can be displayed:
<h1>{{ archive.Title|default:"【未命名】产品/文章" }}</h1>Thumbnail/Logo:If the thumbnail of the article or product is not uploaded, a default image can be displayed:
<img src="{{ archive.Thumb|default:"/static/images/default_thumb.jpg" }}" alt="{{ archive.Title|default:"默认图片" }}" />Contact information:When the contact phone number, email, etc. is not set, "No information" or an alternative message can be displayed:
<p>电话:{{ contact.Cellphone|default:"暂无联系方式" }}</p> <p>邮箱:{{ contact.Email|default:"请联系网站管理员获取" }}</p>Article description/summary:If the article does not have a description filled in, it can be extracted from the main text or display a default prompt:
<p>{{ archive.Description|default:"这篇内容暂时没有简介,请点击查看详情。" }}</p>Custom field:For fields customized by the content model, such as 'author', 'source', etc., default values can also be easily set:
<p>作者:{% archiveDetail author with name="author" %}{{ author|default:"佚名" }}</p> {# 或者如果是在遍历params时 #} {% archiveParams params %} {% for item in params %} {% if item.Name == "来源" %} <p>来源:{{ item.Value|default:"原创" }}</p> {% endif %} {% endfor %} {% endarchiveParams %}
Through these simple applications, you will find that the display of the web page becomes more robust and user-friendly, even if the data is incomplete, it can be presented in a consistent and beautiful manner.
Summary
defaultThe filter is an indispensable tool in the design of Anqi CMS templates.It can help us effectively handle various possible null value situations, ensuring the completeness and good display of website content, greatly enhancing the user experience and robustness of the template.defaultanddefault_if_noneEnglish version: It will make your website content more flexible, avoid unnecessary blank and errors, thus providing a more professional browsing experience.
Common Questions (FAQ)
1.defaultanddefault_if_noneWhat are the differences between filters?
defaultEnglish version: The filter will be applied when the variable isnilAn empty string (""), booleanfalseor numeric0English version: The default value will be applied.default_if_noneFilter is more strict, it only applies the default value when the variable isnil(i.e., the true "none") and for empty strings,falseor0ThesedefaultHandle "seemingly empty" cases more broadly,default_if_noneOnly handle the case of "really not existing."
2.defaultWhat values does the filter consider as blank and apply the default value?
defaultThe filter considers the following types of variable values as blank:
- in Go language,
nil(i.e.,)null). - empty string (
""). - Boolean value
false. - Integer
0. - floating-point numbers
0.0These cases,.defaultThe filter will use the default value you provided.
**3. I can use another variable from the template as `default