How to use the `{% if ... else ... %}` structure to implement a two-way logic in AnQiCMS templates?

Calendar 👁️ 63

As an experienced website operations expert, I fully understand the importance of a flexible and powerful content management system for website operations.AnQiCMS with its high-performance architecture based on the Go language and similar Django template engine syntax, provides us with great convenience.{% if ... else ... %}It is particularly important.

Today, let's delve deeply into how to use AnQiCMS templates{% if ... else ... %}Structure, making your website content more intelligent and personalized.

AnQiCMS template smart decision-making: Master{% if ... else ... %}Achieve flexible content display

In the world of Anqi CMS templates, content is no longer a monotonous static display, but can change intelligently according to preset conditions. This includes,{% if ... else ... %}The structure is undoubtedly the core tool for realizing this 'intelligent decision-making'.It allows us to decide what to display and what not to display on the page based on the state of the data, the identity of the user, and even specific business logic, greatly enhancing the flexibility of content presentation and the user experience.

Why{% if ... else ... %}So important?

Imagine such a scene:

  • Personalized content display: If the user is a VIP member, display exclusive preferential information; otherwise, display the guidance for registering members.
  • Data integrity checkIf an article has a thumbnail, display it; if not, display a default placeholder or simply do not display the image area.
  • Multi-site content adaptation:In a multi-site management environment, display different navigation or advertisement areas based on the specific needs of the current site.
  • Module switch function: Through a simple configuration item, control the opening or closing of a certain front-end function module in the background.

These common needs in daily operations cannot be separated from{% if ... else ... %}This conditional judgment logic.The AnQiCMS template engine (with syntax similar to the Django template engine) provides an intuitive and easy-to-use syntax, allowing website operators and frontend developers to quickly implement these dynamic effects.

{% if ... else ... %}fundamental grammar and structure

AnQiCMS conditional tags, like loop control tags, are always written using single curly braces and percent signs{% %}to define. They always appear in pairs, which means{% if %}there must be{% endif %}Close it.

The basic structure is like this:

{% if 条件 %}
    <!-- 当条件为真时,显示这部分内容 -->
{% endif %}

If your logic needs to handle the 'otherwise' case, you can introduce{% else %}:

{% if 条件 %}
    <!-- 当条件为真时,显示这部分内容 -->
{% else %}
    <!-- 当条件为假时,显示这部分内容 -->
{% endif %}

When you have multiple mutually exclusive conditions to judge,{% elif %}(else if abbreviation) comes into play:

{% if 第一个条件 %}
    <!-- 当第一个条件为真时,显示这部分内容 -->
{% elif 第二个条件 %}
    <!-- 当第一个条件为假,且第二个条件为真时,显示这部分内容 -->
{% elif 第三个条件 %}
    <!-- 当前两个条件为假,且第三个条件为真时,显示这部分内容 -->
{% else %}
    <!-- 所有条件都为假时,显示这部分内容 -->
{% endif %}

Understanding this hierarchy is to masterifThe key statement: The system will evaluate conditions sequentially from top to bottom, and once it finds the first true condition, it will execute the corresponding code block and then skip the rest.elifandelsePart, go directly to{% endif %}.

Deep into conditional judgment: what can you use to judge?

