How to display or hide content blocks in AnQiCMS templates based on conditions (such as `if` statements)?

Calendar 👁️ 70

In website content operation, we often need to display or hide specific content based on different situations, such as displaying special event information on holidays or showing different operation buttons based on the user's status. AnQiCMS provides a flexible template engine that allows you to easily implement these dynamic content controls, with the most core tool being the conditional judgment statement -if.

The AnQiCMS template syntax is similar to the popular Django template engine, which is very easy to learn. By usingifThe statement, you can decide in the template which content should be displayed to the user based on various conditions, and which should not be shown, thus providing a more accurate and personalized browsing experience for website visitors.

MasterifBasic structure of the statement

In the AnQiCMS template,ifThe basic structure of the statement is similar to the programming language we are familiar with, it always starts with{% if 条件 %}and ends with{% endif %}end.

1. The simplest conditional judgment:{% if 条件 %} ... {% endif %}

When you only need to display content when a specific condition is true (True), you can use this form:

{% if system.SiteCloseTips %}
    <div class="site-alert">
        网站维护中,请稍后再访问:{{ system.SiteCloseTips }}
    </div>
{% endif %}

In this example, if the background is setSiteCloseTips(Website closed prompt content), this prompt information will be displayed ifSiteCloseTipsis empty or not set, the content block will not be displayed.

2. Include an alternative solution:{% if 条件 %} ... {% else %} ... {% endif %}

When you want to display a piece of content when a condition is true and another piece of content when the condition is not true,elsethe statement comes into play:

{% if user.IsLoggedIn %}
    <p>您好,{{ user.UserName }}!欢迎回来。</p>
{% else %}
    <p>您尚未登录,请<a href="/login">登录</a>或<a href="/register">注册</a>。</p>
{% endif %}

Here, different greetings or guides are displayed according to whether the user is logged in or not.

3. Multi-condition judgment:{% if 条件1 %} ... {% elif 条件2 %} ... {% else %} ... {% endif %}

Faced with more complex logic, it may be necessary to check multiple different conditions.elif(Else if abbreviation) Allows you to chain more conditions:

{% if archive.ReadLevel > 5 %}
    <div class="vip-exclusive">
        此内容为高级会员专属,请升级您的会员等级。
    </div>
{% elif archive.ReadLevel > 0 %}
    <div class="member-only">
        此内容需登录后查看。
    </div>
{% else %}
    <div class="free-content">
        免费内容,欢迎阅读!
    </div>
{% endif %}

This example displays different prompts to users based on the reading level of the document.

Apply various conditional judgment expressions flexibly

ifThe "condition" can be of various types, it can be the value of a variable, the result of a comparison between variables, or even more complex logical combinations.

  • Equality and inequality judgments:==and!=You can check if a variable's value is equal to (==) or not equal to (!=) another value.

    {% if archive.CategoryId == 10 %}
        <p>这是关于“最新资讯”分类的文章。</p>
    {% endif %}
    
    {% if system.Language != "zh-CN" %}
        <p>This content is not in Simplified Chinese.</p>
    {% endif %}
    
  • Comparison of size:>,<,>=,<=Commonly used to compare the size of numbers, such as judging inventory, price, or reading volume.

    {% if product.Stock <= 0 %}
        <span class="sold-out">已售罄</span>
    {% elif product.Stock < 5 %}
        <span class="low-stock">仅剩少量,欲购从速!</span>
    {% else %}
        <span class="in-stock">有货</span>
    {% endif %}
    
  • Logical combination:and,or,notYou can useand(And),or(or) as wellnot(not) to build more complex logic.

    {% if user.IsLoggedIn and user.IsAdmin %}
        <a href="/system/">进入后台管理</a>
    {% endif %}
    
    {% if archive.Flag contains "h" or archive.Flag contains "f" %}
        <span class="hot-feature">热门推荐/幻灯片内容</span>
    {% endif %}
    
    {% if not archive.Thumb %}
        <p>此文章暂无缩略图。</p>
    {% endif %}
    

    It is worth mentioning,archive.Flag contains "h"This usage can be used to check if a document's recommended properties contain specific tags such ashrepresenting headlines).

  • Check if the variable exists or is a 'true' valueIn the AnQiCMS template, the following situations will be considered 'false' (False):

    • nil(Empty value)
    • false(Boolean false)
    • Number0
    • empty string""
    • An empty array (slice) or key-value pair Most other cases are considered 'true' (True).This allows you to succinctly determine whether a variable has actual content.
    {% if item.Thumb %}
        <img src="{{ item.Thumb }}" alt="{{ item.Title }}">
    {% else %}
        <img src="/static/images/default_thumb.png" alt="无图">
    {% endif %}
    
    {% if archive.Description %}
        <p class="description">{{ archive.Description }}</p>
    {% endif %}
    

    This code will checkitem.ThumbWhether there is a value (i.e., whether the image path exists), as well asarchive.DescriptionWhether there is content, thus determining whether to display the corresponding image or description.

  • Check the members of the set:inIf you need to determine whether a value exists in a list or string, you can useinoperator.

    {% set allowed_users = ["admin", "editor", "moderator"] %}
    {% if user.UserName in allowed_users %}
        <p>欢迎内容管理团队成员!</p>
    {% endif %}
    

