How to use the 'if logical judgment tag' in AnQiCMS template to control the conditional display of content?

Adopt flexible use of the 'if' tag in AnQiCMS templates to accurately present content

In website content management, we often need to display different content based on different conditions, such as displaying a prompt in specific situations, displaying different information based on user identity, or only rendering a specific block when certain data exists.The template system of AnQiCMS (AnQiCMS) provides us with a powerful and easy-to-use 'if logic judgment tag' that helps us easily achieve these dynamic content display requirements.

AnQiCMS's template syntax borrows the style of the Django template engine, which is very easy to get started with for users familiar with this kind of syntax. It uses single curly braces and the percent sign ({% ... %}To define logical control labels, while double curly braces ( are used to output variable content. Mastered{{ ... }}Tags, you can build highly flexible conditional logic in templates.ifTags, you can build highly flexible conditional logic in templates.

Understand the basic structure of the 'if' logical judgment tag

ifThe core function of the tag is to decide whether to render its internal content based on the provided conditions. It supports various forms to deal with different complexity judgment scenarios:

  • Basic condition judgment:When only one condition needs to be judged
    
    {% if 条件 %}
        <!-- 当条件为真时显示的内容 -->
    {% endif %}
    
  • Multiple condition judgment:When it is necessary to judge multiple exclusive conditions in order
    
    {% if 条件一 %}
        <!-- 当条件一为真时显示的内容 -->
    {% elif 条件二 %}
        <!-- 当条件二为真时显示的内容 -->
    {% else %}
        <!-- 以上条件都不为真时显示的内容 -->
    {% endif %}
    
    Please note, alliftags must be with{% endif %}As an end tag, make sure the tags are paired and form a complete logical block.

Build a judgment condition: flexible and diverse combination methods

ifThe strength of tags lies in the flexibility of their conditional expressions.You can use various comparison operators, logical operators, and built-in variables and filters of AnQiCMS to construct complex judgment conditions.

  1. Comparison operators: Used to compare the size of two values or whether they are equal.

    • ==(Equal)
    • !=(Not equal)
    • >(Greater than)
    • <(Less than)
    • >=(greater than or equal to)
    • <=(less than or equal to)

    For example, to determine if the current document ID is 10:

    {% if archive.Id == 10 %}
        这是ID为10的特别文档。
    {% endif %}
    

    Or determine if the page views exceed 100:

    {% if archive.Views > 100 %}
        这篇文章很受欢迎!
    {% endif %}
    
  2. Logical operators:Used to combine multiple conditions or negate conditions.

    • and(Logical AND)
    • or(Logical OR)
    • not(Logical NOT)

    For example, when the article ID is 10andShow when the view count is greater than 100:

    {% if archive.Id == 10 and archive.Views > 100 %}
        ID为10的爆款文章!
    {% endif %}
    

    When a variablesimpleShow when it is not empty:

    {% if not simple %}
        这是一个空对象。
    {% else %}
        对象`simple`存在。
    {% endif %}
    
  3. Variable's true or false judgment:In the AnQiCMS template, many variables can be used directly asifJudgment under conditions.

    • non-zero numbers are considered astrue, zero is considered asfalse.
    • non-empty strings are considered astrue, empty strings are considered asfalse.
    • Non-empty array/slice/map is consideredtrue, empty array/slice/map is consideredfalse.
    • BooleantrueandfalseTake effect directly.
    • nil(Empty value) is consideredfalse.

    For example, check if the document has a thumbnail:

    {% if archive.Thumb %}
        <img src="{{ archive.Thumb }}" alt="{{ archive.Title }}">
    {% else %}
        <img src="{% system with name='SiteLogo' %}" alt="默认图片">
    {% endif %}
    
  4. Combine filters (Filters) for conditional judgment:AnQiCMS provides a rich set of filters that can process data and return results forifjudgment.

    • lengthFilter: Get the length of a string, array, or Map.
      
      {% if tags|length > 0 %}
          <p>相关标签:</p>
          {% for tag in tags %}<a href="{{ tag.Link }}">{{ tag.Title }}</a>{% endfor %}
      {% endif %}
      
    • containFilter: Determine if a string or array contains specific content, returning a boolean value.
      
      {% if system.SiteName|contain:"AnQiCMS" %}
          我们的网站名称包含“AnQiCMS”。
      {% endif %}
      
    • divisiblebyFilter: Determines if one number can be evenly divided by another, returning a boolean value.
      
      {% if forloop.Counter|divisibleby:2 %}
          <li class="even-item">...</li>
      {% else %}
          <li class="odd-item">...</li>
      {% endif %}
      

Application: Enhances template flexibility and user experience.

Here are some applications in the AnQiCMS templateifCommon scenarios of tags:

  • Custom navigation menu:Add navigation menu items based on the current page stateactiveClass.

    {% navList navs %}
        <ul>
            {% for item in navs %}
                <li {% if item.IsCurrent %}class="active"{% endif %}>
                    <a href="{{ item.Link }}">{{ item.Title }}</a>
                </li>
            {% endfor %}
        </ul>
    {% endnavList %}
    
  • Dynamic display/hide content block:For example, display a special promotion area only on specific category pages.

    {% categoryDetail categoryInfo with name="Id" %} {# 获取当前分类ID #}
    {% if categoryInfo == 5 %} {# 假设分类ID为5是“专题活动” #}
        <div class="promo-section">
            <p>参与我们的最新专题活动,赢取丰厚奖品!</p>
        </div>
    {% endif %}
    
  • Handle empty data in the list:When the list is empty, display a friendly prompt message instead of a blank page. (This can also be achieved throughforrepeatedlyemptytags, butifit also applies accordingly)

    {% archiveList archives with type="page" limit="10" %}
        {% if archives %}
            <ul>
                {% for item in archives %}
                    <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
                {% endfor %}
            </ul>
        {% else %}
            <p>目前还没有文章发布,敬请期待!</p>
        {% endif %}
    {% endarchiveList %}
    
  • Control element properties:For example, based on whether the link needsnofollowproperties to dynamically add.

    {% linkList friendLinks %}
        {% for item in friendLinks %}
            <a href="{{item.Link}}" {% if item.Nofollow == 1 %} rel="nofollow"{% endif %} target="_blank">{{item.Title}}</a>
        {% endfor %}
    {% endlinkList %}
    

From the above examples, we can seeifThe powerful role of tags in AnQiCMS templates.It not only helps us achieve personalized content display, but also greatly improves the flexibility and reusability of templates, making the website content present more intelligently and humanly.

Summary

AnQiCMS'ifLogical conditional tags are an indispensable tool for building dynamic, responsive website content. It provides