How to use the `block` tag under `if` conditions to decide whether to override the parent template content when using `extends`?

Advanced Template: How to UseextendsWithblockto Achieve Conditional Content Coverage

In the daily operation of the website, we are well aware of the importance of the maintainability and flexibility of website content for efficient operation.AnQi CMS provides a solid foundation for content management with its efficient architecture based on the Go language and a template engine syntax similar to Django.extends)and Content Blocks(blockis the core to build reusable and scalable website layouts.But merely inheriting and overriding is not enough. Often, we hope that the child template can 'intelligently' decide whether to override the content of the parent template under specific conditions, or to continue using the default design of the parent template.Today, let's delve into this clever and practical technique.

Template Inheritance: The Foundation of Building a Website's Skeleton

Imagine that we are building a brand new corporate website template for AnQiCMS.A website usually has a fixed header, navigation, sidebar, and footer area, with only the core content area changing from page to page.extendsandblockThe combination is like the steel and concrete framework for constructing a house.

Firstly, we create a universal parent template, for example namedbase.htmlIn this parent template, we will define the overall structure and common elements of the website, and useblockLabel the areas that allow child templates to be modified or filled. These blocks are like reserved slots, waiting for specific content to fill them. For example:

<!-- base.html (父模板示例) -->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>{% block page_title %}AnQiCMS企业官网 - 领先的内容管理系统{% endblock %}</title>
    <meta name="description" content="{% block page_description %}AnQiCMS致力于提供高效、可定制的企业级内容管理解决方案。{% endblock %}">
    <!-- 引入通用样式、脚本等 -->
</head>
<body>
    <header>
        <!-- 网站通用头部,包含Logo、主导航等 -->
        {% block header_nav %}{% navList navs %}...{% endnavList %}{% endblock %}
    </header>

    <div class="main-wrapper">
        <aside class="sidebar">
            {% block sidebar_content %}
                <!-- 父模板默认的侧边栏内容,如通用广告、最新文章列表等 -->
                <p>这里是默认侧边栏,展示最新动态。</p>
            {% endblock %}
        </aside>
        <main class="content">
            {% block main_content %}
                <!-- 父模板默认的主内容区域提示 -->
                <p>欢迎来到AnQiCMS网站!</p>
            {% endblock %}
        </main>
    </div>

    <footer>
        <!-- 网站通用底部,包含版权信息、友情链接等 -->
        {% block footer_info %}{% system with name="SiteCopyright" %}{% endblock %}
    </footer>
</body>
</html>

Later, when we create specific pages, such as the detail page of an articlearticle_detail.html, we just need to{% extends 'base.html' %}inherit the parent template, and then selectively rewrite some ofblockThe block fills in the title, content, and other information of the article itself. In this way, the overall layout of the website can be maintained consistently, while the personalized content of each page can be easily achieved.

The beauty of dynamism: inblockinifConditions determine what is retained or discarded

Now, we are facing a more refined requirement: certain pages have unique sidebar content, while the majority of other pages follow the general sidebar of the parent template. If we simply rewritesidebar_contentIf the block is removed, these pages will completely lose the default sidebar provided by the parent template.We truly want is a 'conditional override' - when specific conditions are met, display the customized content of the sub-template; otherwise, 'fallback' to the default content of the parent template.

This is exactlyifTags inblockThe place where ZHONGDA showcases its abilities. The template engine of Anqi CMS supports nested logic judgments inblockthe inner part. The key is that we can use this special variable to refer to the current{{ block.super }}template from the parent template.blockThe original content.

Let us take the sidebar as an example, assuming we have a special 'Promotion Page', the sidebar of which needs to display a unique promotion advertisement instead of the regular latest news.special_promotion.htmlIn this sub-template, we can handle it like this:

<!-- special_promotion.html (子模板示例) -->
{% extends 'base.html' %}

{% set is_promotion_page = true %} {# 假设通过某种逻辑(如URL参数、文章标签、分类ID等)判断出这是促销页 #}

{% block page_title %}限时促销!不容错过 - {% tdk with name="Title" siteName=true %}{% endblock %}

{% block main_content %}
    <section class="promotion-banner">
        <img src="{% system with name='TemplateUrl' %}/images/promotion-banner.jpg" alt="年度大促">
        <h2>激动人心的促销活动标题!</h2>
        <p>这里是促销活动的详细介绍和规则。</p>
    </section>
    <article>
        <!-- 促销活动主体内容 -->
    </article>
{% endblock %}

{% block sidebar_content %}
    {% if is_promotion_page %}
        <div class="promotion-ad">
            <h3>仅限今日!</h3>
            <p>超值优惠,立即抢购!</p>
            <a href="/promo-details.html" class="btn btn-primary">了解详情</a>
        </div>
    {% else %}
        {# 如果is_promotion_page为false(例如,这个变量未设置或设为false),则渲染父模板的默认侧边栏内容 #}
        {{ block.super }}
    {% endif %}
{% endblock %}

In this example:

  1. We first go through{% set is_promotion_page = true %}In practical applications, this is usually the result of dynamic judgment based on backend data, for example:{% if archive.Flag contains 'promo' %}or{% if category.Id == 123 %}Set a conditional variable.
  2. Insidebar_contentWithin the block, we use{% if is_promotion_page %}to determine.
  3. If the condition is true, the child template will render the promotional advertisement defined within its internal structure.
  4. If the condition is false,{% else %}the branch inside{{ block.super }}will be executed, magically wrapping the parent templatebase.htmlinsidebar_content