How to use the `not` operator to reverse conditional judgments in AnQiCMS templates?

Calendar 👁️ 61

AnQiCMS's template system is known for its flexibility and efficiency, drawing inspiration from the syntax style of Django template engine, making content presentation and logic control intuitive.In template development, we often need to display or hide content based on different conditions, at this time, mastering various usage of conditional judgment is particularly important.Today, let's talk about how to cleverly use the AnQiCMS templatenotThe operator to reverse the conditional judgment, making your page logic clearer and more flexible.

What isnotOperator ?

In any programming language,notOperators 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), afternotAfter processing it will become false; conversely, if the condition is false, it will become true.

In the AnQiCMS template,not主要用于{% if %}Inside the tag, it helps us construct reverse logical judgments. This can make the code intent clearer in many scenarios or solve some complex logical problems directly.

Basic usage

notThe basic syntax of operators is very intuitive:

{% if not 你的条件 %}
    <!-- 当“你的条件”为假时,这里的内容会显示 -->
{% endif %}

Or, 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 some actual scenarios from several AnQiCMS templatesnotWhat conveniences can it bring.

Practical Scenarios and Examples

1. Display alternate content or placeholders

Imagine that your articles may not all have thumbnails. Whenarchive.Thumb(Document thumbnail) If it does not exist or is empty, 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.ThumbDoes the value exist. If not (empty or does not exist), the condition is true and the default thumbnail is displayed. If there is, the condition is false and the article's own thumbnail is 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 items differently.

For example, in a category list loop, you want to highlight allNotis 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 category being looped over is the one the user is browsing.{% if not item.IsCurrent %}Then it judged 'If this is not the current category', and then added one.inactive-categoryof the CSS class.

For example, you have a list of documents but 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 %}

By{% if not item.Id == 10 %}we only render those with IDnot equal toThe article item of 10.

Fine control of website status.

AnQiCMS supports website shutdown functionality, when the website is in shutdown mode, it will usually display a shutdown prompt. You can usenotto control when normal content is displayed:

<!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 used{% if system.SiteCloseTips %}to directly determine if the shutdown prompt exists, but another approach is, ifsystem.SiteCloseTipsIf the variable does not exist (i.e., the website is not closed), it will display normal content, and this can also be achieved throughnotto achieve:

{% if not system.SiteCloseTips %}
    <!-- 网站未闭站,显示正常内容 -->
    <header>...</header>
    <main>...</main>
    <footer>...</footer>
{% else %}
    <!-- 网站已闭站,显示闭站提示 -->
    <div class="site-closed-banner">
        <p>{{ system.SiteCloseTips }}</p>
    </div>
{% endif %}

This format is also effective, and you can choose according to personal preference or team specifications.

Combineand,orOperator

notThe operator can also be combined with other logical operatorsand/orto 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 the news category, the condition will be met.

Tip: Reduce blank lines in templates

In AnQiCMS templates, like{% if %}This logical tag sometimes generates extra 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.-Symbol, 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 afterwards{%- endif %}add before-It can effectively remove the lines containing these tags and the adjacent whitespace characters, making the final output HTML more compact.

Summary

notThe operator is a simple yet powerful tool in AnQiCMS templates, which helps us process conditional judgments in 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,notEvery role can play its unique role. Master its use and

Related articles

How to construct complex conditional display logic in AnQiCMS templates using `if`, `elif`, and `else` structures?

In AnQiCMS template design, dynamically displaying content is a key factor in enhancing website interactivity and user experience.When we need to decide what and how to display on the page based on specific data conditions, the `if`, `elif` (short for else if), and `else` conditional tags are particularly important.They have given the template flexible logical control capabilities, allowing our website to meet various complex display needs.The condition judgment syntax of the AnQiCMS template engine is similar to many programming languages, it is very intuitive and easy to understand

2025-11-09

How to use the `if` tag in AnQiCMS templates to determine if a variable is empty, exists, or has a specific value?

In AnQiCMS template development, the `if` tag is the core tool 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.Whether you want to judge whether a data exists, whether it is empty, or hope to adjust the layout according to a specific value, the `if` tag can help you easily achieve it.AnQiCMS's template engine syntax is very similar to the Django template engine, therefore, developers familiar with this syntax will feel very亲切。`if`

2025-11-09

How to implement multi-condition logical judgment with the `if` tag in AnQiCMS template (OR/&&, AND/||, NOT/!)?

In AnQiCMS template development, mastering conditional logic judgment is the key to building dynamic and intelligent web pages.Among them, the `if` tag is the core of controlling content display, not only supporting simple boolean judgments, but also being able to flexibly implement logical combinations of multiple conditions, such as "and" (`&&`), "or" (`||`), and "not" (`!`)`etc. Understanding and skillfully using these logical operators can help us control the display and hiding of template elements more finely, thereby creating a more interactive and customizable user experience.### Basic Conditional Judgment: `if`

2025-11-09

How does the `yesno` filter combine with the data list in AnQiCMS templates to display the specific status of each piece of data?

During the process of building a website with AnQiCMS, we often need to display various data on the front-end page, which often has different states.For example, is an article published or a draft, a product listed or not, or is a user active or not.How to clearly and intuitively present these states to the user while keeping the template code concise and readable is a common concern for content operations and template developers.Today, let's delve into a very practical tool in AnQiCMS——the `yesno` filter

2025-11-09

The `in` operator in the AnQiCMS template's `if` statement, how to judge whether an element exists in an array or set?

When developing the AnQiCMS website template, we often need to dynamically display or hide content based on certain conditions.A common requirement is to determine whether an element exists within a dataset, such as checking if a user has a specific role or if the current article has a specific tag.AnQiCMS's template engine provides a concise and powerful `in` operator that can easily solve such problems. ### Core Function Explanation: What is the `in` operator?The design inspiration of AnQiCMS template engine comes from Django

2025-11-09

How to avoid excessive nesting of `if` statements in AnQiCMS templates to improve code readability?

In AnQiCMS template development, we often encounter situations where we need to display different content based on different conditions.Newcomers may tend to use the `{% if condition %}` statement extensively. As the project requirements grow, these conditional judgments become nested layer by layer, which quickly makes the template difficult to read, maintain, and even hides potential logical errors.Code readability once reduced, not only will the development efficiency be affected, but the future feature iteration and problem troubleshooting will also become extremely difficult.Fortunate is that AnQiCMS is based on Go language Django-like

2025-11-09

How to set default display content for possibly empty variables in the `default` filter of AnQiCMS templates?

In Anqi CMS template design, we often encounter situations where variable values may be empty.For example, an article may not have a thumbnail set or a product field may be temporarily unfilled.If the template directly displays these empty values, ugly blank spaces will appear on the page, affecting user experience, and may even confuse visitors.To solve this problem, Anqi CMS provides a very practical filter mechanism, where the `default` filter is the key tool to ensure the continuity of content display.### `default` Filter

2025-11-09

What is the difference between the `default_if_none` filter and the `default` filter in the AnQiCMS template, and when should the former be preferred?

In web template development, handling the case where variables may not exist or are empty is a common task.The AnQiCMS template system provides a variety of filters to help us elegantly handle such issues, with `default` and `default_if_none` being two commonly used tools.They both provide a default value when the variable is 'no value', but there is a subtle but important difference in the definition of 'no value'.Understanding these differences can help us control the display of template content more accurately.###

2025-11-09