How to implement conditional judgment in AnQiCMS template to control the display of content?

In AnQi CMS template design, flexibly controlling the display of content is the key to building dynamic, responsive websites.Whether it is to display different information based on the page type, data status, or specific conditions, conditional judgment is an indispensable tool.AnQiCMS's template engine provides an intuitive and powerful conditional judgment mechanism, allowing you to easily implement these complex logic.

Basic syntax of conditional judgment

AnQiCMS template condition judgment is similar to many mainstream template engines, using{% if ... %}A tag structure. It allows you to decide whether a part of the template should be rendered based on the truth or falsity of one or more conditions.

The basic conditional judgment structure is as follows:

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

If you need to handle more complex logic, you can introduce{% elif ... %}(else if) and{% else %}(otherwise) tags to handle multiple mutually exclusive conditions:

{% if 条件一 %}
    <!-- 当条件一为真时显示的内容 -->
{% elif 条件二 %}
    <!-- 当条件一为假且条件二为真时显示的内容 -->
{% else %}
    <!-- 当所有条件都为假时显示的内容 -->
{% endif %}

InifInside the tag, you can perform various comparisons and logical operations.For example, determine if a variable exists, whether it is equal to a specific value, whether it contains a certain keyword, or combine multiple conditions.

Common conditional judgment scenarios and practices

In the operation of actual websites, the application scenarios of conditional judgment are very extensive. The following lists some common usage scenarios and implementation methods:

  1. Determine if a variable exists or has a valueThis is one of the most common uses, especially when displaying images, descriptions, or custom fields.If a data field may be empty, by checking whether it exists or has a value, you can avoid the page from displaying incomplete or erroneous information.

    {% if archive %} {# 判断整个文档对象是否存在 #}
        <h1>{{ archive.Title }}</h1>
        {% if archive.Thumb %} {# 判断文档缩略图是否存在 #}
            <img src="{{ archive.Thumb }}" alt="{{ archive.Title }}">
        {% else %}
            <img src="/static/images/default-thumb.jpg" alt="默认缩略图">
        {% endif %}
    {% else %}
        <p>抱歉,您要查找的文档不存在。</p>
    {% endif %}
    

    For list data, there is also a special optimization method for determining if the list is empty. Inforthe loop, you can use{% empty %}tags to handle the case where the list is empty, which is better than writing one separately.ifJudgment is more concise:

    {% for item in archives %}
        <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
    {% empty %}
        <li>目前还没有任何文章。</li>
    {% endfor %}
    
  2. Comparison of numbers or stringsWhen you need to display content based on a number (such as ID, views) or a string (such as title, status), you can directly compare:

    {# 判断当前文档ID是否为特定值 #}
    {% if archive.Id == 10 %}
        <p>这是一篇非常重要的精选文章!</p>
    {% endif %}
    
    
    {# 根据系统设置决定显示内容 #}
    {% if system.SiteCloseTips %}
        <div class="site-closed-message">
            <p>{{ system.SiteCloseTips }}</p>
        </div>
    {% endif %}
    

    The AnQiCMS template supports various comparison operators, such as equal==, not equal to!=, greater than>, less than<and greater than or equal to>=and less than or equal to<=.

  3. Combination of logical operatorsBy logical operators, you can combine multiple conditions to achieve more complex judgment logic:

    • &&orand: Logical 'AND', true only when all conditions are true.
    • ||oror: Logical 'OR', true if any condition is true.
    • !ornot: Logical 'NOT', the negation.
    • in: Check if a value exists in a list or string.

    For example, the navigation menu is highlighted only when it is on the current page and has a submenu:

    {% if item.IsCurrent && item.NavList %}
        <li class="active has-submenu">
            <a href="{{ item.Link }}">{{ item.Title }}</a>
            <!-- 显示子菜单 -->
        </li>
    {% else %}
        <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
    {% endif %}
    

    Another example, to determine if the article title contains a specific keyword:

    {% if archive.Title|contain:"限时优惠" %}
        <span class="badge sale-tag">限时优惠中!</span>
    {% endif %}
    
  4. Combine the filter for more advanced judgmentFilters can process variables before they are used for conditional judgment or display, which greatly expands the ability of conditional judgment.

    • |contain: "关键词": Determine if a string or array contains a specific keyword.
    • |length: Get the length of a string, array, or key-value pair, commonly used to judge the amount of content.
    • |yesno:"真值,假值,未知值": Map boolean values or variables that can be evaluated as boolean values to a custom string representation.

    For example, determine if there are comments in the comment list and display the number of comments:

    {% commentList comments with archiveId=archive.Id type="list" %}
        {% if comments|length > 0 %}
            <p>共有 {{ comments|length }} 条评论。</p>
            {% for comment in comments %}
                <!-- 显示评论详情 -->
            {% endfor %}
        {% else %}
            <p>还没有评论,快来发表您的看法吧!</p>
        {% endif %}
    {% endcommentList %}
    

    Determine if a status value is 'Published':

    {% if archive.Status|yesno:"已发布,草稿,待审核" == "已发布" %}
        <span class="status-published">已发布</span>
    {% endif %}
    
  5. Determine the state of the current page.The AnQiCMS template automatically identifies the type of the current page (document detail page, category list page, single page, etc.), and you can use this contextual information for conditional judgments. For example, only show related recommendations on the document detail page:

    {# 假设`archive`变量只在详情页存在 #}
    {% if archive %}
        <h3>相关推荐</h3>
        {% archiveList relatedArchives with type="related" limit="5" %}
            <ul>
            {% for item in relatedArchives %}
                <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
            {% endfor %}
            </ul>
        {% endarchiveList %}
    {% endif %}
    

Points to note

  • grammatical accuracy:if/elif/else/endiftags must be paired and the syntax must be precise, including%symbols and spaces.
  • variable names and case sensitivityThe variable names of AnQiCMS templates are usually camel case and case sensitive. Please refer to the tag document to ensure the spelling of variable names is correct.
  • Content escaping: AnQiCMS template defaults to escaping output variables to prevent XSS attacks. If you are sure that the output content is safe HTML code (such as rich text editor content), please use|safea filter such as{{ archive.Content|safe }}.
  • Testing and debuggingIn practical applications, be sure to thoroughly test various combinations of conditions to ensure that the content is displayed as expected.

By proficiently using these conditional judgment techniques, you will be able to fully utilize the potential of the AnQiCMS template to provide users with a more intelligent and personalized website experience.


Frequently Asked Questions (FAQ)

Q1: How do I determine if the current page is a specific document detail page or a category list page?

**A