How can you reference and overwrite the base master template (extends) in a template to unify the display style?

Calendar 👁️ 74

In the world of AnQiCMS, building a website with a unified appearance, easy to manage, and efficient to update is a common pursuit of each content operator and developer.Imagine if each page of the website had to be designed individually for the header, footer, and sidebar, then the workload would be huge when it comes to modifying the logo, navigation menu, or copyright information.Fortunately, AnQiCMS cleverly utilizes the template inheritance mechanism, allowing us to easily achieve a unified display style while maintaining flexible content updates.

This is like designing a beautiful 'template' for your website—a skeleton that includes all common elements and basic layout.Then, you just need to fill in the unique content for each page on this skeleton, and you can ensure that the entire website looks coordinated and consistent.AnQiCMS uses a syntax similar to the Django template engine, in which theextendsandblockTags are the key to realizing this magic.

Build your website "master template":extendsFoundation

Firstly, we need to create a basic master template, usually we name itbase.html, and place it in your template directory (/template/你的模板目录/). Thisbase.htmlThe file will carry the common structure of all web pages, such as the basic HTML skeleton, introduced CSS styles, JavaScript scripts, Logo, main navigation, footer copyright information, etc.

In this template, you need to define some "slots", which are the areas that child pages can fill or overwrite content in the future. We useblocktags to define these slots.

