In website content operation, we often need to display or hide specific content based on different situations, such as displaying special event information on holidays or showing different operation buttons based on the user's status. AnQiCMS provides a flexible template engine that allows you to easily implement these dynamic content controls, with the most core tool being the conditional judgment statement -if.
The AnQiCMS template syntax is similar to the popular Django template engine, which is very easy to learn. By usingifThe statement, you can decide in the template which content should be displayed to the user based on various conditions, and which should not be shown, thus providing a more accurate and personalized browsing experience for website visitors.
MasterifBasic structure of the statement
In the AnQiCMS template,ifThe basic structure of the statement is similar to the programming language we are familiar with, it always starts with{% if 条件 %}and ends with{% endif %}end.
1. The simplest conditional judgment:{% if 条件 %} ... {% endif %}
When you only need to display content when a specific condition is true (True), you can use this form:
{% if system.SiteCloseTips %}
<div class="site-alert">
网站维护中,请稍后再访问:{{ system.SiteCloseTips }}
</div>
{% endif %}
In this example, if the background is setSiteCloseTips(Website closed prompt content), this prompt information will be displayed ifSiteCloseTipsis empty or not set, the content block will not be displayed.
2. Include an alternative solution:{% if 条件 %} ... {% else %} ... {% endif %}
When you want to display a piece of content when a condition is true and another piece of content when the condition is not true,elsethe statement comes into play:
{% if user.IsLoggedIn %}
<p>您好,{{ user.UserName }}!欢迎回来。</p>
{% else %}
<p>您尚未登录,请<a href="/login">登录</a>或<a href="/register">注册</a>。</p>
{% endif %}
Here, different greetings or guides are displayed according to whether the user is logged in or not.
3. Multi-condition judgment:{% if 条件1 %} ... {% elif 条件2 %} ... {% else %} ... {% endif %}
Faced with more complex logic, it may be necessary to check multiple different conditions.elif(Else if abbreviation) Allows you to chain more conditions:
{% if archive.ReadLevel > 5 %}
<div class="vip-exclusive">
此内容为高级会员专属,请升级您的会员等级。
</div>
{% elif archive.ReadLevel > 0 %}
<div class="member-only">
此内容需登录后查看。
</div>
{% else %}
<div class="free-content">
免费内容,欢迎阅读!
</div>
{% endif %}
This example displays different prompts to users based on the reading level of the document.
Apply various conditional judgment expressions flexibly
ifThe "condition" can be of various types, it can be the value of a variable, the result of a comparison between variables, or even more complex logical combinations.
Equality and inequality judgments:
==and!=You can check if a variable's value is equal to (==) or not equal to (!=) another value.{% if archive.CategoryId == 10 %} <p>这是关于“最新资讯”分类的文章。</p> {% endif %} {% if system.Language != "zh-CN" %} <p>This content is not in Simplified Chinese.</p> {% endif %}Comparison of size:
>,<,>=,<=Commonly used to compare the size of numbers, such as judging inventory, price, or reading volume.{% if product.Stock <= 0 %} <span class="sold-out">已售罄</span> {% elif product.Stock < 5 %} <span class="low-stock">仅剩少量,欲购从速!</span> {% else %} <span class="in-stock">有货</span> {% endif %}Logical combination:
and,or,notYou can useand(And),or(or) as wellnot(not) to build more complex logic.{% if user.IsLoggedIn and user.IsAdmin %} <a href="/system/">进入后台管理</a> {% endif %} {% if archive.Flag contains "h" or archive.Flag contains "f" %} <span class="hot-feature">热门推荐/幻灯片内容</span> {% endif %} {% if not archive.Thumb %} <p>此文章暂无缩略图。</p> {% endif %}It is worth mentioning,
archive.Flag contains "h"This usage can be used to check if a document's recommended properties contain specific tags such ashrepresenting headlines).Check if the variable exists or is a 'true' valueIn the AnQiCMS template, the following situations will be considered 'false' (False):
nil(Empty value)false(Boolean false)- Number
0 - empty string
"" - An empty array (slice) or key-value pair Most other cases are considered 'true' (True).This allows you to succinctly determine whether a variable has actual content.
{% if item.Thumb %} <img src="{{ item.Thumb }}" alt="{{ item.Title }}"> {% else %} <img src="/static/images/default_thumb.png" alt="无图"> {% endif %} {% if archive.Description %} <p class="description">{{ archive.Description }}</p> {% endif %}This code will check
item.ThumbWhether there is a value (i.e., whether the image path exists), as well asarchive.DescriptionWhether there is content, thus determining whether to display the corresponding image or description.Check the members of the set:
inIf you need to determine whether a value exists in a list or string, you can useinoperator.{% set allowed_users = ["admin", "editor", "moderator"] %} {% if user.UserName in allowed_users %} <p>欢迎内容管理团队成员!</p> {% endif %}
tips for writing neat templates
While usingifWhen writing statements, there are several tips that can make your template code cleaner and more readable:
- Control whitespace:Sometimes, `