How to display different content blocks based on conditions in the AnQi CMS template?

Calendar 👁️ 54

In AnQi CMS template design, displaying or hiding content blocks based on different conditions is a key ability to achieve dynamic website effects and improve user experience.By using the built-in template syntax and rich tag features, we can easily build flexible and versatile pages that respond to different situations.

The Anqi CMS template engine uses a tagging method similar to Django syntax, which makes conditional judgments and loop controls very intuitive and easy to learn.It allows us to directly access various data in the template and make logical judgments to determine what content should be presented to the visitor.


1. Core Mechanism: Conditional Judgment Tags (if/elif/else)

The core of implementing conditional judgment in the AnQi CMS template isifLabel. Its basic syntax is similar to that of other common template engines, through{% if 条件 %}/{% elif 其他条件 %}and{% else %}to build logical branches, and with{% endif %}end.

Basic syntax structure:

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

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

In this case, “condition” can be in many forms:

  • Comparison operation:For example, determine if a number is equal to, not equal to, greater than, less than, or between a certain range.
    • archive.Id == 10(Is the document ID equal to 10)
    • item.Views > 1000(Is the article view count greater than 1000?)
    • item.Price < 50(Is the product price less than 50?)
  • Boolean value judgment:Check if the variable itself istrueorfalse.
    • item.HasChildren(Does the category have subcategories, if true, then execute)
    • system.SiteCloseStatus(Is the website closed?)
  • Logical combination:Useand(And),or(Or),notNot to combine multiple conditions.
    • item.IsCurrent and item.HasChildrenBoth the current item and the subcategory.
    • not item.ThumbNo thumbnail.
  • Null or non-null judgment:Check if a variable has content.
    • {% if item.Title %}(Title must not be empty if displayed)

II. Data Source: How to obtain variables for judgment?

We first need to obtain the data available for judgment in order to make a conditional judgment.AnQi CMS provides a rich set of tags to obtain various information about the website and use this information as variables directly in the template.

  1. System Global Settings(systemtags)By{% system with name="字段名称" %}Can obtain the global configuration of the website, such as the name of the website, Logo, filing number, and even custom system parameters.

    • Example: {% system with name="SiteCloseStatus" %}Can retrieve the site's shutdown status (usually a boolean value or a string) to determine whether to display the shutdown notice.
  2. Categories, documents, single page details (categoryDetail,archiveDetail,pageDetailtags)On category list pages, document detail pages, single pages, and other specific pages, the system will automatically load the main information of the current page into the context, which can be accessed directly througharchive.Title/category.DescriptionAccess in this way. If you need to get details of other specified IDs or get detailed field information, you can usecategoryDetail/archiveDetail/pageDetail.

    • Example:You can directly use it on the article detail page{{archive.Title}}Get the title. If you want to determine whether the current article has a thumbnail, you can use{% if archive.Thumb %}.
  3. list data (archiveList,categoryList,navListtags combinedforloop)When we usearchiveList/categoryList/navListwhile traversing the list with these tags, in{% for item in items %}inside the loop,itemThe variable contains all the data of the current loop item. This data is the most commonly used source for conditional judgment.

    • Example:when classifying a list by loop,item.HasChildrenCan determine if the category has subcategories;item.IsCurrentCan determine if it is the currently active category, thereby adding different styles.
  4. Custom fieldThe Anqi CMS supports adding custom fields for content models, categories, etc. The data of these custom fields can also be directly used for conditional judgments in templates.

    • Example:If you add a custom field namedVIPAccessto the article model, you can use it on the article detail page to determine if it is only accessible to VIP users.{% if archive.VIPAccess == 'true' %}.

III. Practical Scenarios and Examples

Combine condition judgment tags and data sources, we can achieve various flexible content display.

1. Display different content based on the website status.Assuming your website needs to be temporarily closed for maintenance, you can judge in the templateSiteCloseStatusvariable to display maintenance tips instead of normal content.

