How can AnQi CMS templates inherit parent templates and rewrite specific block content to customize display?

Calendar 👁️ 73

In AnQi CMS, the template system provides a powerful and flexible mechanism, allowing users to efficiently customize the layout and content display of the website.In which, inheriting the parent template and rewriting specific block content is a key function to achieve personalized design and maintain the consistency of the website's overall style.

Core Concept: Say goodbye to repetition, embrace inheritance

Imagine a website's header, footer, sidebar, and other elements that are fixed and consistent on almost every page.If there is no template inheritance mechanism, we may need to rewrite these common codes on each page template, which not only increases the maintenance difficulty but also tends to lead to code redundancy and inconsistency.

The AnQi CMS template engine has borrowed the syntax from Django, which allows us to define a "skeleton" template (usually referred to as the parent template or base template), which includes the common structure and editable content areas of the website.Based on this skeleton, sub-templates are built, which do not need to repeat the general part, just focus on how to fill in or modify the specific areas defined in the parent template, thus achieving customized content display.

Foundation:extendswithblockTag

The core of realizing this mechanism in Anqi CMS template is two tags:extendsandblock.

  1. extendsTags:This tag is used to declare the parent template inherited by a sub-template. It must be the first non-comment tag in the sub-template. For example,{% extends 'base.html' %}means that the current template will inherittemplateUnder the directorybase.htmlIt serves as the basic layout. Through it, the child template gains all the structures and content defined in the parent template.

  2. blockTags: blockThe tag defines a named, writable content area in the parent template.The parent template will provide default content for these areas (if needed), but the child template can optionally override this content. AblockTags always appear in pairs, formatted as{% block 区域名称 %} ... {% endblock %}For example,{% block title %}默认标题{% endblock %}Defined a namedtitleblock, and provides default content.

Custom display: Rewrite practice in the sub-template

When the sub-template passesextendsAfter inheriting the parent template, it can rewrite the content of this block by definingblocktags with the same name as those in the parent template.

For example, if the parent templatebase.htmlhas a{% block content %}block, the child templateindex.htmlit can be rewritten like this:

