Which conditional judgment operations does the `if` logical judgment tag support in the AnQiCMS template?

Calendar 👁️ 52

As an experienced website operations expert, I know that a powerful and flexible template system is crucial for the effective presentation of website content.AnQiCMS (AnQi Content Management System) leverages its high efficiency based on the Go language and the syntax features borrowed from the Django template engine, providing great convenience for our content operation.In AnQiCMS template development, mastering the logic judgment tags is the key to realizing dynamic content display and enhancing user experience.Today, let's delve deep into the AnQiCMS template.ifLogical judgment label supports various conditional judgment operations.

Unlock AnQiCMS template logical judgment: in-depth analysisifThe mystery of tags

In the world of AnQiCMS templates,ifThe tag plays the role of a 'decision-maker'. It allows us to flexibly control the display and hiding of content based on different conditions, thereby achieving more interactive and personalized page effects.Imagine a website with rich content, but not all visitors need to see all the information;Or under certain conditions, we want to highlight certain content - all of this depends onifThe clever use of tags.

AnQiCMS'ifThe tag syntax style is very similar to the Django template engine we are familiar with, it ends with{% if 条件 %}starts with{% endif %}and can be nested in the middle{% elif 其他条件 %}(which is equivalent to else if) and{% else %}A clause, forming a complete conditional judgment structure.

{% if 条件 %}
    <!-- 条件为真时显示的内容 -->
{% elif 另一个条件 %}
    <!-- 当第一个条件为假,但第二个条件为真时显示的内容 -->
{% else %}
    <!-- 所有条件都为假时显示的内容 -->
{% endif %}

Next, let's go through and clarify what flexible and variable operations the 'condition' part supports.

1. True and False Value Judgment: Perceiving the 'Existence' and 'Non-existence' of Data

In many programming languages, some values are automatically evaluated as 'truthy' or 'falsy'. AnQiCMS templateifThe label also follows this principle. Generally, the following situations are considered as 'false values':

  • nilornothing: Indicates that the variable does not exist or is empty.
  • 0: Represents the number zero.
  • empty string"".
  • Empty array, empty slice (slice), or empty map (map).

All other non-empty, non-zero, nonnilThe value is evaluated as 'true'. This allows us to very succinctly determine whether a variable has content, whether a list is empty, etc.

Example:

