How to dynamically display different content blocks based on conditions (if tag)?

Calendar 👁️ 60

In Anqi CMS, the dynamic display of website content is the key to improving user experience and operational efficiency.You often encounter such needs: you want a certain content block to appear only under specific conditions, or to display different information for different situations.This is the powerful conditional judgment feature of Anqi CMS--ifTags can come in handy.

The Anqi CMS template system adopts syntax similar to the Django template engine, allowing you to implement logical judgments in templates in a straightforward manner. By using it flexibly,ifLabel, you can easily control the display and hiding of page elements, making the website content appear more intelligent and personalized than ever before.

Get to knowifBasic usage of tags

ifThe core idea of the tag is that if a condition is true (True), execute the code block it wraps. Anqicms provides several commonifLabel structure to meet your different judgment needs:

  1. Simpleifthe judgment.This is the most basic usage. It can be used when you only need to display content under a single condition, or to check if a variable exists.

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

    For example, if you only want to display the image when the article has a thumbnail:

    {% if archive.Thumb %}
        <img src="{{ archive.Thumb }}" alt="{{ archive.Title }}" />
    {% endif %}
    
  2. if-elsethe judgment.When you need to choose between two contents based on conditions:if-elsethe structure is very practical.

    {% if 条件 %}
        <!-- 当条件为真时显示 -->
    {% else %}
        <!-- 当条件为假时显示 -->
    {% endif %}
    

    For example, if the article has a description, display the description, otherwise display a default prompt:

    {% if archive.Description %}
        <p>{{ archive.Description }}</p>
    {% else %}
        <p>暂无内容简介。</p>
    {% endif %}
    
  3. if-elif-elseMultiple judgmentsWhen you need to handle multiple mutually exclusive conditions,if-elif-elsethe structure can make your logic clearer.elifIs the abbreviation of "else if".

    {% if 条件1 %}
        <!-- 当条件1为真时显示 -->
    {% elif 条件2 %}
        <!-- 当条件1为假,且条件2为真时显示 -->
    {% else %}
        <!-- 所有条件都为假时显示 -->
    {% endif %}
    

    For example, show different hot states according to the number of page views of the article:

    {% if archive.Views > 1000 %}
        <span>🔥 超级热门!</span>
    {% elif archive.Views > 100 %}
        <span>👍 比较热门</span>
    {% else %}
        <span>✨ 持续更新中</span>
    {% endif %}
    

    No matter which form is used, it must end with{% endif %}End the entire conditional judgment block.

Common conditional expressions

