How to use in AnQiCMS templates?ifTags perform conditional judgments to control content display?
When building website templates, we often need to display or hide specific content blocks based on different conditions.For example, an image is displayed only when the article has a thumbnail, or a welcome message is displayed only when the user logs in.ifThe tag is a powerful tool for implementing this conditional judgment.It uses syntax similar to Django template engine, concise and expressive, making the content display logic clear and easy to understand.
Core concept: What isifTag?
ifThe tag is a control structure used for logical judgment in the AnQiCMS template.Its main function is to determine whether to render the content inside based on the truth value of a certain condition expression.When the condition is true, the content inside the tag will be output to the final HTML page; when the condition is false, this part of the content will be ignored.This allows the template to flexibly adjust the display of the page according to changes in data or context.
The most basicifThe tag structure is as follows:{% if 条件表达式 %}
<!-- 当条件为真时显示的内容 -->
{% endif %}
ifTags appear in pairs and must be presented with{% endif %}end.
Basic Usage: Single condition judgment
The most common usage is to check if a variable exists or its value is "true":
{# 假设有一个名为 `archive` 的文档对象 #}
{% if archive %}
<p>当前页面存在文档内容。</p>
{% endif %}
{# 检查 `archive.Thumb` 是否存在,存在则显示缩略图 #}
{% if archive.Thumb %}
<img src="{{ archive.Thumb }}" alt="{{ archive.Title }}">
{% endif %}
In AnQiCMS templates, the following values are considered "false":false/nilan empty string""numbers0and empty lists or dictionaries. Any other value is usually considered 'true'.
Advanced usage: multi-condition judgment and branch
When you need to handle more complex logic,iftags can be used in conjunction withelif(else if abbreviation) andelseUsed to build multi-branch conditional judgment structures.
{% if archive.Views > 1000 %}
<p>这篇文章非常受欢迎,阅读量超过1000!</p>
{% elif archive.Views > 500 %}
<p>这篇文章有一定热度,阅读量超过500。</p>
{% else %}
<p>这篇文章的阅读量尚不明显。</p>
{% endif %}
In this example, the system will first checkarchive.ViewsDoes it exceed 1000? If true, display the first text; otherwise, continue checking.archive.ViewsIs greater than 500. If true, then display the second paragraph; if the above conditions are not met, then finally displayelseContent in the tag.
Common types of judgment conditions
ifThe conditional expression of the label can be very flexible, supporting various data types and operators:
Does the variable exist or is not emptyThis is the most common judgment, write the variable name directly. If the variable
nilis an empty string or0/falsean empty set, it is considered false.{% if system.SiteIcp %} {# 检查网站备案号是否存在 #} <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">{{ system.SiteIcp }}</a> {% endif %} {% if contact.Cellphone %} {# 检查联系电话是否存在 #} 电话: {{ contact.Cellphone }} {% endif %}Numerical comparison: Use
==(equals,)!=(not equal,)>(greater than,)<(less than,)>=(greater than or equal to,)<=(Less than or equal to) comparison operator.{% if archive.Id == 10 %} {# 检查文档ID是否为10 #} <p>这是ID为10的特殊文档。</p> {% endif %} {% if item.Price < 100 %} {# 检查商品价格是否低于100 #} <span class="sale-price">{{ item.Price }}</span> {% endif %}Boolean value judgment: Directly judge the truth or falsity of a boolean variable or expression, you can use
notkeyword to negate.{% if item.IsCurrent %} {# 检查当前导航项是否是当前页面 #} <li class="active">{{ item.Title }}</li> {% else %} <li><a href="{{ item.Link }}">{{ item.Title }}</a></li> {% endif %} {% if not pages.PrevPage %} {# 如果没有上一页 #} <span class="disabled">上一页</span> {% endif %}String/set contains: Use
inThe operator can check if a value exists in a string, an array (slice), or a dictionary (map).{% set tags = "AnQiCMS, Go, Web" %} {% if "Go" in tags %} {# 检查标签字符串是否包含 "Go" #} <p>这是一个关于 Go 语言的文档。</p> {% endif %} {% set categories = ["新闻", "博客", "产品"] %} {% if "产品" in categories %} {# 检查数组是否包含 "产品" #} <p>分类列表中有产品分类。</p> {% endif %}Logical combination: Use
and(AND),or(OR) operator combines multiple conditions.{% if archive.Views > 100 and archive.CommentCount > 10 %} {# 同时满足两个条件 #} <p>热门且讨论度高的文章。</p> {% endif %} {% if user.IsAdmin or user.IsVip %} {# 满足任一条件 #} <p>欢迎尊贵的会员/管理员!</p> {% endif %}
Examples of actual application scenarios
1. Show/Hide document thumbnail
In the article list or detail page, show the thumbnail if the article has one, otherwise do not show.
{% archiveList archives with type="list" limit="5" %}
{% for item in archives %}
<div class="article-item">
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
{% if item.Thumb %} {# 如果存在缩略图 #}
<a href="{{ item.Link }}">
<img src="{{ item.Thumb }}" alt="{{ item.Title }}">
</a>
{% endif %}
<p>{{ item.Description }}</p>
</div>
{% endfor %}
{% endarchiveList %}
2. Highlight the current item in the navigation menu
Based onitem.IsCurrentProperty to add navigation items corresponding to the current pageactiveClass.
{% navList navs %}
<ul>
{% for item in navs %}
<li {% if item.IsCurrent %}class="active"{% endif %}>
<a href="{{ item.Link }}">{{ item.Title }}</a>
</li>
{% endfor %}
</ul>
{% endnavList %}
3. Control the display status of the pagination button
In the pagination component, disable or hide the corresponding buttons based on whether there is a previous page or next page.
`twig {% pagination pages with show=“5” %}
<div class="pagination-controls">
{% if pages.FirstPage.IsCurrent %}
<span class="disabled">首页</span>
{% else %}
<a href="{{ pages.FirstPage.Link }}">首页</a>
{% endif %}
{% if pages.PrevPage %}
<a href="{{ pages.PrevPage.Link }}">上一页</a>
{% else %}
<span class="disabled">上一页</span>
{% endif %}
{# 中间页码循环 #}
{% for pageItem in pages.Pages %}
{% if pageItem.IsCurrent %}
<span class="current">{{ pageItem.Name }}</span>
{% else %}
<a href="{{ pageItem.Link }}">{{ pageItem.Name }}</a>
{% endif %}
{% endfor %}
{% if pages.NextPage %}
<