How to use conditional judgment (if/elif/else) logic to control the display status of content in AnQiCMS templates?

In Anqi CMS template development, effectively controlling the display status of content is the key to achieving high customization and excellent user experience. Conditional judgment logic, especiallyif/elif/elseThe statement has infused intelligence into the template, making it flexible in presenting information according to different data or situations.Deeply understand and master these logic, making your website content more dynamic and interactive.


The basic conditional judgment syntax in Anqi CMS template

The template engine of AnQi CMS has adopted the syntax style of Django templates, making the conditional judgment logic intuitive and easy to understand. All conditional judgment statements are wrapped in{% %}Tags must be enclosed and end with{% endif %}Tags as the end.

The basic conditional judgment structure is as follows:

{% if 条件表达式 %}
    <!-- 当条件为真时显示的内容 -->
{% endif %}

If you need to handle multiple possibilities, you can introduceelif(else if) andelsestatements:

{% if 条件表达式一 %}
    <!-- 当条件一为真时显示的内容 -->
{% elif 条件表达式二 %}
    <!-- 当条件二为真时显示的内容 -->
{% else %}
    <!-- 当以上所有条件都不为真时显示的内容 -->
{% endif %}

In conditional expressions, you can directly use variables available in the template (such as{{ variable_name }}) for comparison, or combine various filters for more complex judgments.


Core application scenarios and case analysis

The power of conditional judgment lies in its ability to meet various practical content display needs. The following are some common application scenarios and examples:

1. Determine if the data exists or is empty

This is the most basic and commonly used judgment. For example, you may need to check if an article has a thumbnail or if a list contains any content.

Example: Determine if the article has a thumbnailNot all articles in the article list or detail page have thumbnails. Conditional judgment can avoid displaying empty placeholder images.

{% if item.Thumb %}
    <img src="{{ item.Thumb }}" alt="{{ item.Title }}">
{% else %}
    <img src="/static/images/default-thumb.png" alt="默认图片">
{% endif %}

here,item.ThumbIt will automatically determine whether its value is valid. If it exists and is not empty, the condition is true.

Example: Check if the list is emptyWhen usingarchiveListorcategoryListSuch a tag retrieves a data list when the list is empty, you may want to display a prompt message instead of a blank area. AnQi CMS'sforLoop tags are built-in{% empty %}Syntax can elegantly handle this situation:

{% archiveList archives with type="list" categoryId="1" limit="10" %}
    {% for item in archives %}
        <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
    {% empty %}
        <li>目前还没有文章发布到这个分类哦!</li>
    {% endfor %}
{% endarchiveList %}

{% empty %}It willforexecute when the collection in the loop is empty. If you want to judge whether a variable is empty in other non-loop scenarios, you can use the standard{% if variable %}Combine{% else %}.

2. Display control based on the value of a specific field

Website content often needs to present different styles or content blocks based on the specific value of a field.

Example: Display specific content based on the article IDSuppose you want to add a special recommendation to the article detail page with ID 10

{% if archive.Id == 10 %}
    <p class="special-recommend">编辑特别推荐:这篇深度解析不容错过!</p>
{% else %}
    <p>欢迎阅读文章详情。</p>
{% endif %}

here,archive.IdIs the current article ID, through==The operator is compared with numbers.

Example: Display tags based on recommended properties (Flag)The articles of AnQi CMS support various recommendation attributes (such as headline [h], recommended [c], etc.). You can add visual marks to the articles based on these attributes.

{% if item.Flag == "h" %}
    <span class="flag-hot">热点</span>
{% elif item.Flag == "c" %}
    <span class="flag-recommend">推荐</span>
{% endif %}
<span>{{ item.Title }}</span>

When comparing strings, ensure that both sides have the same type and case.

3. Combine logical operators for complex judgments

When a single condition is not enough to express your needs, you can useand(&&) andor(||) ornot(!) Logical operators are used to combine multiple conditions.

Example: Articles that are popular and have images.You may wish to only display image articles with more than 1000 views on the homepage and the recommended attribute 'h'.

{% archiveList hot_articles with type="list" limit="5" %}
    {% for article in hot_articles %}
        {% if article.Views > 1000 and article.Flag == "h" and article.Thumb %}
            <div class="hot-image-article">
                <a href="{{ article.Link }}">
                    <img src="{{ article.Thumb }}" alt="{{ article.Title }}">
                    <h3>{{ article.Title }}</h3>
                </a>
            </div>
        {% endif %}
    {% endfor %}
{% endarchiveList %}

This has been usedandTo ensure that all conditions are met before displaying.

Example: Different user groups display different contentAssume you have a membership system, where regular users and VIP users see different content.

{% userDetail current_user with name="GroupId" %}
{% userGroupDetail group_info with id=current_user %}

{% if group_info.Title == "VIP会员" %}
    <p>尊贵的VIP会员,您好!这里是为您准备的专属内容。</p>
{% else %}
    <p>欢迎普通用户,升级VIP享受更多特权!</p>
{% endif %}

ByuserDetailanduserGroupDetailLabel retrieves the current user's group information and then makes a judgment.

4. In the loop, judge the element at a specific position.

Inforthe loop,forloopThe object provides information about the loop state, such as.forloop.Counter(current loop count, starting from 1) andforloop.RevcounterThe remaining loop count.

Example: Add a special style to the first list item.When displaying a list, it is often necessary to add a prominent style to the first element.

{% archiveList latest_news with type="list" limit="5" %}
    {% for news_item in latest_news %}
        <li {% if forloop.Counter == 1 %}class="first-item-highlight"{% endif %}>
            <a href="{{ news_item.Link }}">{{ news_item.Title }}</a>
        </li>
    {% endfor %}
{% endarchiveList %}

This allows for front-end differentiation to be achieved purely through templates without modifying the backend logic.

5. UseinOperator judgment of inclusion relationship

inThe operator can be used to check if a value exists in a sequence (such as a string, list) or if a key exists in a mapping (such as object properties).

Example: Determine if the article title contains a specific keywordAlthough it is usually handled on the backend, in some template scenarios, you may need to determine if a string contains.

{% if "安企CMS" in article.Title %}
    <p>这篇是关于安企CMS的深度文章!</p>
{% endif %}

Ifarticle.TitleIs "AnQi CMS template creation tutorial", then the condition will be true.


**Practice and Precautions

  1. Keep it concise and clear:Try to avoid writing too complex business logic in the template.