How to use conditional judgment logic to control the display and hiding of content in a template?

In the presentation of website content, we often need to decide based on different situations which information should be displayed to visitors and which should be temporarily hidden.AnQiCMS provides flexible template conditional judgment logic, making the display and hiding of content intuitive and efficient.Through these judgments, you can easily achieve personalized page layouts, content display under permission control, and adjusting page elements based on data status, greatly enhancing the dynamicity and user experience of the website.

The template system of AnQi CMS adopts syntax similar to Django's template engine, making the structure of conditional judgment clear and easy to understand. The core conditional judgment statement is{% if ... %}It allows you to check if a condition is true and execute the corresponding code block accordingly.

The conditional judgment syntax in Anqi CMS: The Core Foundation

The most basic conditional judgment structure is{% if 条件 %} ... {% endif %}). When条件Expression evaluates to true,ifandendifthe content between them will be displayed.

If you need to handle more complex logic, you can introduce{% elif 其他条件 %}and{% else %}.elifthat allows you to add more conditional branches, andelseThen it is the default handling when all conditions are not met.

{# 简单条件判断 #}
{% if archive.Id == 10 %}
    <p>这是ID为10的特别文章。</p>
{% endif %}

{# 包含多个分支的判断 #}
{% if user.is_admin %}
    <p>欢迎,管理员!</p>
{% elif user.is_vip %}
    <p>尊贵的VIP用户,您好!</p>
{% else %}
    <p>普通用户,请登录。</p>
{% endif %}

Here, 'condition' can be various expressions, including variables, comparison operations such as==/!=/>/</>=/<=), logical operations (and/or/not), even some functions or filter results.

Common application scenarios and practical examples

After understanding the basic syntax, let's take a look at how to use these conditional judgments to control content display in actual enterprise CMS template development.

1. Based on whether a variable exists or is empty

This is one of the most commonly used scenarios.Many times, we want to display related content only when certain data exists, such as showing a thumbnail for an article or displaying a system setting item that has been configured.nilandfalsewhether it is zero (number), or an empty array/slice, it will be considered asfalse.

{# 判断文章是否有缩略图 #}
{% if archive.Thumb %}
    <img src="{{ archive.Thumb }}" alt="{{ archive.Title }}"/>
{% else %}
    <span>此文章暂无缩略图。</span>
{% endif %}

{# 判断网站备案号是否配置 #}
{% system siteIcp with name="SiteIcp" %}
{% if siteIcp %}
    <p>备案号:<a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">{{ siteIcp }}</a></p>
{% else %}
    {# 不显示备案号区域 #}
{% endif %}

2. Based on a specific value for judgment

You may need to display different content based on the specific value of the variable. For example, whether to add a friend link.rel="nofollow"Properties, or display different copy based on the current page language.

{# 友情链接根据后台设置决定是否添加nofollow #}
{% linkList friendLinks %}
    {% if friendLinks %}
        <ul>
            {% for item in friendLinks %}
                <li><a href="{{ item.Link }}" {% if item.Nofollow == 1 %} rel="nofollow" {% endif %} target="_blank">{{ item.Title }}</a></li>
            {% endfor %}
        </ul>
    {% endif %}
{% endlinkList %}

{# 根据系统语言显示不同内容 (假设您在后台设置了Language字段) #}
{% system currentLang with name="Language" %}
{% if currentLang == "zh-CN" %}
    <p>欢迎来到中文站!</p>
{% elif currentLang == "en-US" %}
    <p>Welcome to our English site!</p>
{% else %}
    <p>语言未知。</p>
{% endif %}

3. Logical combination judgment

When a single condition is not sufficient to express your needs, you can useand/orandnotto combine multiple conditions and build more complex logic.

{# 判断分类既有子分类,且子分类下有文章,才显示特定布局 #}
{% categoryList currentCategory with parentId="0" %} {# 获取当前分类,假设它是一个父级分类 #}
{% if currentCategory.HasChildren and currentCategory.ArchiveCount > 0 %}
    <h3>包含子分类和文章的模块</h3>
    {# ... 展示子分类和文章的复杂布局 ... #}
{% else %}
    <h3>普通分类模块</h3>
    {# ... 展示普通分类内容 ... #}
{% endif %}

{# 判断如果没有Logo图片,且网站名称不为空,则显示网站名称作为Logo替代 #}
{% system siteLogo with name="SiteLogo" %}
{% system siteName with name="SiteName" %}
{% if not siteLogo and siteName %}
    <h1>{{ siteName }}</h1>
{% elif siteLogo %}
    <img src="{{ siteLogo }}" alt="{{ siteName }}"/>
{% endif %}

4. Condition judgment in loops

InforIn loops, conditional judgments are also very useful. For example, adding special styles to the first element of the loop, or handling the case of an empty list.

{% archiveList archives with type="list" limit="5" %}
    <ul>
    {% for item in archives %}
        {# 为第一个列表项添加'active'类名 #}
        <li {% if forloop.Counter == 1 %}class="active"{% endif %}>
            <a href="{{ item.Link }}">{{ item.Title }}</a>
        </li>
    {% empty %}
        {# 如果archives列表为空,显示此内容 #}
        <li>暂无文章可显示。</li>
    {% endfor %}
    </ul>
{% endarchiveList %}

Here are theforloop.Counteris a special variable that represents the current iteration number of the loop (starting from 1).{% empty %}is a block that isfora special judgment executed when the loop iteration object is empty, which allows you to not use a separateif archivesjudgment.

5. Enhance conditional judgment with filters

The AnQi CMS template provides rich filters that can be combined with conditional judgments to achieve more fine-grained control.For example, judge the length of a string, whether it contains a specific substring, etc.

{# 判断文章描述的长度,决定是否显示“阅读更多” #}
{% archiveDetail archiveDesc with name="Description" %}
{% if archiveDesc|length > 100 %}
    <p>{{ archiveDesc|truncatechars:100 }}...</p>
    <a href="{{ archive.Link }}">阅读更多</a>
{% else %}
    <p>{{ archiveDesc }}</p>
{% endif %}

{# 判断文章标题是否包含特定关键词,例如“促销” #}
{% archiveList archives with type="list" limit="3" %}
    {% for item in archives %}
        <li>
            {% if item.Title|contain:"促销" %}
                <span style="color: red;">【促销】</span>
            {% endif %}
            <a href="{{ item.Link }}">{{ item.Title }}</a>
        </li>
    {% endfor %}
{% endarchiveList %}

Optimization and注意事项

When using conditional judgment, there are several tips to help you keep your template clean and efficient:

  • Keep it simple:Avoid overly complex nested conditions as it will reduce the readability of the code.If the logic becomes very complex, consider whether it is possible to preprocess the data at the controller layer or break it down into smaller logic blocks.
  • Note whitespace characters:The template tags themselves may introduce unnecessary outputs: