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

Calendar 👁️ 75

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

Related articles

How to use the 'loop iteration tag (for)' to display a dynamic data list in the template in AnQiCMS?

The website content needs to be continuously updated, such as displaying the latest articles, products, user comments, or navigation menus, and this dynamic data is often presented in the form of lists.In AnQi CMS, by using its powerful template engine and the intuitive 'loop traversal tag (for)', we can easily display these background data on the website front-end, making your website vibrant.The AnQi CMS template engine draws on the simplicity and efficiency of Django template syntax, making it easy for even developers who are new to the field to get started quickly. Among them

2025-11-08

How to safely output rich text content in AnQiCMS templates and avoid XSS attacks?

When building and operating a website, content display is undoubtedly the core link.Especially when displaying rich text content that includes images, links, bold, italic, and other formats, how to ensure that the page is beautiful while also effectively resisting potential security threats is a problem worth in-depth discussion.AnQiCMS (AnQiCMS) is a content management system that emphasizes security and efficiency, providing clear and strong mechanisms in this aspect.

2025-11-08

How to display custom contact information on AnQiCMS (such as WhatsApp, Facebook links)?

In today's digital age, a website is not just a platform for displaying information, but also an important bridge for businesses to connect with customers.Provide a variety of contact methods, such as WhatsApp, Facebook links, which can greatly enhance user experience and conversion efficiency.AnQiCMS as a powerful content management system provides a flexible and convenient solution in this regard.This article will introduce in detail how to set and display custom contact information in AnQiCMS, ensuring that your website can communicate with potential customers in the most convenient way.

2025-11-08

How to control the automatic compression of large images and the generation of thumbnails in AnQiCMS?

In website operation, images play a crucial role.They are not only elements that attract users' attention, but also directly affect the website's loading speed, user experience, and even the performance of search engine optimization (SEO).Managing website images, especially the compression of large-sized images and the generation of thumbnails, is a key link in improving website performance and aesthetics.AnQiCMS (AnQiCMS) fully understands this and provides flexible and powerful image processing features in its content management system, allowing you to easily control the automatic compression of large images and the generation of thumbnails.

2025-11-08

How to customize the navigation menu in AnQiCMS and implement multi-level dropdown display on the front end?

In website operation, a clear and intuitive navigation menu is the core of user experience, as well as the key to content organization and promotion.AnQiCMS (AnQiCMS) fully understands this point, providing you with a flexible and powerful navigation menu customization feature, and supporting multi-level dropdown display on the front end to make your website structure clearer and content access more convenient.Next, we will together learn how to easily set up and apply these navigation in AnQiCMS.

2025-11-08

How to display the number of views and comments for articles in AnQiCMS?

In content operation, the number of page views and comments on articles is an important indicator of the popularity and user engagement of the content.They not only provide readers with references, but also help operators to understand content performance.AnQiCMS as a powerful content management system naturally also provides a convenient way to display these real-time data, making your website content more interactive and transparent.In AnQiCMS, whether it is articles, products, or other content types, they are all uniformly referred to as "documents" (archive)

2025-11-08

How to use Tag labels in AnQiCMS to associate and display related documents on the front end?

AnQiCMS provides powerful classification functions to organize the website structure, and also opens up new ways for deep association and multi-dimensional display of content through flexible Tag label mechanisms.Imagine that your website is like a library, with different shelves for categories, and Tag tags are like different subject keywords on books, allowing readers to find books with similar themes scattered on different shelves according to their interests.Next, let's delve into how AnQiCMS uses Tag labels to make your website content more intelligent and rich in front-end display

2025-11-08

How to set the website to "closed" status and display a custom prompt message in AnQiCMS?

During the operation of the website, it is sometimes necessary to temporarily close the website, whether it is for system upgrades, data maintenance, or a brief adjustment of content, AnQiCMS provides a simple and efficient shutdown feature to help us elegantly manage the temporary shutdown status of the website.This not only avoids the user from seeing an error page and affecting the experience, but also clearly conveys information and maintains the brand image. We will learn in detail how to set the website to "closed" status in AnQiCMS and display personalized prompt information.

2025-11-08