For example, a simplifiedbase.htmlmight look something like this:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    {# 这里定义页面的标题,子模板可以重写 #}
    {% block title %}
        <title>{% system with name="SiteName" %} - 您的网站标题</title>
    {% endblock %}
    <meta name="keywords" content="{% tdk with name="Keywords" %}">
    <meta name="description" content="{% tdk with name="Description" %}">
    
    {# 引入全局CSS样式 #}
    <link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/style.css">
    {% block head_extra %}{# 预留给子模板添加额外head内容 #}{% endblock %}
</head>
<body>
    <header class="main-header">
        <div class="logo">
            <a href="{% system with name="BaseUrl" %}">
                <img src="{% system with name="SiteLogo" %}" alt="{% system with name="SiteName" %}">
            </a>
        </div>
        <nav class="main-nav">
            {# 主导航通常放在这里 #}
            {% navList navs %}
            <ul>
                {%- for item in navs %}
                    <li class="{% if item.IsCurrent %}active{% endif %}">
                        <a href="{{ item.Link }}">{{item.Title}}</a>
                        {%- if item.NavList %}
                        <dl>
                            {%- for inner in item.NavList %}
                                <dd><a href="{{ inner.Link }}">{{inner.Title}}</a></dd>
                            {% endfor %}
                        </dl>
                        {% endif %}
                    </li>
                {% endfor %}
            </ul>
            {% endnavList %}
        </nav>
    </header>

    <div class="main-content-wrapper">
        <aside class="sidebar">
            {% block sidebar %}{# 侧边栏内容 #}{% endblock %}
        </aside>
        <main class="page-content">
            {# 这是核心内容区域,每个子页面都会重写这里 #}
            {% block content %}
                <p>这里是母版默认内容,如果子模板不重写,就会显示。</p>
            {% endblock %}
        </main>
    </div>

    <footer class="main-footer">
        <p>{% system with name="SiteCopyright" %}</p>
        <p><a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">{% system with name="SiteIcp" %}</a></p>
    </footer>

    {# 引入全局JS脚本 #}
    <script src="{% system with name="TemplateUrl" %}/js/main.js"></script>
    {% block body_extra %}{# 预留给子模板添加额外body末尾内容 #}{% endblock %}
</body>
</html>

As you can see,base.htmlDefined the overall layout of the page and through{% block title %}/{% block sidebar %}/{% block content %}Tags, reserved writable positions for different areas. Even if the child template does not rewrite theseblockThey will also be displayedbase.htmlContent defined in it.

Sub-page: Refer to master page and overwrite content.

Now, we have the "master template" for the website. Next, when you create specific pages like the homepage, article detail page, and product list page, you don't need to rewrite the header and footer, just refer to thisbase.htmlThen rewrite what you need to change.blockarea.

Reference the master template.

In your child template file (for exampleindex.html/archive/detail.htmlorcategory/list.html), the first thing you need to do is to useextendstag to declare which master template it inherits from.

It is a very important point:{% extends 'base.html' %}It must be the first line of code in the child template file.The template engine needs to know which master template the file is based on first, in order to correctly parse the content that follows.

Rewrite the content

InextendsAfter declaration, you can use the same as in the master template.blockLabel name to rewrite the content of this area.

For example, for the homepage of the websiteindex.htmlYou may want to have a customized title and unique homepage content, but keep the common header, footer, and sidebar. You can write it like thisindex.html:

{# 必须是文件的第一行 #}
{% extends 'base.html' %}

{# 重写title block,定义首页标题 #}
{% block title %}
    <title>首页 - {% system with name="SiteName" %}</title>
{% endblock %}

{# 重写content block,放置首页特有的内容 #}
{% block content %}
    <h2>欢迎来到我们的网站首页!</h2>
    <p>这里是首页独有的内容,包括精选文章、推荐产品等。</p>
    
    {# 可以在这里引入其他模板片段,例如最新的文章列表 #}
    {% include 'partial/latest_articles.html' %}
{% endblock %}

{# 如果首页需要额外的脚本,可以重写body_extra block #}
{% block body_extra %}
    <script src="{% system with name="TemplateUrl" %}/js/index_specific.js"></script>
{% endblock %}

in thisindex.htmlIn them, we only rewrittentitleandcontentthese twoblock.head_extra/sidebarandmain-header/main-footerAreas such as this, due to not rewriting the correspondingblock, the template engine will automatically usebase.htmlcontent defined within. In this way, we have achieved a unified style of the page while ensuring the flexibility of the content.

Practice: unify style and local innovation

The power of template inheritance lies in its layered thinking. You can create different "secondary master pages" for different types of content (such as article details, product lists), and these secondary master pages inherit frombase.htmlFor example, you can have aarticle_base.htmlTo define the common layout of all article pages (such as fixed sidebar, comment section structure, etc.), then the specific article detail page will inheritarticle_base.html.

When using inheritance, there are some tips that can help us better organize the template:

  • Place the common header and footer inbase.html.This is the most basic uniformity, ensuring that all pages have the same navigation and brand information.
  • Define the area that may change.block.Even if it looks like it won't change now, reserve.blockIt is also a good habit for future expansion.
  • Use for small, reusable components,include.For example, a 'latest comments' module can be used,sidebar blockis used{% include 'partial/latest_comments.html' %}Introducing. This makes the code more modular and easier to maintain.
  • Do not over-nest.Although AnQiCMS supports multi-level inheritance, a too deep inheritance level may increase.

Related articles

How to prevent HTML tags from being automatically escaped when content is displayed?

When using Anqi CMS to manage website content, you may encounter a seemingly small but annoying problem: the formatted HTML code entered in the background editor is displayed as plain text on the front page, even presented directly in the form of angle brackets, losing the original style and layout.This not only affects the appearance of the page, but also discounts the carefully arranged content.Why is that, and how should we solve it?###

2025-11-09

How to implement the automatic display of content collected on the website front end?

How to automatically display the collected content on the website front-end is a concern for many content operators.AnQiCMS (AnQiCMS) is an efficient and flexible content management system that provides a complete solution from content entry to front-end display.It goes through a powerful content model, flexible template tags, and a friendly SEO mechanism, making this process simple and efficient.Next, we will explore how to use the various functions of AnQiCMS to automatically and beautifully display the valuable content you collect on the website's front-end.###

2025-11-09

How to add special styles to specific items when displaying in a list, based on the count in a for loop?

In website content display, we often need to highlight specific items in the list visually, whether it is to attract the attention of users or to enhance the hierarchy of the content.For example, you may want to add a "pin" mark to the latest few articles published, or make the odd and even rows of the list display different background colors to enhance readability.AnQiCMS (AnQiCMS) flexible template engine, which provides powerful support for these refined controls.AnQiCMS's template system borrows the syntax of the Django template engine

2025-11-09

How to use filters in a template to format displayed numbers and strings (such as decimal places, case conversion)?

In website content presentation, the aesthetics and readability of the data are as important as the quality of the content itself.AnQiCMS (AnQiCMS) provides powerful filter (Filters) functions at the template level, allowing us to easily format numbers and strings displayed on the page, thereby enhancing user experience and the professionalism of page information. ### Filter: A process in data processing We can imagine a filter as a series of processes that handle data.Raw data is like unprocessed raw materials, after the processing of the filter, it can be shaped into what we need

2025-11-09

How to define macro functions in templates to reuse commonly used content display code snippets?

In the development of website content management system templates, we often encounter situations where we need to repeatedly write the same or similar code snippets to display specific types of content.For example, each article card in the blog article list, each product feature item on the product detail page, although their content is different, their structure and style are highly similar.If you copy and paste this code manually every time, not only is it inefficient, but you also have to repeat the laborious work in multiple places when you need to make modifications, which is very prone to errors.AnQiCMS (AnQiCMS) is an efficient content management system, its template engine supports similar

2025-11-09

How to customize new parameters in the AnQiCMS backend settings and display them in the front-end template?

In Anqi CMS, to meet the personalized and flexible content display needs of the website, you can easily customize new parameters and call these parameters to display in the front-end template.This greatly enhances the website's customization capabilities, whether it is to display specific contact information, additional attributes of articles, or special configuration of single pages, it can do so with ease. AnQi CMS provides two main ways of custom parameters: **global parameters at the system/contacts level** and **custom fields at the content model level**. Understand these two methods and their applicable scenarios

2025-11-09

How to implement unified content management and front-end display for AnQiCMS?

In the digital age, many enterprises and content operations teams need to manage multiple websites, which may involve different brands, product lines, regions, or multilingual content.The traditional approach often means deploying multiple independent CMS systems, leading to high maintenance costs, repeated content updates, and difficult data integration.How to achieve unified management of content across multiple sites and efficient front-end display in the face of such challenges has become an urgent problem to be solved.AnQiCMS (AnQi Content Management System) is precisely in this context, providing users with a simple and powerful solution. AnQiCMS

2025-11-09

How can you customize the display layout of different types of content using a flexible content model?

In website operation, we often encounter such a need: different types of content require different display methods.The layout of a news article may be very different from that of a product detail page, and a team member introduction page may have its own unique style.The flexible content model of AnQiCMS (AnQiCMS) is designed to address this pain point, allowing us to deeply customize the structure of the content and the final display layout, thereby providing a ** browsing experience for visitors.### Understanding the Core Value of Content Modeling One of the Core Advantages of AnQi CMS

2025-11-09