In website content management, data integrity and consistency are crucial.However, in actual operation, we often encounter situations where certain variables, strings, or objects may be empty.If the template does not handle these null values properly, the front-end page may appear blank, disordered, or even crash, severely affecting user experience.AnQiCMS (AnQiCMS) provides a variety of flexible and powerful template tags and filters to help us elegantly handle potential null values, ensuring the stability and beauty of the website content.
The AnqiCMS template system has adopted the Django syntax, it provides a series of intuitive tools to set default display values, so that even empty areas can be displayed properly, thereby enhancing the professionalism and customer satisfaction of the website.
Core Strategy One: Skillfully Use Default Value Filters
In the AnQi CMS template,defaultA filter is a very practical tool that allows us to set a backup value for variables that may be empty or undefined. When the value of the variable isfalse/0an empty string""or an empty listdefaultthe filter will come in handy.
for example, if your article title variableitem.TitleSometimes it may be empty, you do not want to have a blank space on the page, but instead display a default prompt, you can use it like this:{{ item.Title|default:"无标题内容" }}So, even ifitem.TitleEmpty, the page will also display 'No title content'.
Another closely related filter isdefault_if_none. It is withdefaultSimilar, but more focused on processingnil(i.e., in Go language)null). In some cases, variables may be explicitly set tonilIt is not an empty string or zero. At this point,default_if_noneit ensures that you set a fallback value.{{ someVariable|default_if_none:"N/A" }}This filter is particularly effective in handling potentially empty fields retrieved from the database, as it helps ensure that the page displays friendly and complete information.
Core Strategy Two: Use conditional judgment to achieve flexible control
When you need to decide on different content structures to display based on whether a variable exists or is empty,ifLogic judgment tags become your powerful assistant. It allows you to write more complex conditional logic to meet diverse display requirements.
For example, when displaying a thumbnail of an article or category, you may want to show it only when the image actually exists.<img>Tag, otherwise do not display, or display a placeholder image:
{% if item.Thumb %}
<a href="{{ item.Link }}">
<img src="{{ item.Thumb }}" alt="{{ item.Title|default:'图片' }}">
</a>
{% else %}
{# 如果没有缩略图,可以显示一张默认的占位图 #}
<a href="{{ item.Link }}">
<img src="/static/images/default-thumb.png" alt="{{ item.Title|default:'默认图片' }}">
</a>
{% endif %}
By{% if ... %}and{% else %}Structure, you can provide multiple guarantees for the content to ensure that the page can present elegantly regardless of the data status.
Core Strategy Three: The elegant way to handle empty lists.
When displaying article lists, comment lists, or friend links and other content, data is usually provided in the form of arrays or slices.If these lists happen to be empty, directly traversing them may cause the page to be blank.AnQi CMS offorA loop tag provides a{% empty %}branch, specifically used to handle the case where the list is empty.
For example, when displaying an article list:
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<li>
<a href="{{ item.Link }}">
<h5>{{ item.Title|default:'暂无标题' }}</h5>
<div>{{ item.Description|default:'暂无简介' }}</div>
</a>
</li>
{% empty %}
<li>
<p>当前分类下暂无文章,敬请期待!</p>
</li>
{% endfor %}
{% endarchiveList %}
This method is better than adding an extra oneforoutside the loopifJudgment is more concise and readable, it combines the logic of 'traversal' and 'empty state handling' closely together.
Practical application scenario
Apply these strategies to the daily display of website content, which can greatly enhance the robustness and user experience of the template:
Website title, keywords, and description (TDK):Use
tdkWhen fetching the TDK information of the page, it can be combineddefaultThe filter ensures that even if the backend is not filled in, the front-end also displays a reasonable default value.<title>{% tdk with name="Title" siteName=true %}|{% system with name="SiteName"|default:"我的网站" %}</title><meta name="description" content="{% tdk with name="Description" %}|{% system with name="SiteName"|default:"这是一个提供优质内容的网站" %}">Images and links in the content details:On the article or product detail page, if the Logo or specific image field may be empty, you can use conditional judgment to display the default image or hide the image placeholder.
{% archiveDetail logo with name="Logo" %}{% if logo %}<img src="{{ logo }}" alt="{% archiveDetail with name='Title' %}" />{% else %}<img src="/static/images/default-product.jpg" alt="默认产品图片" />{% endif %}Display of custom field:When custom fields are defined in the content model but not all content may be filled in, use
defaultThe filter can prevent blank output.{% archiveParams params with sorted=false %}{% if params.author %}<p>作者: {{ params.author.Value|default:"匿名作者" }}</p>{% endif %}Or directly in the loop:<span>{{ item.Name }}:{{ item.Value|default:"未填写" }}</span>Contact information or friend link:Even if no contact information or friend link is set, you can still access it:
ifDetermine to control the display of the entire block, avoiding unnecessary empty areas.{% contact cellphone with name="Cellphone" %}{% if cellphone %}<div>联系电话:{{ cellphone }}</div>{% endif %}
By proficiently using these template techniques, you will be able to build a more stable, professional, and user-friendly Anqi CMS website.
Frequently Asked Questions (FAQ)
Q1:defaultanddefault_if_noneWhat are the main differences between the filter in AnQi CMS template?A1: The main difference lies in their definition of 'empty'.defaultThe filter handles various 'empty' values, including empty strings.""numbers0and a boolean valuefalseand empty lists, etc.default_if_noneThe filter is stricter, it only takes effect when the value of the variable isnil(i.e., in Go language)null). This means that if you want to set a default value for a variable that really has no value, default_if_noneMore precise; if a variable may appear to be empty for various reasons (such as an empty string or zero),defaultit is more general.
Q2: How can I set a default placeholder image for images that may not exist in the template?A2: You can useifstatements to combinedefaultOr specify the default image path directly. For example, for article thumbnailsitem.Thumb:
{% if item.Thumb %}
<img src="{{ item.Thumb }}" alt="{{ item.Title|default:'文章图片' }}">
{% else %}
<img src="/static/images/placeholder.jpg" alt="默认占位图">
{% endif %}
to/static/images/placeholder.jpgReplace with the actual default image path.
Q3: When myarchiveListorcategoryListThere is no tag query result, how can you politely prompt the user with 'No content'?A3: You canforUse loop tags inside{% empty %}Tags to handle this situation. Whenforthe list being traversed by a loop is empty,{% empty %}The content inside the block will be rendered.
{% archiveList archives with type="list" categoryId="1" limit="10" %}
{% for item in archives %}
{# 正常显示文章内容的 HTML 代码 #}
<p>{{ item.Title }}</p>
{% empty %}
{# 列表为空时显示的内容 #}
<p>抱歉,此分类下暂无文章发布。</p>
{% endfor %}
{% endarchiveList %}
This method provides a concise, efficient, and easy-to-maintain solution for handling empty content.