tips for writing neat templates

While usingifWhen writing statements, there are several tips that can make your template code cleaner and more readable:

  • Control whitespace:Sometimes, `

Related articles

How to get and display detailed information of a specific user, such as username, avatar, and level in AnQiCMS template?

In website operation, displaying personalized information to users, such as usernames, avatars, and membership levels, can greatly enhance user experience and website interaction.AnQiCMS as a flexible content management system provides intuitive template tags to help us easily achieve this goal.The AnQiCMS template system has adopted a syntax similar to Django templates, allowing backend data to be called through concise tags.When we want to display detailed information about a specific user

2025-11-07

How to loop to display user group information in AnQiCMS template?

In the website built with AnQiCMS, the user grouping function provides us with powerful content management and user permission division capabilities, whether it is to implement membership content, display different information according to user level, or plan VIP services, user grouping is an indispensable foundation.How to flexibly display these user grouping information on the front-end template of a website, which is a focus for many operators.AnQiCMS uses a template engine syntax similar to Django, which makes template writing intuitive and efficient.When it comes to user group information, the system provides

2025-11-07

How to customize the display content and link of the homepage Banner in AnQiCMS template?

In website operation, the homepage banner acts as the 'facade' of the website, and its visual effect and guiding role are crucial.A well-designed and tasteful Banner can not only attract visitors' attention but also effectively convey the core information of the website and guide users to take the next step.For friends using AnQiCMS, how to flexibly customize the display content and link of the home page Banner is a key step to improve the user experience and marketing effect of the website.

2025-11-07

How to implement the conversion of the first letter to uppercase or all uppercase of the string in AnQiCMS template?

In website content presentation, the case format of strings often needs to be flexibly adjusted according to different scenarios, such as the first letter of the article title in uppercase, the username in all lowercase, or the brand name in all uppercase.AnQiCMS as a feature-rich enterprise-level content management system, its template engine provides convenient and powerful filters (Filters), which help us easily implement the case conversion of these strings, making content display more standardized and professional.

2025-11-07

How to iterate over an array or object list in AnQiCMS template and display loop count and remaining quantity?

In website content management, dynamically displaying list data is a basic and crucial function.Whether it is an article list, product display, or user comments, we need to present the data efficiently to the users.How to clearly mark the order of list items or inform the user how much content is left to browse can greatly enhance the user experience and readability of the content.AnQiCMS (AnQiCMS) with its flexible template engine, can help us easily meet these needs.

2025-11-07

How to define and use temporary variables in AnQiCMS templates to simplify complex data processing?

In AnQiCMS template development, we often encounter situations where we need to process or reuse complex data.Writing complex logic or data paths repeatedly in templates not only makes the code long and hard to read, but also affects maintenance efficiency.At this time, skillfully using temporary variables can make template code clearer and more concise, as well as more flexible in data processing.AnQiCMS's template engine provides a powerful and flexible variable definition mechanism that can help us effectively manage and process page data.

2025-11-07

How does AnQiCMS template reference other template files (such as `header.html`, `footer.html`) to achieve code reuse?

In website construction and maintenance, how to efficiently manage code and avoid redundant labor is the key to improving development efficiency and ensuring website consistency.For users who build websites using AnQiCMS, its flexible template engine provides a powerful code reuse mechanism, allowing us to easily refer to other template files, such as the common header (header) and footer (footer), thereby achieving modular development.The template design of AnQiCMS borrows the syntax features of the Django template engine

2025-11-07

How to display Markdown formatted article content in AnQiCMS template and render it correctly to HTML?

In a content management system, Markdown has become the preferred writing format for many content creators, it is concise, efficient, and easy to convert to structured HTML.For AnQiCMS users, using Markdown to write articles can not only improve writing efficiency but also better organize content structure.Luckyly, AnQiCMS deeply supports Markdown formatted article content and provides a perfect mechanism to ensure that they are correctly rendered into beautiful HTML pages on the website front-end.### Start

2025-11-07