AnQiCMS's template system is renowned for its flexibility and efficiency, drawing inspiration from Django's template engine syntax, making content presentation and logical control intuitive.In template development, we often need to display or hide content based on different conditions, at which point, mastering the various usages of conditional judgment becomes particularly important.notOperator to reverse conditional judgment, making your page logic clearer and more flexible.
What isnotOperator ?
In any programming language,notOperators all play the role of "NOT", with a very simple function: to reverse the result of a boolean expression (true or false). If a condition was originally true (True), it becomesnotProcessed, it will become False; conversely, if the condition is false, it will become true.
In AnQiCMS templates,notMainly used for{% if %}Labels inside, helping us build reverse logical judgments. This can make the code intent clearer in many scenarios, or solve some complex logical problems that are difficult to judge directly.
Basic Usage
notThe basic syntax of the operator is very intuitive:
{% if not 你的条件 %}
<!-- 当“你的条件”为假时,这里的内容会显示 -->
{% endif %}
Alternatively, if you want to execute another block of code when the condition is true, you can combineelse:
{% if not 你的条件 %}
<!-- 当“你的条件”为假时,这里的内容会显示 -->
{% else %}
<!-- 当“你的条件”为真时,这里的内容会显示 -->
{% endif %}
Next, let's take a look at specific scenarios from several AnQiCMS templates.notWhat conveniences can it bring?
Practical scenarios and examples
1. Display alternative content or placeholders.
Imagine that your articles may not all have thumbnails.archive.ThumbWhen the thumbnail of the document does not exist or is empty, do you want to display a default image as a placeholder instead of leaving the page blank?
<a href="{{ archive.Link }}">
{% if not archive.Thumb %}
<img src="/public/static/images/default-thumbnail.webp" alt="默认缩略图">
{% else %}
<img src="{{ archive.Thumb }}" alt="{{ archive.Title }}">
{% endif %}
<h3>{{ archive.Title }}</h3>
</a>
Here,{% if not archive.Thumb %}will checkarchive.ThumbIs there a value. If not (it is empty or does not exist), the condition is true and the default thumbnail will be displayed. If there is, the condition is false and the thumbnail of the article itself will be displayed.
2. Handle inactive states or exclude specific items
In the navigation menu or list display, you may want to handle 'non-current' pages or certain specific items differently.
For example, in a category list loop, you want to highlight allnotIt is the link of the currently selected category:
{% categoryList categories with moduleId="1" parentId="0" %}
<ul>
{% for item in categories %}
<li {% if not item.IsCurrent %}class="inactive-category"{% endif %}>
<a href="{{ item.Link }}">{{ item.Title }}</a>
</li>
{% endfor %}
</ul>
{% endcategoryList %}
Here,item.IsCurrentIt is a boolean value indicating whether the current loop category is the one being viewed by the user.{% if not item.IsCurrent %}then it checks 'If this is not the current category', and then it adds ainactive-categoryThe CSS class.
For example, you have a list of documents, but you want to exclude the special document with ID 10:
{% archiveList archives with type="list" limit="10" %}
<ul>
{% for item in archives %}
{% if not item.Id == 10 %}
<li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
{% endif %}
{% endfor %}
</ul>
{% endarchiveList %}
Pass{% if not item.Id == 10 %}We only render those with IDNot equal to10 Article items.
3. Fine control of website status
AnQiCMS supports website shutdown feature, when the website is in shutdown status, it will usually display a shutdown prompt. You can usenotto control when to display normal content:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>{% tdk with name="Title" siteName=true %}</title>
</head>
<body>
{% if system.SiteCloseTips %}
<!-- 如果网站处于闭站状态,显示闭站提示 -->
<div class="site-closed-banner">
<p>{{ system.SiteCloseTips }}</p>
</div>
{% else %}
<!-- 如果网站未闭站,则显示正常网站内容 -->
<header>...</header>
<main>
<h1>欢迎访问我们的网站!</h1>
<!-- 其他正常页面内容 -->
</main>
<footer>...</footer>
{% endif %}
</body>
</html>
Although here we use{% if system.SiteCloseTips %}to directly judge whether the shutdown prompt exists, but another idea is, ifsystem.SiteCloseTipsThe variable does not exist (i.e., the site is not closed), and normal content is displayed, which can also be achieved bynotThis can be achieved:
{% if not system.SiteCloseTips %}
<!-- 网站未闭站,显示正常内容 -->
<header>...</header>
<main>...</main>
<footer>...</footer>
{% else %}
<!-- 网站已闭站,显示闭站提示 -->
<div class="site-closed-banner">
<p>{{ system.SiteCloseTips }}</p>
</div>
{% endif %}
This syntax is also valid, and you can choose according to personal preference or team specifications.
Combineand,orOperator
notThe operator can also be used with other logical operatorsand/orCombined to build more complex conditional judgments. For example:
{% if not (item.IsFeatured and item.Category == "News") %}
<!-- 如果文章不是“特色”且“新闻”类别,则显示此内容 -->
{% endif %}
Herenot (item.IsFeatured and item.Category == "News")Means: If the articlenotIs a featured articleor notBelongs to news category, the condition will be met.
Tip: Reduce empty lines in the template.
In AnQiCMS templates, like{% if %}This logical label may sometimes produce unnecessary blank lines during rendering, affecting the aesthetics of the HTML structure. To avoid this, you can use at the beginning and/or end of the tag.-Symbols, as shown below:
{%- if not archive.Thumb %}
<img src="/public/static/images/default-thumbnail.webp" alt="默认缩略图">
{%- else %}
<img src="{{ archive.Thumb }}" alt="{{ archive.Title }}">
{%- endif %}
In{%- ifand{%- elseas well as{%- endif %}before-, can effectively remove the lines containing these tags and the adjacent whitespace characters, making the final output HTML more compact.
Summary
notThe operator is an AnQiCMS template feature that is simple yet powerful. It helps us handle conditional judgments with reverse thinking, making the template logic more flexible and readable. Whether it is to display alternative content, exclude specific items, or control the visibility of page elements,notCan all play their unique roles. Master its application, and