{# index.html #}

{% extends 'base.html' %} {# 声明继承 base.html #}

{% block title %}
    <title>安企CMS首页 - 轻松定制你的网站</title> {# 重写父模板的 title 区块 #}
{% endblock %}

{% block content %}
    <div class="main-content">
        <h2>欢迎来到安企CMS!</h2>
        <p>这里是首页的定制内容,我们重写了父模板的 content 区块。</p>
        <p>你可以根据需要在此处添加文章列表、产品展示、轮播图等。</p>
    </div>
{% endblock %}

{# 子模板中未被 block 标签包裹的内容将不会被渲染 #}

in thisindex.htmlIn the sub-template:

  • {% extends 'base.html' %}Explicitly specifies that it inheritsbase.html.
  • {% block title %}Rewrote the parent template in<title>The content of the label makes it display as "Anqi CMS Home - Customize your website easily."
  • {% block content %}It completely replaces the parent template.contentThe default content in the block, inserted the unique HTML structure and text of the homepage.

Ifindex.htmlNot definedblock headerthenbase.htmlinheaderThe default content of the block will be displayed as is. This is the charm of template inheritance - customization as needed without touching the common part.

Actual operation: Customize your Anqi CMS template step by step

Let us demonstrate this process through a simple example:

Step 1: Create the basic template file (template/default/base.html)

Assume your template directory name isdefault.defaultCreate a folder underbase.htmlfile, the content is as follows:

{# template/default/base.html #}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    {# 定义一个可重写的 title 区块 #}
    {% block title %}
        <title>我的安企CMS网站</title>
    {% endblock %}
    <link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/style.css">
    {# 定义一个可重写的 head 额外内容区块 #}
    {% block extra_head %}{% endblock %}
</head>
<body>
    <header>
        <nav>
            <a href="/">首页</a>
            <a href="/about.html">关于我们</a>
            <a href="/contact.html">联系我们</a>
        </nav>
        <hr>
    </header>

    <div class="container">
        {# 定义一个可重写的内容主体区块 #}
        {% block content %}
            <p>这里是网站的默认内容区域。</p>
            <p>子模板可以在这里填充或重写它们自己的内容。</p>
        {% endblock %}
    </div>

    <footer>
        <hr>
        <p>&copy; {% now "2006" %} {% system with name="SiteName" %}. All rights reserved.</p>
    </footer>
    <script src="{% system with name="TemplateUrl" %}/js/main.js"></script>
    {# 定义一个可重写的 body 额外内容区块 #}
    {% block extra_body %}{% endblock %}
</body>
</html>

Thisbase.htmlContains the basic structure of the HTML document, CSS/JS references, and definestitle/extra_head/contentandextra_bodyFour writable areas.

Second step: Create a sub-template file (template/default/index.html)

Now, let's create the homepage templateindex.htmlto inheritbase.htmland rewrite the content:

{# template/default/index.html #}
{% extends 'base.html' %}

{% block title %}
    <title>安企CMS官方网站 - 灵活高效的企业级内容管理系统</title> {# 重写 title 区块 #}
{% endblock %}

{% block extra_head %}
    <meta name="description" content="安企CMS是基于Go语言开发的企业级内容管理系统,提供高效、可定制、易扩展的解决方案。">
    <meta name="keywords" content="安企CMS, 内容管理, CMS, Go语言, 企业建站">
{% endblock %}

{% block content %}
    <div class="hero-section">
        <h1>欢迎使用安企CMS!</h1>
        <p>一个专为中小企业、自媒体和多站点管理设计的Go语言内容管理系统。</p>
        <a href="/docs/start.html" class="button">开始使用</a>
    </div>

    <section class="features">
        <h2>核心功能</h2>
        <ul>
            <li>多站点管理</li>
            <li>灵活的内容模型</li>
            <li>高级SEO工具</li>
            <li>防采集与水印管理</li>
        </ul>
    </section>

    {# 可以在这里调用安企CMS的标签来展示动态内容 #}
    <section class="latest-articles">
        <h2>最新文章</h2>
        <ul>
            {% archiveList archives with type="list" moduleId="1" limit="5" %}
                {% for item in archives %}
                    <li>
                        <a href="{{item.Link}}">{{item.Title}}</a>
                        <span>发布日期: {{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
                    </li>
                {% empty %}
                    <li>暂无最新文章。</li>
                {% endfor %}
            {% endarchiveList %}
        </ul>
    </section>
{% endblock %}

{% block extra_body %}
    <script>
        // 只有首页才加载的JS逻辑
        console.log('首页特有的JS已加载。');
    </script>
{% endblock %}

When AnQi CMS rendersindex.htmlWhen it loads, it will first loadbase.htmlThe structure, then useindex.htmlinblock titleContent replacementbase.htmloftitleBlock, usingblock contentContent replacementbase.htmlofcontentBlock, and insertextra_headandextra_bodyAdditional content.

Auxiliary labels and **practice.

  • includewithextendsCollaboration: extendsUsed to establish the hierarchical relationship of the entire page layout, which determines the "skeleton" of the page. AndincludeTags are suitable for reusing smaller, independent components, such as a sidebar module, a comment form, or a product card.They complement each other, improving the reusability and maintainability of the template.
  • extendsThis must be the first label of the sub-template:This is a rigid rule. Any in{% extends %}The HTML code, text, even whitespace characters (except comments) that appeared previously can cause template inheritance to fail.
  • **Make good use of `

Related articles

How to truncate a long string and display an ellipsis in AnQi CMS templates?

The flexibility of content display is crucial in the template making process of Anqi CMS.When it is necessary to display a long text within a limited space, such as an article abstract, product description, or brief introduction, truncating the strings with an ellipsis is a common optimization method. It can effectively maintain the neat layout of the page and guide users to click and view the full content.AnQi CMS is developed based on Go language, its template engine syntax is similar to Django, and it provides powerful filter (filters) functions that can easily meet this requirement.### Skillfully Utilize Filters

2025-11-08

How to automatically add hyperlinks to URLs and email addresses in article content of AnQi CMS?

In content management and website operations, automatically converting URLs and email addresses in the text to clickable hyperlinks can greatly enhance user experience and optimize the internal link structure of the website to some extent.For AnQiCMS users, implementing this feature is not through simple backend settings, but through the specific "filter" provided by its powerful template engine.This design approach gives website owners great flexibility and control, allowing them to finely manage the generation of links according to the needs of different content areas.

2025-11-08

How to display multi-language switch links and the current language identifier in Anqi CMS?

In AnQi CMS, implementing multi-language switch links and current language identification is a very practical feature that can help your website easily face global users and enhance the internationalization level of your website.Aqicms provides intuitive and powerful template tags, allowing you to flexibly display these elements on the website front end. ### One, understand the multilingual capabilities of Anqicms Anqicms has considered the needs of multilingual sites from the very beginning and provides comprehensive multilingual content management support.This means you can create and manage independent content for different languages in the background, such as articles

2025-11-08

How to display a custom prompt message during the website shutdown maintenance of AnQi CMS?

During the operation of the website, we sometimes have to temporarily close the website for maintenance in order to upgrade the system, optimize data, or adjust functions.How to clearly and friendly convey this status to visitors, avoiding confusion or loss of users, is particularly important.Safe CMS provides a set of built-in solutions that allow you to easily customize the prompt information for your website during maintenance shutdown. ### Understanding the shutdown mechanism of AnQi CMS The shutdown feature of AnQi CMS is designed to be very intuitive and flexible.When you enable the closed site mode, the system will automatically intercept all access requests to the website content

2025-11-08

How can AnQi CMS use the 'time factor' feature to display future published content?

How to efficiently and strategically publish content in today's fast-paced content environment is a question that every content operator is thinking about.Maintain the consistency and foresight of content publication, which can not only enhance the user experience but also effectively promote brand communication and search engine optimization.AnQi CMS deeply understands this, and its 'Time Factor' feature was born for this very purpose, providing content creators and operation teams with a precise tool to control the timing of content release.## Precise Planning: The Foundation of Content Operation We have all faced such a situation: writing an精心 prepared article

2025-11-08

Does AnQi CMS provide an integrated JS statistics code calling tag for easy display on the page?

When using AnQiCMS, many users are concerned about how to conveniently integrate third-party JavaScript statistical codes into web pages, such as Baidu Statistics, Google Analytics, and so on.About this, AnQiCMS indeed provides built-in mechanisms and flexible template tags, allowing you to easily call and display these statistics codes on your website.### Using built-in JS code to call tags AnQiCMS to meet the needs of website operators to integrate third-party scripts

2025-11-08

How to display the current year or time in a specific format in AnQi CMS template?

In website operation, it is a common requirement to flexibly display the current year or format specific time, whether it is the automatic update of the year in the copyright statement or the clear display of the publication time on the article detail page.AnQiCMS provides two main ways to meet these needs, they are both simple to use and follow the elegant time handling of the Go language.### Method 1: Directly retrieve and display the current year or time (`{% now %}` tag) When you need to directly retrieve and display the current year or the current time down to the second in the template

2025-11-08

How to add multi-dimensional parameter filtering function for the product list page in Anqi CMS and display the filtering results?

In modern e-commerce and content display websites, providing multi-dimensional parameter filtering functions for product lists has become a key factor in improving user experience and conversion rates.Visitors can quickly locate the products they need based on their own needs, not only saving time but also significantly improving the usability of the website.For users of AnQiCMS, implementing such a feature is not difficult. The built-in powerful functions and flexible template tags make the whole process intuitive and efficient.This article will delve into how to add multi-dimensional parameter filtering functions to the product list page in Anqi CMS

2025-11-08