{% system siteStatus with name="SiteCloseStatus" %}
{% if siteStatus == "true" %} {# 假设后台设置为字符串"true"或"false" #}
    <div class="maintenance-message">
        <h1>网站维护中</h1>
        <p>抱歉,网站正在进行系统升级,请稍后访问。给您带来的不便敬请谅解!</p>
        {% system closeTips with name="SiteCloseTips" %}
        {% if closeTips %}<p>{{ closeTips }}</p>{% endif %}
    </div>
{% else %}
    <!-- 正常网站内容 -->
    {% include "partial/header.html" %}
    <main>
        <!-- 页面主体内容 -->
    </main>
    {% include "partial/footer.html" %}
{% endif %}

2. Highlight the current active item in the navigation menu and display the submenuWhen building a navigation menu, it is often necessary to highlight the corresponding navigation item based on the current page visited by the user, and to determine whether there is a submenu to dynamically expand.

<nav>
    <ul class="main-menu">
        {% navList navs %}
        {% for item in navs %}
            <li class="menu-item {% if item.IsCurrent %}active{% endif %}">
                <a href="{{ item.Link }}">{{ item.Title }}</a>
                {% if item.NavList %} {# 判断是否有下级导航 #}
                    <ul class="sub-menu">
                        {% for subItem in item.NavList %}
                            <li class="sub-menu-item {% if subItem.IsCurrent %}active{% endif %}">
                                <a href="{{ subItem.Link }}">{{ subItem.Title }}</a>
                            </li>
                        {% endfor %}
                    </ul>
                {% endif %}
            </li>
        {% endfor %}
        {% endnavList %}
    </ul>
</nav>

3. Special handling of the first, last, or image items in the list loopIn the article list or product list, it may be necessary to add special to the first article, the last article, or the article with a thumbnail

Related articles

How does the "Full Site Content Replacement" feature of AnQi CMS affect the final display of published content?

In the daily operation of website management, content updating and maintenance is a continuous and crucial aspect.AnQiCMS (AnQiCMS) understands this need and therefore provides a powerful and efficient feature - 'Full Site Content Replacement'.This feature is not a simple modification of the front-end display, but a deep and immediate modification at the database level of published content, which has a direct and comprehensive impact on the final display effect of the website content.What is the "site-wide content replacement" feature?The "All-site Content Replacement" feature of Anqi CMS, as the name implies

2025-11-08

How to get and display the list of friend links in Anqi CMS?

In website operation, friendship links (also known as friend links) are an important component for improving website authority, increasing external traffic, and promoting interaction between websites.A well-maintained list of friendship links, which can not only increase the external links of the website and have a positive effect on SEO, but also provide users with more relevant information and improve the user experience.AnQi CMS knows its importance and provides an intuitive and user-friendly backend management interface and convenient template calling method, allowing you to easily obtain and display the list of friend links. We will introduce in detail how to implement this feature in Anqi CMS.--- ##

2025-11-08

Does AnQi CMS support the display of mathematical formulas and flowcharts in article content?

In content creation, especially in technical articles, academic papers, or project documents, we often need to present complex mathematical formulas and clear flowcharts to enhance the professionalism and readability of the content.For users of AnQiCMS, this is indeed a very practical need.Then, does Anqi CMS support displaying mathematical formulas and flowcharts in article content?The answer is affirmative, and it is not complex to implement. AnQi CMS provides flexible and diverse content management methods for content creators, one of which is the built-in Markdown editor.

2025-11-08

How to display the single page content of a specified ID in Anqi CMS, such as 'About Us'?

In AnQi CMS, whether it is to display a corporate profile like "About Us" or static content such as "Contact Information", "Terms of Service", the single-page feature is very practical.It allows you to create independent pages that do not belong to any category and provides a flexible way to control the display of these pages. To make the Anqie CMS display the single page content of a specified ID, you will mainly use the built-in template tags and the page management function of the back-end.This makes it very convenient to customize exclusive layouts at any location on the website or for specific single-page websites.

2025-11-08

How to implement multi-condition filtering display of article lists (such as by price, region) in Anqi CMS?

In modern website operations, users' demand for content filtering is growing increasingly.Whether it is to find products at a specific price or to inquire about activities in a certain area, an article list that supports multi-condition filtering can greatly improve user experience and content reach efficiency.AnQiCMS (AnQiCMS) leverages its flexible content model and powerful template tags to provide website operators with a convenient solution for implementing such complex filtering functions.

2025-11-08

How to set the website Logo, filing number, copyright information and other global display content in Anqi CMS?

The website's logo, filing number, copyright information, these seemingly trivial elements are the foundation of the website's professional image, brand recognition, and compliance.In Anqi CMS, managing these global display contents is much more convenient than imagined, without the need for complex code modifications, just by performing intuitive backend operations, you can easily create a professional and standardized website facade.This article will guide you step by step on how to efficiently set up and use these important global information in Anqi CMS.

2025-11-08

How can AnQi CMS automatically extract the first image in the article content to display as a thumbnail?

In website content operation, carefully selecting and setting thumbnails for each article is a routine and time-consuming task.These small images can effectively attract visitors' attention and are also the key to maintaining visual consistency on the list page and recommended positions.Anqi CMS deeply understands this, providing users with a very convenient feature: automatically extracting the first image from the article content to display as a thumbnail. ### Core Feature Unveiling: Automatic Thumbnail Extraction Mechanism Anqi CMS showcases its user-friendly side in content management.When you write or edit an article, if you have not manually uploaded a specific thumbnail

2025-11-08

How to prevent content scraping in Anqi CMS and add watermark display to images?

In the era where content is king, the value of original content is self-evident, however, the problems of content plagiarism and image theft are becoming increasingly rampant.For website operators, how to effectively protect their hard work, prevent malicious collection of content, and add exclusive identification to images is a key link in maintaining brand image and content rights.AnQiCMS (AnQiCMS) is well aware of this pain point and has built-in features to address these challenges.

2025-11-08