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,ifTags are the core tools for building dynamic page content. They allow 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 determine whether a piece of data exists, whether it is empty, or want to adjust the layout based on a specific value,ifTags can easily help you achieve that.

AnQiCMS's template engine syntax is very similar to the Django template engine, so developers familiar with this syntax will feel very亲切.ifThe basic structure of labels is intuitive and easy to understand, including{% if 条件 %}/{% elif 其他条件 %}(optional),{% else %}(optional) and{% endif %}. Through this structure, we can clearly define the logical flow of content.

Determine if the variable exists or is not empty

In practical applications, one of the most common needs is to determine whether a variable has a value or is empty. AnQiCMS'sifThe label follows a concept of 'truth value' when judging variables. Simply put, for most variables, if they are:

  • non-empty strings (such as"Hello World")
  • non-zero numbers (such as10/3.14)
  • non-empty list, array, or mapping
  • notnilof the object

they will beiflabeled as 'True'. Conversely, an empty string""numbers0, an empty list/array/map as wellnilis considered "False".

Therefore, to determine whether a variablearchive.TitleDoes it exist and have content, the simplest way is to use it directly:

{% if archive.Title %}
    <h1>{{ archive.Title }}</h1>
{% else %}
    <h1>暂无标题</h1>
{% endif %}

If we need to explicitly determine whether a variable isan empty string, we can also use the equality operator:

{% if archive.Description == "" %}
    <p>该文章暂无简介。</p>
{% else %}
    <p>{{ archive.Description }}</p>
{% endif %}

For a list or array variable, for examplearchive.Images(which may contain multiple images), to determine if it contains images, it is usually combinedlengthwith a filter to get its length:

{% if archive.Images|length > 0 %}
    <div class="gallery">
        {% for img in archive.Images %}
            <img src="{{ img }}" alt="图片">
        {% endfor %}
    </div>
{% else %}
    <p>暂无相关图片。</p>
{% endif %}

Of course, you can also usenotReverse judgment using keywords, for example{% if not archive.Thumb %}Used to determine if the thumbnail field is empty or does not exist.

Determine if the variable is a specific value

In addition to checking for existence or non-empty,ifThe tag is powerful because it can drive content display based on the specific value of the variable. This includes numerical comparisons, string matching, and boolean judgments.

Numerical judgment:We can use common comparison operators:==(equal to),!=(Not equal),>(greater than),<(Less than),>=(Greater than or equal),<=(Less than or equal).

For example, based on the number of views of the article:archive.ViewsTo display different recommendation levels:

{% if archive.Views > 1000 %}
    <span class="hot-badge">热门文章</span>
{% elif archive.Views > 500 %}
    <span class="popular-badge">受欢迎</span>
{% else %}
    <span class="new-badge">普通阅读</span>
{% endif %}

String judgment:When a variable is of string type, it can be directly compared with a specific string. For example, the AnQiCMS template can usesystemtags to get the current language setting of the websitesystem.LanguageWe can display different prompts based on this:

{% if system.Language == "zh-cn" %}
    <p>欢迎访问我们的中文网站!</p>
{% elif system.Language == "en-us" %}
    <p>Welcome to our English website!</p>
{% else %}
    <p>Hello World!</p>
{% endif %}

Boolean value judgment:If the variable itself is a boolean value(trueorfalse)the judgment will be more direct. For example, for an article'sFlagProperties may contain boolean judgments (although the documentFlagProperties are strings or characters; here we assume custom fieldsIsFeaturedare boolean values):

{% if archive.IsFeatured %}
    <div class="featured-banner">特别推荐</div>
{% endif %}

To determine if an element exists in a collection (inoperator)

AnQiCMS template also providedinThe operator makes it convenient to check whether a value exists in a set (such as a list, array, or a key of a mapping). This is very practical when dealing with labels, permissions, or categorization scenarios.

For example, if a document may have multipleTagAssuming we want to determine if a document contains a specific tag name:

{# 假设我们有一个名为 'tags' 的列表,其中包含当前文章的所有标签名 #}
{% set tags = archive.Tags|split:"," %} {# 假设 Tags 字段是逗号分隔的字符串,先用 split 过滤器转换成列表 #}
{% if "AnQiCMS" in tags %}
    <span class="tag-highlight">AnQiCMS 相关</span>
{% endif %}

Or, if an element's attribute value is a list, determine whether a certain identifier is in the list:

{% if 'h' in archive.Flag %} {# 假设 archive.Flag 是一个包含多个字母的字符串,可以视为集合判断 #}
    <span class="flag-h">头条</span>
{% endif %}

Apply in real-world scenarios

We have mastered these basic judgment methods, and we can implement various complex logic in the AnQiCMS template.

For example, on the article detail page, we usually display the links to the previous and next articles.But if the current article is the first or last one, the corresponding link should not be displayed. At this time,ifThe tags can be put into use:

<nav class="pagination-nav">
    {% prevArchive prev %}
        {% if prev %}
            <a href="{{ prev.Link }}">上一篇:{{ prev.Title }}</a>
        {% else %}
            <span>没有上一篇了</span>
        {% endif %}
    {% endprevArchive %}

    {% nextArchive next %}
        {% if next %}
            <a href="{{ next.Link }}">下一篇:{{ next.Title }}</a>
        {% else %}
            <span>没有下一篇了</span>
        {% endif %}
    {% endnextArchive %}
</nav>

Another common example is to display different content blocks on the homepage, such as only showing the article list under a certain category when there is content in that category:

`twig {% categoryList categories with moduleId=“1” parentId=“0” limit=“4” %}

{% for category in categories %}
    {% if category.ArchiveCount > 0 %} {# 判断分类下是否有文章 #}
        <section class="category-section">
            <h2><a href="{{ category.Link }}">{{ category.Title }}</a></h2>
            <ul class="article-list">
                {%