How to display or hide content blocks in AnQiCMS templates based on conditions (such as `if` statements)?

In website content operation, we often need to display or hide specific content based on different situations, such as showing special event information during holidays or displaying different operation buttons based on the user's status. AnQiCMS provides a flexible template engine that allows you to easily implement these dynamic content controls, with the most core tool being the conditional judgment statement -if.

AnQiCMS's template syntax is similar to the popular Django template engine, and it is very easy to get started. By usingif[en]You can decide which content should be displayed to users based on various conditions in the template, thereby providing visitors to the website with a more accurate and personalized browsing experience.

MasterifThe basic structure of a statement

In AnQiCMS templates,ifThe basic structure of a statement is similar to the programming languages we are familiar with, it always starts with{% if 条件 %}and ends with{% endif %}End.

1. The simplest conditional judgment:{% if 条件 %} ... {% endif %}

When you need to display content only when a specific condition is true (True), you can use this form:

{% if system.SiteCloseTips %}
    <div class="site-alert">
        网站维护中,请稍后再访问:{{ system.SiteCloseTips }}
    </div>
{% endif %}

In this example, if the background is setSiteCloseTips[Website closure prompt content], this prompt information will be displayed. IfSiteCloseTipsis empty or not set, the content block will not be displayed at all.

2. Includes alternative options:{% if 条件 %} ... {% else %} ... {% endif %}

When you want to display a piece of content when a condition is true and another piece of content when the condition is not true,elsethe statement comes in handy:

{% if user.IsLoggedIn %}
    <p>您好,{{ user.UserName }}!欢迎回来。</p>
{% else %}
    <p>您尚未登录,请<a href="/login">登录</a>或<a href="/register">注册</a>。</p>
{% endif %}

Here, different welcome messages or guides are displayed based on whether the user is logged in.

3. Multi-condition judgment:{% if 条件1 %} ... {% elif 条件2 %} ... {% else %} ... {% endif %}

When facing more complex logic, it may be necessary to check multiple different conditions.elif[en]The abbreviation for `else if` allows you to add more conditions in a chained manner:

{% if archive.ReadLevel > 5 %}
    <div class="vip-exclusive">
        此内容为高级会员专属,请升级您的会员等级。
    </div>
{% elif archive.ReadLevel > 0 %}
    <div class="member-only">
        此内容需登录后查看。
    </div>
{% else %}
    <div class="free-content">
        免费内容,欢迎阅读!
    </div>
{% endif %}

[en]This example shows different prompts to users based on the reading level of the document.

flexibly use various conditional judgment expressions

ifThe 'condition' in the statement can be of various kinds, it can be the value of a variable, the result of comparison between variables, or even more complex logical combinations.

  • Equality and inequality judgment:==and!=You can check if a variable's value is equal to.==Or not equal to.!=Another value.

    {% if archive.CategoryId == 10 %}
        <p>这是关于“最新资讯”分类的文章。</p>
    {% endif %}
    
    
    {% if system.Language != "zh-CN" %}
        <p>This content is not in Simplified Chinese.</p>
    {% endif %}
    
  • Size comparison:>,<,>=,<=Commonly used to compare the size of numbers, such as judging stock quantity, price, or reading volume.

    {% if product.Stock <= 0 %}
        <span class="sold-out">已售罄</span>
    {% elif product.Stock < 5 %}
        <span class="low-stock">仅剩少量,欲购从速!</span>
    {% else %}
        <span class="in-stock">有货</span>
    {% endif %}
    
  • Logic combination:and,or,notYou can useand(AND),or(OR) andnot(NOT) to build more complex logic.

    {% if user.IsLoggedIn and user.IsAdmin %}
        <a href="/system/">进入后台管理</a>
    {% endif %}
    
    
    {% if archive.Flag contains "h" or archive.Flag contains "f" %}
        <span class="hot-feature">热门推荐/幻灯片内容</span>
    {% endif %}
    
    
    {% if not archive.Thumb %}
        <p>此文章暂无缩略图。</p>
    {% endif %}
    

    It is worth mentioning that,archive.Flag contains "h"This usage can be used to check if a specific tag (such ashRepresents the top news.)

  • Check if a variable exists or is a 'true' valueIn the AnQiCMS template, the following situations will be considered 'False':

    • nil(Empty value)
    • false(Boolean false)
    • Number0
    • Empty string""
    • An empty array (slice) or key-value pair (map) In most other cases, it is considered to be 'True'.This allows you to simply judge whether a variable has actual content.
    {% if item.Thumb %}
        <img src="{{ item.Thumb }}" alt="{{ item.Title }}">
    {% else %}
        <img src="/static/images/default_thumb.png" alt="无图">
    {% endif %}
    
    
    {% if archive.Description %}
        <p class="description">{{ archive.Description }}</p>
    {% endif %}
    

    This code will checkitem.ThumbWhether there is a value (i.e., the existence of the image path), andarchive.Descriptionwhether there is content, thus determining whether to display the corresponding image or description.

  • Check the members of the collection:inIf you need to determine whether a value exists in a list or a string, you can useinthe operator.

    {% set allowed_users = ["admin", "editor", "moderator"] %}
    {% if user.UserName in allowed_users %}
        <p>欢迎内容管理团队成员!</p>
    {% endif %}
    

Tips for writing neat templates

When usingifWhen writing statements, there are a few tips that can make your template code cleaner and more readable:

  • Control whitespace:Sometimes,