How to perform logical NOT (`!` or `not`) operation to reverse boolean conditions in Anqi CMS template?

As a senior website operation expert, I am very happy to elaborate in detail on how to flexibly use logical NOT in AnQiCMS templates.!ornot)Perform the operation to reverse the boolean condition.Auto CMS provides powerful support for content management with its efficient and customizable features based on the Go language.The template system inherits the essence of Django's syntax, making the implementation of complex logic intuitive and practical.

Understand the boolean conditions in the Auto CMS template

In the template design of Anqi CMS, we often need to decide whether to display or hide page elements based on specific conditions. These conditions are usually boolean expressions, and their results can only be two: Englishtrue(true) orfalse(假)。For example, determining if an article has a thumbnail, if a category has subcategories, or if the value of a variable equals a specific number, all these will produce boolean results.

Basic conditional judgment through{% if ... %}Tags implementation. WhenifThe condition aftertruewhenifThe code block inside the tag will be rendered; otherwise, it will be skipped.

For example, you may want to display a piece of text when the article ID is 10:

{% if archive.Id == 10 %}这是文档ID为10的文档{% endif %}

Or check if a variable exists or is true:

{% if hasPromotion %}正在进行促销活动{% endif %}

Logical NOT: a powerful tool for reversing conditions

However, in practical applications, we often encounter situations where we need to use a condition thatnotThe scenario when the condition is met is executed. In this case, the logical NOT operator is particularly important. The Anqi CMS template system provides two equivalent logical NOT operators: exclamation mark!and keywordnot。They all serve the purpose of reversing the true or false state of a boolean value:truebecomesfalse,falsebecomestrue.

For example, if you want to check a variableisEmptywhether it is false (i.e., not empty), you can write it like this: Using!symbols:

{% if !isEmpty %}这个变量不为空{% endif %}

UsenotKeyword:

{% if not isEmpty %}这个变量不为空{% endif %}

These two writing methods are completely equivalent. You can choose to use them based on personal preference or team coding standards. Usually, when simply negating a boolean variable, notThe keyword may sound more natural; while in complex logical expressions,!symbols are more concise.

Practical scenarios and examples of logical NOT

Logical NOT operations are widely used in template development, let's delve deeper through several specific examples.

1. Determine the absence or null value of data

In website operation, it is often necessary to judge whether a data field exists or is empty. For example, a thumbnail of an article may not be available for every article.Thumb), or a product list (products)May be empty under some categories.

When you want to display a default placeholder image when there is no thumbnail in the article, or show a "No content available" prompt when the list is empty, logical NOT operations can come in handy:

{# 当文章没有缩略图时,显示默认占位图 #}
{% if not archive.Thumb %}
  <img src="/static/images/default-thumb.jpg" alt="默认缩略图">
{% else %}
  <img src="{{ archive.Thumb }}" alt="{{ archive.Title }}">
{% endif %}

For list data, you can determine the list variable directly usingifstatement, an empty list will be evaluated asfalse.

{% archiveList products with type="list" categoryId=currentCategory.Id limit="8" %}
  {% if not products %} {# 如果 products 列表为空 #}
    <p>该分类下暂无相关产品。</p>
  {% else %}
    <ul>
      {% for product in products %}
        <li><a href="{{ product.Link }}">{{ product.Title }}</a></li>
      {% endfor %}
    </ul>
  {% endif %}
{% endarchiveList %}

2. The result of the reversed comparison expression

Logical NOT can also be used to reverse comparison operations (such as==/>/<) results. Although using not equal directly!=>=>=<=<=The comparison operator is more direct, but understanding the logic of negation here helps you build more complex logic.

For example, if you want to perform some operation when the article ID is not equal to 10:

{# 两种等效的写法,判断文章ID不等于10 #}
{% if archive.Id != 10 %}
  <p>这不是ID为10的文章。</p>
{% endif %}

{# 等价于: #}
{% if not (archive.Id == 10) %}
  <p>这不是ID为10的文章。</p>
{% endif %}

Here, use!=is obviously more concise, butnot (archive.Id == 10)This structure is very important for understanding the negation logic under complex conditions.

3. Combine logical ANDand)and logical OR(or)Perform complex conditional judgment

When multiple boolean conditions are metandororLogical NOT can negate the result of the combined conditions. Reasonable use of parentheses()It is crucial here to explicitly define the precedence of operations.

Assuming you want to display a registration prompt when “the user is not logged in”andNot a VIP user”

{# 如果用户未登录 并且 不是VIP用户 #}
{% if not (user.IsLoggedIn and user.IsVip) %}
  <p>请注册或升级为VIP会员,以获取更多特权!</p>
{% endif %}

This is equivalent to “the user is not logged in”orUser is not a VIP user (based on De Morgan's Law)。“In this way,you can precisely control the content display in complex scenarios。“

**Practice** when writing templates

  • Choose the appropriate expression:In simple scenarios, such as determining the negation of a single boolean variable,{% if not variable %}often{% if !variable %}is more readable. But in complex arithmetic or comparison expressions,!it may be more compact.
  • The use of parentheses:When performing a logical NOT operation on a compound expression (such as(a and b)or(x > y)) always use parentheses()Wrap the expression to ensure that the logical NOT operator acts on the entire result of the expression, rather than on a part of it. This will effectively avoid priority errors.
  • Blank line control:The template engine of Anqi CMS may produce extra blank lines due to automatic line breaks when processing logic tags, which may affect the HTML structure or visual effects of the page. You can add a hyphen after the start or end percentage sign of the logic tag-Remove these blank lines.
    
    {%- if not archive.Thumb -%}
      <img src="/static/images/default-thumb.jpg" alt="默认缩略图">
    {%- endif -%}
    
    By using inifUse at both ends of the tag{%- ... -%}It can eliminate the tag itself and the blank lines around it, making the rendered HTML cleaner.

Through a deep understanding and proficient application of logical NOT operation, you