{# 判断文档标题是否存在或非空 #}
{% if archive.Title %}
    <h1>{{ archive.Title }}</h1>
{% else %}
    <h1>[无标题]</h1>
{% endif %}

{# 判断分类列表是否为空 #}
{% if categories %}
    <ul class="category-list">
        {% for category in categories %}
            <li><a href="{{ category.Link }}">{{ category.Title }}</a></li>
        {% endfor %}
    </ul>
{% else %}
    <p>暂无分类信息。</p>
{% endif %}

2. Judgment of equality and inequality: exact data matching

The most common judgment is the comparison of values.ifTags support standard equality (==) and inequality (!=Operator, used to compare numbers, strings, and other comparable types.

Example:

{# 判断当前文档ID是否为特定值,例如用于展示特定内容块 #}
{% if archive.Id == 10 %}
    <div class="special-promo">
        <p>这是ID为10的特别推荐内容。</p>
    </div>
{% elif archive.Id != 5 %}
    <p>这不是ID为5的文档。</p>
{% endif %}

{# 判断用户组名称 #}
{% if user.GroupName == "VIP" %}
    <p>欢迎尊贵的VIP用户!</p>
{% endif %}

3. Compare numeric values: master the size and range.

For numeric data, we can perform various comparisons of size relationships, such as greater than, less than, greater than or equal to, and less than or equal to.These operators are very useful in handling scenarios such as product prices, article views, and user points.

Supported operators:

  • Greater than:>
  • Less than:<
  • Greater than or equal to:>=
  • Less than or equal to:<=

Example:

{# 根据文章浏览量显示不同样式 #}
{% if archive.Views > 1000 %}
    <span class="badge badge-hot">🔥 热点文章</span>
{% elif archive.Views >= 500 %}
    <span class="badge badge-trending">📈 流行趋势</span>
{% else %}
    <span class="badge">普通文章</span>
{% endif %}

{# 判断产品库存是否充足 #}
{% if product.Stock <= 0 %}
    <span class="out-of-stock">已售罄</span>
{% else %}
    <span class="in-stock">库存充足 (剩余{{ product.Stock }}件)</span>
{% endif %}

4. Logical combination judgment: Build complex conditions

A single condition often cannot meet the needs of complex business logic. AnQiCMS'ifTags support the use of logical operators to combine multiple conditions, thereby creating more refined judgments.

Supported logical operators:

  • AND (And):andor&&. The result is true when all conditions are true.
  • OR:oror||. The result is true when any condition is true.
  • NOT:notor!. The truth value of the condition is reversed.

When combining conditions, it is recommended to use parentheses()to clarify the logical priority and improve code readability.

Example:

{# 判断文章是否为头条推荐且浏览量超过2000 #}
{% if archive.Flag == "h" and archive.Views > 2000 %}
    <p class="featured-headline">【今日头条】此文不可错过!</p>
{% endif %}

{# 判断用户是否为VIP会员或处于试用期 #}
{% if user.IsVIP or user.IsTrialPeriod %}
    <p>您拥有高级功能访问权限。</p>
{% else %}
    <p>升级会员以解锁更多功能。</p>
{% endif %}

{# 判断分类ID不等于1且不是产品模型 #}
{% if archive.CategoryId != 1 and not (archive.ModuleId == 2) %}
    <p>这是一个非默认分类且非产品模型的文档。</p>
{% endif %}

5. Member relationship judgment: Check element ownership

inThe operator is a very convenient feature, it allows us to check if an element exists in a set (such as a string, array, or key of a map).

Example:

{# 判断当前用户所属组是否在特定VIP组列表中 #}
{% if user.GroupName in ["钻石会员", "铂金会员"] %}
    <p>您是尊贵的{{ user.GroupName }},享有专属服务!</p>
{% endif %}

{# 检查文章的Flag属性中是否包含"c"(推荐)标志 #}
{% if "c" in archive.Flag %}
    <p>这篇文章被推荐了。</p>
{% endif %}

By these conditions, AnQiCMS'sifTags can help us achieve powerful logic control at the template level. Whether it is simple content display/hide, or complex personalized display based on user role and data status,ifLabels are indispensable tools for building dynamic and intelligent websites.Mastering these judgment methods will greatly enhance your content operation and template development efficiency on the AnQiCMS platform.


Frequently Asked Questions (FAQ)

Q1:ifCan a tag directly determine if a string variable is empty? A1:Absolutely, it can. In the AnQiCMS template engine, an empty string (""An empty value will be automatically evaluated as a "falsy" value. Therefore, you do not need to make an explicitarchive.Title == ""such judgment, just use{% if archive.Title %}to determineTitlewhether a variable has content. IfTitleIt is an empty string, the condition is false ifTitlethere is any non-empty character, the condition is true.

Q2:ifDoes the tag support regular expressions (Regular Expression) for complex string matching judgments? A2: Designed according to AnQiCMS template syntax,ifThe logical judgment label itself does not directly support regular expressions as conditional judgment operations. It is mainly used for equality and inequality based on variable values, numbers, and strings

Related articles

How does the `stampToDate` tag format timestamps into any custom date and time format?

## Mastering AnqiCMS `stampToDate`: Easily Customize Time Display Format The way time information is presented is crucial in the daily work of content operation.Whether it is the release date of an article, the time a product is listed, or the moment a comment is submitted, a clear and readable date and time format can greatly enhance the user experience.However, we often get a series of difficult-to-understand Unix timestamps from the database - like the number `1609470335`. Don't worry

2025-11-06

How to use the `breadcrumb` tag in the AnQiCMS template to generate breadcrumb navigation?

As an experienced website operations expert, I know that a user-friendly and SEO-optimized website cannot do without a clear navigation structure, and breadcrumb navigation is the key link in this regard.It can not only help users understand their current location, improve the usability of the website, but also provide clear website structure signals for search engines.In AnQiCMS, integrating and using breadcrumb navigation is very simple and efficient, which is mainly due to its built-in `breadcrumb` tag. Today

2025-11-06

How to build a multi-level website navigation menu using the `navList` tag and support dynamic display of categories or document links?

As an experienced website operations expert, I am well aware of the importance of a clear and efficient website navigation system for improving user experience, guiding content consumption, and optimizing search engine rankings (SEO), which is self-evident.In AnQiCMS (AnQiCMS) such a well-designed content management system, building flexible and variable navigation menus, and making them able to intelligently display dynamic content, is the key to operators achieving fine-grained content layout.

2025-11-06

How to use the `pageDetail` tag to get detailed content and images of a single page (such as "About Us")?

In website operation, pages like "About Us" and "Contact Us" are indispensable, as they carry important information such as corporate culture, contact information, and service introduction.How to present these carefully prepared contents efficiently and beautifully on the front end of a website is a skill that every website operator needs to master.For users using AnQiCMS, the `pageDetail` tag is the key tool to achieve this goal.

2025-11-06

The `for` loop tag can iterate over arrays, but it also supports advanced usages like `reversed` or `empty`?

## The `for` loop in Anqi CMS template: Not just iteration, but advanced techniques to help you achieve twice the content operation results As a senior website operation expert, I am well aware of the importance of an efficient and flexible content management system for enterprises.AnQiCMS (AnQiCMS) leverages its high-performance architecture based on the Go language and Django template engine syntax to provide strong support for our content creation and display.In daily operations, we often need to present data collections in the form of lists on websites, at which time, the `for` loop traversal tag is undoubtedly our reliable helper

2025-11-06

How to use `with` or `set` tags to define and assign variables in AnQiCMS templates?

AnQiCMS's template system is known for its flexibility and ease of use, providing strong support for content operators and developers.In the presentation of content, we often need to define temporary variables to handle data, optimize logic, or pass parameters.AnQiCMS template engine borrowed the syntax style of Django templates and provided two very practical tags to help us achieve this goal: `with` and `set`.Understanding their respective features and application scenarios can make your template code clearer and more efficient.

2025-11-06

How to implement code reuse and pass additional variables using the `include` tag in template development?

As an experienced website operations expert, I am well aware of the importance of template development efficiency and code maintainability for a website in my daily work.AnQiCMS (AnQiCMS) relies on its efficient architecture based on the Go language and the Django-style template engine, providing us with powerful content management capabilities.Today, let's delve deeply into a tool that can significantly improve efficiency and code reuse in Anqi CMS template development, namely the `include` tag, especially how it helps us achieve code reuse and flexibly pass additional variables.

2025-11-06

How to implement template inheritance with the `extends` tag and overwrite specific blocks of the parent template?

As an experienced website operations expert, I deeply understand that a flexible and efficient template system is crucial for the long-term development of a website.In AnQiCMS (AnQiCMS), it draws on the powerful capabilities of the Django template engine, allowing you to manage the website interface in a simple and logical way.Today, we will focus on one of the core features - the `extends` tag, and delve into how it implements template inheritance, allowing us to accurately rewrite specific blocks of the parent template.

2025-11-06