Ride the content wisdom: The Art of Variable Judgment in AnQiCMS Templates
In the AnQiCMS website building world, templates are the stage for content, and how to make the elements on this stage intelligently display according to different conditions is the core skill that every operation expert and developer needs to master.As an enterprise-level content management system based on Go language and supporting syntax similar to Django template engine, AnQiCMS provides intuitive and powerful template tags. The most basic and commonly used among them is the conditional judgment of variables, especially the judgment of whether a variable exists or has a value.
Imagine if your website needs to display the author's avatar on an article detail page, but some articles may not have one set; or, on a product list page, you only want to show the dropdown menu when there are subcategories.These scenarios all rely on observing variables.Today, let's delve deeply into how to perform the most basic conditional judgment elegantly in the AnQiCMS template, especially the judgment of whether a variable exists and has a value.
I. Basic Conditional Judgment in AnQiCMS Templates
AnQiCMS's template syntax follows the style of Django template engine, conditional judgment uses{% if ... %}Label pair. It allows us to decide whether to render a specific content block based on the truth or falsity of an expression. A complete conditional judgment structure usually includes{% if %}, optional{% elif %}(else if) and{% else %}, and the final{% endif %}End the judgment.
For example, the simplest judgment might be like this:
{% if archive.Id == 10 %}
<p>这是文档ID为10的文档的特别内容。</p>
{% else %}
<p>这不是文档ID为10的文档。</p>
{% endif %}
This structure provides the basic framework for controlling the display of page elements. Similarly, we can also use it ingeniously to determine whether a variable exists or has a value.{% if %}Label.
English,判断变量是否存在及有值:核心技巧与实践
In AnQiCMS templates, the existence or value of a variable mainly depends on the template engine's understanding of 'truthy' and 'falsy' values. When a variable is defined but its value is an empty string, 0,nilWhen the value is empty (such as an empty value, empty collection like an empty array, empty slice, or empty dictionary), it is considered as 'falsy' by the template engine; otherwise, it is 'truthy'.
1. Directly judge the variable name: the most concise 'existence' judgment
The most direct way to determine whether a variable exists or has a value is to place it{% if %}Tag within. If the variable exists and is considered a 'true value', the condition is met.
Suppose we are displaying a document and we want to show a thumbnail only if the document has one set.ThumbIt is displayed only when:
{% if archive.Thumb %}
<img src="{{ archive.Thumb }}" alt="{{ archive.Title }}" />
{% else %}
<p>暂无缩略图。</p>
{% endif %}
Here,archive.ThumbIf it is a valid image URL string, it is a "true value", and the image will be displayed. Ifarchive.Thumbis an empty string ornilIt is called “false value”, and it will display “No thumbnail available”. This method is concise and efficient, and it is commonly used in template development as a judgment technique.
The same logic applies to判断数字、布尔值等:
{# 如果 archive.Views(浏览量)大于0,则显示 #}
{% if archive.Views %}
<span>浏览量:{{ archive.Views }}</span>
{% endif %}
{# 如果 item.IsCurrent(是否当前)为true,则添加active类 #}
<li class="{% if item.IsCurrent %}active{% endif %}">
<a href="{{ item.Link }}">{{ item.Title }}</a>
</li>
2. Judge if the set (list) is empty:{% for ... empty %}elegant application of
when we need to judge a set (such asarchiveListorcategoryListDoes the returned list contain an element when, in addition to using{% if collection_name %}AnQiCMS also provides a more elegant and semantically meaningful{% for ... empty %}structure.
This structure allows you to execute the content in the block when traversing the collection, if the collection is empty{% empty %}the content in the block:
{% archiveList archives with type="list" categoryId="1" limit="10" %}
{% for item in archives %}
<li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
{% empty %}
<li>该分类下没有任何文档。</li>
{% endfor %}
{% endarchiveList %}
This code clearly expresses the logic of 'If there is a document, traverse and display it; otherwise, prompt that there is no content', avoiding extra{% if %}judgment, making the code more compact and readable.
3. English variable does not exist (negative judgment): Use{% if not %}
Sometimes, we may need to judge whether a variablenotexists orNonethe value is executed. At this point, you can addnotkeywords.
For example, on the article detail page, we often display "Previous" and "Next
{% prevArchive prev %}
<div>
上一篇:
{% if not prev %}
<span>没有了</span>
{% else %}
<a href="{{ prev.Link }}">{{ prev.Title }}</a>
{% endif %}
</div>
{% endprevArchive %}
{% nextArchive next %}
<div>
下一篇:
{% if not next %}
<span>没有了</span>
{% else %}
<a href="{{ next.Link }}">{{ next.Title }}</a>
{% endif %}
</div>
{% endnextArchive %}
{% if not prev %}It will determineprevVariable is a 'false value' (i.e., does not exist or is empty), thus deciding whether to display a prompt message or the actual link.
4. UsedefaultFilter provides default value: display judgment
except for controlling the display of content blocks, sometimes we just want to display a preset default text when a variable has no value, rather than controlling the logic of the entire code block. At this point, we can usedefaultFilter.
defaultThe filter returns the default value you specify when the variable is a "false value". Please note that this does not change the rendering logic of the template, but only provides an alternative display content.
<p>作者:{{ archive.Author|default:"佚名" }}</p>
<p>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02")|default:"未知日期" }}</p>
In this example, ifarchive.AuthorNo value, the page will display "Anonymous"; ifarchive.CreatedTimeThis can be formatted (for example, a value of 0 or nil), and then it will display 'Unknown date'. This is especially suitable for optional text information that does not affect the core layout of the page.
Moreover, there is alsodefault_if_nonea filter that is more strict, and only provides a default value whennilthe variable is a (nil pointer) in the Go language, unlikedefaultIt also applies to empty strings, 0, and other values. In most web template scenarios,defaultit is usually enough to use.
3. Applications in actual scenarios
You can control the display of content with ease in the AnQiCMS template after mastering these basic conditional judgment skills:
- **Lazy loading of image resources