How to implement page layout inheritance and rewriting using the `extends` tag in AnQiCMS templates?

Calendar 👁️ 72

When building a website, maintaining a unified page style, improving development efficiency, and reducing maintenance costs are goals pursued by every website operator. AnQiCMS (AnQiCMS) is well-versed in this, and its template system design draws on the excellent ideas of the Django template engine, among whichextendsTags are one of the core functions to achieve this goal.

The core concept of template inheritance

Imagine that you are designing a series of posters. Each poster has a common brand logo, top title, and bottom contact information, but the promotional content in the middle is different.If you had to redraw all these repeated elements from scratch every time you make a poster, it would take a lot of time and effort.The template inheritance is what solves this problem.

In AnQiCMS, template inheritance allows you to define a basic 'parent template' (usually calledbase.htmlIt includes the HTML structure, style references, JavaScript references, and fixed page areas (such as headers, footers, sidebars, etc.) shared by all pages of your website. You can also use the parent template toblockLabel defines some 'fillable' areas.

And the child template inherits this parent template and selectively 'overwrites' or 'fills' these according to the specific needs of its own page.blockThe area. If the sub-template does not overwrite someblockIt will automatically use the default content defined in the parent template.This mechanism allows you to centrally manage the overall layout and style of the website, while also giving each page the freedom to flexibly customize its content.

extendsUsage method of the tag

In the AnQiCMS template system, useextendsTo declare a sub-template inherits from which parent template is very simple. You just need to write in the first line of the sub-template file:

{% extends 'path/to/your/base.html' %}

For example, if your basic layout file is in/template/default/base.htmlThen on other pages (such as article detail pages)article/detail.htmlAt the beginning:

{% extends 'base.html' %}

Herebase.htmlIt refers to the current active template directory underbase.htmlfile.

Then, in the parent template (base.html) you will see something similar to thisblockLabel definition:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    {% block title %}<title>我的AnQiCMS网站</title>{% endblock %}
    {% block meta_description %}<meta name="description" content="AnQiCMS官方网站">{% endblock %}
    {% block meta_keywords %}<meta name="keywords" content="AnQiCMS, Go CMS">{% endblock %}
    <link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/style.css">
    {# 引入头部脚本,如果存在 #}
    {% include "partial/head_scripts.html" if_exists %}
</head>
<body>
    <header>{% include "partial/header.html" %}</header>
    <nav>{% include "partial/nav.html" %}</nav>
    <div class="main-content">
        {% block content %}
            <!-- 这是父模板中的默认内容,如果子模板不重写,则显示此内容 -->
            <p>欢迎来到我们的网站!</p>
        {% endblock %}
    </div>
    <footer>{% include "partial/footer.html" %}</footer>
    {# 引入底部脚本,如果存在 #}
    {% include "partial/body_scripts.html" if_exists %}
</body>
</html>

In the parent template, we definedtitle/meta_description/meta_keywordsandcontentand many moreblock. TheseblockThe content inside the label is the default value, and the child template can optionally overwrite them. At the same time, likeheader/nav/footerThese parts that are consistent on all pages can be directly written in the parent template or throughincludetag to introduce smaller code snippets.

rewritingblockto customize page content

Now, let's assume we want to create the article detail page for AnQiCMS (article/detail.html) Create content. We only need to focus on the unique part of the article itself, and do not need to rewrite the entire HTML structure of the page.

Inarticle/detail.htmlIn it, we rewrite the parent template as follows.block:

{% extends 'base.html' %}

{% block title %}
    {# 使用AnQiCMS的archiveDetail标签获取文章标题,并拼接网站名称 #}
    <title>{% archiveDetail with name="Title" %} - {% system with name="SiteName" %}</title>
{% endblock %}

{% block meta_description %}
    {# 获取文章描述作为页面元描述 #}
    <meta name="description" content="{% archiveDetail with name="Description" %}">
{% endblock %}

{% block meta_keywords %}
    {# 获取文章关键词作为页面元关键词 #}
    <meta name="keywords" content="{% archiveDetail with name="Keywords" %}">
{% endblock %}

{% block content %}
    <article class="article-detail">
        <h1>{% archiveDetail with name="Title" %}</h1>
        <div class="article-meta">
            <span>发布时间:{{stampToDate(archive.CreatedTime, "2006-01-02")}}</span>
            <span>浏览量:{% archiveDetail with name="Views" %}</span>
        </div>
        <div class="article-body">
            {# 获取文章内容,并使用safe过滤器防止HTML转义 #}
            {%- archiveDetail articleContent with name="Content" %}
            {{articleContent|safe}}
        </div>
    </article>
{% endblock %}

By this means,article/detail.htmlIt contains only its unique content (article title, metadata, article body), while the overall layout, common header and footer of the page are automatically inherited frombase.html. You do not need to paste these common elements on each page, which greatly improves development and maintenance efficiency.

The secret to unified style and efficient layout

extendsThe core value of tags lies in:

  1. Unified visual styleAll pages are based on the same parent template structure, ensuring consistency in the overall layout and common elements (such as navigation, footer), enhancing the brand image.
  2. The leap in development efficiency: Developers can focus on the unique content of specific pages without having to rewrite a large amount of common HTML code, significantly speeding up the development speed.
  3. convenient maintenance experienceWhen you need to adjust the global layout or common components of the website, just modifybase.htmlor among themincludeThe common segment, all pages inheriting it will automatically update, avoiding the trouble of repeated modifications in multiple files.
  4. Flexible content customization:blockThe mechanism allows child templates to rewrite content as needed, ensuring a unified style while providing sufficient flexibility to handle special requirements of different pages.

This template inheritance mechanism is a key component of AnQiCMS for efficient and easy management of content. It makes the construction and operation of the website more structured.

Related articles

How to reuse common code snippets through the `include` tag in AnQiCMS templates to optimize the display structure?

When building and managing websites, improving efficiency and maintaining code neatness is the goal pursued by every operator.AnQiCMS (AnQiCMS) as an efficient content management system, provides powerful template functions, among which the `include` tag is the tool to achieve this goal.It allows us to abstract out repeated code snippets from the website, forming independent modules, and then referencing them where needed, greatly optimizing the template structure and subsequent maintenance work.

2025-11-07

How to define and use variables in AnQiCMS templates to optimize the display and management of content?

In AnQi CMS, flexibly using template variables is the key to optimizing website content display and management efficiency.It not only makes your website content more dynamic, but also greatly improves the maintainability and reusability of the template.This article will delve into how to define and use variables in AnQiCMS templates, as well as how to better present content through these techniques. ### Variables: The Foundation of Living Content Imagine how cumbersome it would be if every title, every paragraph on a website had to be manually modified.The introduction of variables is to solve this pain point

2025-11-07

How to correctly format timestamps in AnQiCMS templates so that content is displayed as needed?

In the daily operation of website content, time information plays a vital role.No matter the publication date of the article, the launch time of the product, or the latest update of the content, these time points directly affect users' perception and trust in the information.However, in the templates of content management systems, we often encounter dates stored in the form of "timestamps", which are precise but not intuitive.How should we correctly format these timestamps in the AnQiCMS template so that the content can be displayed in the way we expect?

2025-11-07

How to use the (for) loop tag in AnQiCMS template to traverse and display the content list?

Efficiently using the `for` loop tag in AnQiCMS templates to display content lists is an indispensable core skill for building dynamic websites.By flexibly using the `for` loop, we can easily traverse various types of data collections and display them in a customized style on the website page, whether it is a news list, product display, category navigation, or any other repetitive content block, the `for` loop can help you a lot.

2025-11-07

How to set the automatic compression and thumbnail processing method in AnQiCMS, which affects the image display on the front end?

The quality of website content is often not just reflected in text, but images as visual elements also play a crucial role.In website operation, how to efficiently handle images, ensuring that the images are clear and beautiful while also considering the loading speed, this is the core value of AnQiCMS image processing function.Today, let's delve deeper into how AnQiCMS helps us manage the automatic compression and thumbnail processing of large images, as well as how these settings will affect the display effect of images on the website's frontend.### Say goodbye to slow image loading

2025-11-07

How to configure the category template and document template in the background of AnQiCMS to achieve personalized content display?

As website operators, we all hope that our website content can be presented to visitors in the most attractive way possible.This is not just about visual beauty, but also about how the content structure can better serve users and how to highlight the unique brand character of the website.AnQiCMS fully understands this need, therefore it provides a powerful and flexible template configuration feature in the background, allowing you to achieve highly personalized displays based on different content types - whether it is articles, products, or category pages.Below, let's learn together how to configure category templates and document templates in the AnQiCMS backend

2025-11-07

How to configure banner images and thumbnails for a single page in AnQiCMS to enrich its visual display?

Today, with the increasing richness of website content, relying solely on text to convey information is no longer enough.Visual elements, such as eye-catching banner images and expressive thumbnails, can attract visitors' attention at first glance and enhance the professionalism and user experience of the website.For AnQiCMS users, cleverly configuring these visual elements for single pages (such as "About Us", "Contact Us", or "Service Introduction", etc.) is a simple operation that can bring great benefits.AnQiCMS fully understands the importance of content display and provides an intuitive backend management interface and a flexible template calling mechanism

2025-11-07

How to optimize the display link of category and tag pages by using custom URL aliases in AnQiCMS?

In website operation, the URL (Uniform Resource Locator) is not only the address of the content but also an important component of user experience and search engine optimization.A clear, concise URL that contains keywords, which can help users understand the page content faster and also improve the page ranking in search engines.AnQiCMS as an efficient content management system, provides powerful custom URL aliasing functionality, allowing users to flexibly optimize the display links of category and tag pages.### Understand the importance of URL optimization Before delving into the specific operations of AnQiCMS

2025-11-07