InifIn the tag, you can combine various expressions to build complex judgment logic.

  • Does the variable exist/Is it true (Truthiness)In AnQi CMS template, any non-empty string, non-zero number, non-empty object, or array is considered 'true'. While empty string, zero,nil(null) or boolean valuefalseIt is considered "false".

    {% if system.SiteLogo %}
        <img src="{{ system.SiteLogo }}" alt="{{ system.SiteName }}" />
    {% else %}
        <p>网站Logo缺失</p>
    {% endif %}
    
  • Comparison of numbers with stringsYou can compare numbers (such as ID, views, price) with strings.

    • Equal/Not equal: ==(equals,)!=(Not equal)
    • Comparison of size: >(greater than,)<(less than,)>=(greater than or equal to,)<=(less than or equal to)
    {% if archive.CategoryId == 5 %}
        <!-- 这是ID为5的分类下的文章 -->
    {% endif %}
    
    {% if contact.Cellphone != "" %}
        <a href="tel:{{ contact.Cellphone }}">{{ contact.Cellphone }}</a>
    {% endif %}
    
  • Logical operatorWhen you need to combine multiple conditions, you can useand(Logical AND),or(Logical OR),not(Logical NOT, can also be used!)

    {% if archive.Thumb and archive.CreatedTime %}
        <p>文章带图且有发布时间</p>
    {% endif %}
    
    {% if archive.ModuleId == 1 or archive.ModuleId == 2 %}
        <p>这篇文章属于文章或产品模型</p>
    {% endif %}
    
    {% if not archive.Description %}
        <p>文章没有简介</p>
    {% endif %}
    
  • List or set judgmentYou can useinOperator to determine if a value is in the list.

    {% set allowed_ids = "1,3,5"|split:"," %} {# 假设这是一个ID列表 #}
    {% if archive.Id in allowed_ids %}
        <p>这是被允许展示的文章</p>
    {% endif %}
    

Combine the actual application scenarios of AnQi CMS tags

Now, let's see how to integrate tags into daily template development in AnQi CMSifApply tags to specific scenarios.

  1. The active state in the navigation menuThe website navigation is the part where users interact the most. Adding a highlight style to the navigation item corresponding to the current page is a common practice to enhance user experience.
    
    {% navList navs %}
        <ul>
            {% for item in navs %}
                <li {% if item.IsCurrent %}class="active"{% endif %}>
                    <a href="{{ item.Link }}">{{ item.Title }}</a>
                    {% if item.NavList %} {# 判断是否有子导航 #}
                        <ul class="sub-menu">
                            {% for sub_item in item.NavList %}
                                <li {% if sub_item.IsCurrent %}class="active"{% endif %}>
                                    <a href="{{ sub_item.Link }}">{{ sub_item.Title }}</a>
                                </li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                </li>
            {% endfor %}
        </ul>
    {% endnavList %}
    
    here,item.IsCurrentIt is a boolean value used to determine whether the current navigation item corresponds to the current one

Related articles

How to display the background custom banner (Banner) image list in the template?

The banner image on the website is an important element to attract visitors' attention and display core information.In Anqi CMS, you can flexibly customize these banner images in the background and elegantly present them in various template positions on the website.Below, we will discuss in detail how to display the custom banner image list in the template, making your website more attractive. ### Configuration and Management of Background Banner Images In the Anqi CMS backend, the management of banner images is mainly divided into two cases: global homepage banners and specific content banners. 1.

2025-11-08

How to convert image addresses to thumbnail format for display?

In website operation, images play a crucial role.They can not only attract users' attention but also effectively convey information.However, very large images can severely affect website loading speed, thereby damaging user experience and search engine rankings.Therefore, converting the image address to a thumbnail format for display has become an indispensable part of optimizing website performance.AnQiCMS provides very convenient features in this aspect, it can automatically generate and manage thumbnail images for your website, and also provides flexible calling methods to help you easily meet the challenges of image optimization.### AnQiCMS

2025-11-08

How to truncate long text content and add an ellipsis at the end to optimize list display?

In website operation, especially managing a content-rich platform, the display effect of the list page is crucial for user experience.Whether it is an article list, product overview, or news update, we hope the page is tidy and the information is clear at a glance.However, when the document content or description is too long, directly displaying it in the list often leads to layout confusion, affecting the overall aesthetics and information acquisition efficiency.At this moment, effectively truncating text and adding an ellipsis is particularly important.AnQiCMS (AnQiCMS) provides a powerful and flexible template system, in which built-in text filters can help us easily solve this problem

2025-11-08

How to safely output HTML tags in website content to avoid escaping?

In website content management, especially when content includes rich formats or user input, how to safely output HTML tags is a crucial issue.AnQiCMS (AnQiCMS) fully considered this from the beginning, its template engine defaults to escaping output variables to effectively prevent cross-site scripting attacks (XSS) and other security risks.### Understanding the Default Behavior of Template Engines The Anqi CMS template engine follows the syntax of mainstream frameworks like Django, one of its core concepts being 'Security First'

2025-11-08

How to use a loop (for label) to traverse and display a content list?

In Anqi CMS, whether it is displaying article lists, product catalogs, navigation menus, or friend links, we will frequently deal with content lists.To dynamically display this content on the website front-end, you can't do without powerful looping functions.Today, let's delve deeper into how to use the loop (`for` tag) in Anqi CMS templates to traverse and display your content list. ### The core syntax of the `for` tag: Bring content to life The Anqi CMS template syntax is similar to Django and Blade, very intuitive.When you need to handle a set of data

2025-11-08

How to format a timestamp into a readable date and time string for display?

In Anqi CMS, formatting timestamps into readable dates: A practical guide When managing website content, we often encounter the need to display article publishing time, update time, and user registration time information.These data are usually stored in the database in the form of a string of numbers - that is, a 'timestamp'.Although computers can easily understand these numbers, they may not seem so friendly to users visiting websites.For example, seeing a number like '1678886400' is not as intuitive as '2023 March 15 10:00:00'

2025-11-08

How to define template variables and reuse them in content display?

In AnQi CMS, managing and displaying website content, flexibly using template variables is undoubtedly the key to improving efficiency and maintaining consistency.Whether it is necessary to unify the information of the entire site or to customize unique attributes for specific content, understanding how to define and reuse template variables can make content operation more skillful. ### From the background source to define variables In AnQi CMS, many of the information we see on the website every day is actually managed through backend predefined variables.This means you don't have to touch a single line of code to easily define and modify these contents. First

2025-11-08

How does Anqi CMS manage and display uploaded images and video resources?

When using Anqi CMS to manage website content, images and video resources are undoubtedly the key to enriching the page and improving the user experience.AnQi CMS provides a set of intuitive and powerful functions to help us efficiently upload, manage, and display these multimedia contents. ### Easy upload and centralized management of resources When creating articles, products, or single-page content, images and videos can be directly inserted into the content editing area.This process is very smooth, just like using a document editor in everyday life.In addition to this immediate insertion method, Anqi CMS also has a dedicated "image resource management" area

2025-11-08