When using AnQiCMS to build a website, we often encounter such situations: some content fields may not have values every time, such as 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.
Fortunately, AnQiCMS's 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. UsedefaultFilter settings basic default value
The most direct and commonly used method is to use the built-in AnQiCMS template.defaultThe filter. 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 valuesfalsenumbers0and in Go languagenil.
is very intuitive, just pass through the pipe symbol after the variable name|连接defaultfilter, and specify your default text with double quotes.
For example, if your article summaryarchive.DescriptionMay be empty, and you want 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>
So, even ifarchive.Descriptionorcontact.WhatsAppIn the database, it is empty, and there will be no blank on the page, but 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
Under certain specific scenarios, you may need to make a more accurate judgment, only for variable values ofnil(indicating that the variable does not exist or is not assigned, rather than just an empty string) the default value is set, while an empty string is retained as is. AnQiCMS provides for thisdefault_if_nonefilter.
This filter is compatible withdefaultThe main difference is that it only targetsnilvalue takes effect. If the variable's value is an empty string ("")default_if_noneit will treat it as a valid value and display it, whereasdefaultit will be considered as "empty" and display the default value.
The way to use withdefaultis the same as the filter:
<p>文章作者: {{ archive.Author|default_if_none:"匿名读者" }}</p>
Assumearchive.AuthorField:
- If it is
nil: Display "Anonymous Reader". - If it is
""(Empty string): Display an empty string (i.e., no content is displayed). - If it is
"张三": Display "Zhang San".
Understand the subtle differences between these two filters, which can help you control the rendering logic of the template more accurately.
3. Use conditional judgment{% if %}Tag rendering of different structures
In addition to setting default text directly, sometimes you may need to display a completely different content structure based on the existence of variables, or even not display certain blocks. AnQiCMS templates support syntax similar to Django's{% if 条件 %}Label, 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 particularly useful when handling images, ad slots, and optional content modules.
For example, to set a default thumbnail for an article:
We first obtain the article's logo image througharchiveDetailLabel to get the article's logo image, and then use{% if %}to judge.
{% 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: ifarticleLogoThe variable has a value (i.e., the article has an accompanying image), then display this image.Otherwise, display a preset default thumbnail. This can prevent layout errors on the page or damage to the placeholder due to missing images.
For example, some contact information is displayed with a link only when it has 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 cases, you may want 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 %}To achieve this with a tag.
{% set %}Define a local variable:{% set %}The tag is 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 assigned.archive.TitleThe value, ifarchive.Titleis empty, then use 'No Title Article' as the default value. After that, we can reuse it multiple timesarticleTitle.{% with %}Define scope variables:{% with %}Tags are used to define one or more variables within a specified block, which are only valid in{% with %}and{% endwith %}between. It can also be used to pass default values in{% include %}tags. It is also valid within a{% with defaultDesc="此内容暂无详细描述。请等待后续更新。" %} <p>{{ archive.Description|default:defaultDesc }}</p> {% endwith %}In this example,
defaultDesconly{% with %}block, used to providearchive.DescriptionSet default value.
Summary
In AnQiCMS template, setting a default display value for variables that may be empty is a key step in building a robust and user-friendly website.
- For simple text content missing, use directly
defaultFilteris the most convenient way. - Need to distinguish strictly
nilAnd for empty strings,default_if_noneFilterProvided more refined control. - When it is necessary to display different HTML structures based on the existence of variables,
{% if %}Conditional judgmentit is indispensable. - And
{% set %}or{% with %}TagIt 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