In{% if %}The "condition" part, you can use a variety of expressions and operators to construct complex logic:

  1. Does the variable exist or is it empty?This is one of the most commonly used judgments. If a variable has a value (non-empty string, non-zero number, non-empty array or object), it is considered true.

    {# 判断文章是否有缩略图 #}
    {% if archive.Thumb %}
        <img src="{{ archive.Thumb }}" alt="{{ archive.Title }}">
    {% else %}
        <img src="/static/images/default-thumb.webp" alt="默认图片">
    {% endif %}
    
    {# 判断变量不存在或为空 #}
    {% if not some_variable %}
        <p>该变量未设置或为空。</p>
    {% endif %}
    
  2. Comparison of numbers and strings: You can use common comparison operators to compare variable values:==(equals,)!=(not equal,)>(greater than,)<(less than,)>=(greater than or equal to,)<=(less than or equal to).

    {# 判断文档ID是否为特定值 #}
    {% if archive.Id == 10 %}
        <p>这是ID为10的特别文档!</p>
    {% endif %}
    
    {# 根据浏览量显示热门标签 #}
    {% if item.Views > 1000 %}
        <span class="hot-badge">热门</span>
    {% endif %}
    
    {# 比较字符串 #}
    {% if category.Title == "安企CMS模板制作" %}
        <p>当前是模板制作分类</p>
    {% endif %}
    
  3. The set contains(in/not in)This operator is very suitable for checking if a value exists in a list, array, or map (map).

    {# 假设 archive.Flag 是一个包含推荐属性的列表,如 ["h", "c"] #}
    {% if "h" in archive.Flag %}
        <span>头条推荐</span>
    {% endif %}
    
    {# 检查某项是否在某个列表内,也可以结合过滤器 #}
    {% set tags = "PHP,Java,Go,Python"|split:"," %}
    {% if "Go" in tags %}
        <p>我们关注 Go 语言。</p>
    {% endif %}
    

    In addition, you can also usecontainA filter to determine if a string or array contains a specific keyword, for example{{ "欢迎使用安企CMS(AnQiCMS)"|contain:"CMS" }}.

  4. Boolean value judgmentDirectly judge the boolean variable.

Related articles

How to perform basic conditional judgments in AnQiCMS templates, such as checking if a variable exists?

## Mastering Content Wisdom: The Art of Variable Judgment in AnQiCMS Templates In the world of websites built with AnQiCMS, templates are the stage for content, and how to make the elements on this stage intelligently display according to different conditions is the core skill that every operations expert and developer needs to master. As an enterprise-level content management system based on Go language and supporting syntax similar to Django template engine, AnQiCMS provides intuitive and powerful template tags, among which the most basic and most commonly used is the variable condition judgment.

2025-11-06

When encountering difficulties in submitting or displaying anomalies in the AnQiCMS message form, how should it be investigated?

Hello, as an experienced website operation expert, I know that when a message form on a website has problems, whether it is unable to submit or shows an error, it will bring a bad experience to users, and may even lead to the loss of important business leads.For AnQiCMS such an efficient and customizable Go language CMS system, although its underlying architecture is stable and reliable, we may also encounter various unexpected situations in actual operation.

2025-11-06

How to add a checkbox for privacy policy or terms of service in the comment form?

## Enhancing Website Compliance: How to Add a Privacy Policy Checkbox Gracefully to Anqi CMS Contact Form In the digital age, user privacy protection has become a cornerstone that cannot be ignored in website operations.Whether it is to deal with the EU's GDPR or similar regulations in other regions, clearly informing users of the data processing methods and obtaining their consent is an important step in building trust and avoiding risks.

2025-11-06

How to manage and set the `placeholder` text of the AnQiCMS message form through the backend?

In the many details of website operation, the design and management of the message form often reflect the professionalism and user-friendliness of the website.A well-designed feedback form that can effectively collect information and enhance user experience.Among them, the `placeholder` text may seem trivial, but it plays an indispensable role in guiding user input and enhancing the clarity of forms.Today, let's delve deeply into how AnQiCMS (AnQiCMS) achieves unified configuration and optimization of the `placeholder` text for the comment form through its powerful backend management system.

2025-11-06

In AnQiCMS template, how to handle multiple conditional branches using `{% if ... elif ... else ... %}`?

## The conditional logic in AnQi CMS template: flexible use of `{% if ...elif ...else ...Implement multi-branch controlAnQiCMS (AnQiCMS) with its efficient, customizable features, provides us with powerful content management capabilities.

2025-11-06

What are the specific application scenarios of the negation judgment `{% if not variable %}` in AnQiCMS templates?

In the Anqi CMS template world, efficiently and flexibly displaying content is the key to the success of website operation.We often need to decide the display of page elements based on the existence or not, and the truth or falsity of the status.And the negation judgment like `{% if not variable %}` is a seemingly simple but extremely powerful tool in template development, which can help us build smarter and more user-friendly website interfaces.As an experienced website operations expert, I know that every detail of the template may affect user experience and operational efficiency.

2025-11-06

How does the `if` tag in AnQiCMS template judge whether a numeric variable is `0` or `0.0`?

How to elegantly judge whether a numeric variable is zero in Anqi CMS template?As an experienced website operations expert, I know how important it is to flexibly use the conditional judgment function of templates in daily content management.Especially when dealing with dynamic data, such as product inventory, article views, or price variables of this kind, we often need to determine whether they are "zero" or "0.0", so as to display different content or execute different logic.

2025-11-06

How to determine if a string variable is empty in AnQiCMS template?

AnQiCMS (AnQiCMS) takes advantage of the efficient and flexible template mechanism of the Go language, bringing great convenience to content management.As an experienced website operations expert, I know that the robustness of template content is crucial when building high-quality, user-friendly websites.How can you elegantly handle empty string variables in templates, which is a common detail problem we often encounter in our daily work.Properly handle these situations, not only can it avoid unnecessary blank spaces or error messages on the page, but also significantly improve the user experience.

2025-11-06