In AnQiCMS template development,ifLabels are the core tools for building dynamic page content.It allows us to flexibly control the display of content based on different conditions, thereby providing users with a more intelligent and personalized browsing experience.ifTags can easily help you achieve it.
AnQiCMS's template engine syntax is very similar to Django's template engine, so developers familiar with this syntax will feel very comfortable.ifLabel structure is intuitive and easy to understand, including{% if 条件 %}/{% elif 其他条件 %}(optional),{% else %}(optional) and{% endif %}. With this structure, we can clearly define the logical flow of content.
Determine if a variable exists or is not empty
In practical applications, one of the most common requirements we have is to determine whether a variable has a value or is empty. AnQiCMS'sifLabels follow a 'truth value' concept when judging variables. In simple terms, for most variables, if they are:
- non-empty strings (such as
"Hello World") - non-zero numbers (such as
10/3.14) - Non-empty list, array, or mapping
- Not
nilof the object
they will beiftreated as 'True' (True). Conversely, an empty string"", or a number0、empty list/array/dictionary as wellnilis considered 'False'.
Therefore, to determine whether a variablearchive.TitleIs there content and exists, the simplest way is to use it directly:
{% if archive.Title %}
<h1>{{ archive.Title }}</h1>
{% else %}
<h1>暂无标题</h1>
{% endif %}
If we need to explicitly judge whether a variableis an empty string, we can also use the equality operator:
{% if archive.Description == "" %}
<p>该文章暂无简介。</p>
{% else %}
<p>{{ archive.Description }}</p>
{% endif %}
For variables of list or array type, such asarchive.Images(which may contain multiple images), to determine if it contains images, it is often combined withlengtha filter to obtain its length:
{% if archive.Images|length > 0 %}
<div class="gallery">
{% for img in archive.Images %}
<img src="{{ img }}" alt="图片">
{% endfor %}
</div>
{% else %}
<p>暂无相关图片。</p>
{% endif %}
Of course, you can also usenotKeywords are used to judge in reverse, for example{% if not archive.Thumb %}Used to judge whether the thumbnail field is empty or does not exist.
Judging whether a variable is a specific value
In addition to judging existence or non-empty,ifThe powerful place of the tag lies in its ability to drive content display based on the specific value of the variable. This includes numerical comparison, string matching, and boolean value judgment.
Numerical judgment:We can use common comparison operators:==(equals,)!=(not equal to,)>(greater than,)<(less than,)>=(greater than or equal to,)<=(less than or equal to).
For example, based on the number of views of the article:archive.Viewsto display different recommendation levels:
{% if archive.Views > 1000 %}
<span class="hot-badge">热门文章</span>
{% elif archive.Views > 500 %}
<span class="popular-badge">受欢迎</span>
{% else %}
<span class="new-badge">普通阅读</span>
{% endif %}
String judgment:When the variable is of string type, it can be directly compared with a specific string. For example, AnQiCMS templates can be accessed throughsystemtags to obtain the current language setting of the websitesystem.LanguageWe can display different prompts based on this:
{% if system.Language == "zh-cn" %}
<p>欢迎访问我们的中文网站!</p>
{% elif system.Language == "en-us" %}
<p>Welcome to our English website!</p>
{% else %}
<p>Hello World!</p>
{% endif %}
Boolean value judgment:If the variable itself is a boolean value (trueorfalse), the judgment will be more direct. For example, a certain article'sFlagProperty may contain boolean judgments (although the document mentions)FlagProperty is a string or character, here we assume custom fieldsIsFeaturedIs a boolean value):
{% if archive.IsFeatured %}
<div class="featured-banner">特别推荐</div>
{% endif %}
Check if an element exists in the collection (inoperator)
AnQiCMS template also providesinOperator, convenient for checking if a value exists in a set (such as a list, array, or key of a mapping). This is very useful in scenarios such as handling tags, permissions, or classifications.
For example, if a document may have multipleTagAssuming we want to determine whether a document contains a specific tag name:
{# 假设我们有一个名为 'tags' 的列表,其中包含当前文章的所有标签名 #}
{% set tags = archive.Tags|split:"," %} {# 假设 Tags 字段是逗号分隔的字符串,先用 split 过滤器转换成列表 #}
{% if "AnQiCMS" in tags %}
<span class="tag-highlight">AnQiCMS 相关</span>
{% endif %}
Or, if an element's attribute value is a list, determine if a specific identifier is in the list:
{% if 'h' in archive.Flag %} {# 假设 archive.Flag 是一个包含多个字母的字符串,可以视为集合判断 #}
<span class="flag-h">头条</span>
{% endif %}
Apply in actual scenarios
mastered these basic judgment methods, we can implement various complex logic in the AnQiCMS template.
For example, on the article detail page, we usually display links to 'Previous Article' and 'Next Article'.If the current article is the first one or the last one, the corresponding link should not be displayed.ifthe tags can be put to use:
<nav class="pagination-nav">
{% prevArchive prev %}
{% if prev %}
<a href="{{ prev.Link }}">上一篇:{{ prev.Title }}</a>
{% else %}
<span>没有上一篇了</span>
{% endif %}
{% endprevArchive %}
{% nextArchive next %}
{% if next %}
<a href="{{ next.Link }}">下一篇:{{ next.Title }}</a>
{% else %}
<span>没有下一篇了</span>
{% endif %}
{% endnextArchive %}
</nav>
Another common example is to display different content blocks on the homepage, such as only showing the article list under a certain category when there is content in that category:
English`twig {% categoryList categories with moduleId=“1” parentId=“0” limit=“4” %}
{% for category in categories %}
{% if category.ArchiveCount > 0 %} {# 判断分类下是否有文章 #}
<section class="category-section">
<h2><a href="{{ category.Link }}">{{ category.Title }}</a></h2>
<ul class="article-list">
{%