How does AnQiCMS implement template inheritance through the `extends` tag to simplify the maintenance of page structure display?

Calendar 👁️ 76

In website operation and content management, maintaining a clear, unified, and easy-to-maintain website structure is an important challenge.Especially when there are many web pages, repetitive page elements (such as headers, footers, navigation bars) need to be modified one by one, which undoubtedly consumes a lot of time and effort.AnQiCMS (AnQi CMS) is exactly through its powerful template inheritance mechanism, especiallyextendsThe use of tags helps us efficiently solve this problem.

Imagine if each page of a website were compared to a painting, then the template inheritance mechanism is like providing a standardized frame and background canvas for these paintings.It allows us to define a basic "parent template" that includes the common structure and elements of all web pages.And the specific "child template" only needs to inherit this parent template, then fill in or modify those content areas that are unique to itself.This way, the overall structure of the website is like building blocks, flexible and stable.

The template inheritance syntax of AnQiCMS is similar to Django template engine, its core lies inextendsandblockThese tags.

First, we need to create a basic templateWe usually name itbase.htmlIt includes the common layout of the website page. In this basic template, we can use{% block block_name %}{% endblock %}This syntax defines several content areas that can be "filled" or "overridden" by sub-templates. For example, a typicalbase.htmlIt might look like this:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    {% block title %}<title>我的AnQiCMS网站</title>{% endblock %}
    <link rel="stylesheet" href="/public/static/css/base.css">
    {% block head_extra %}{% endblock %} {# 预留额外头部资源位置 #}
</head>
<body>
    <header>
        <div class="logo">AnQiCMS</div>
        <nav>{% include "partial/main_nav.html" %}</nav>
    </header>

    <main>
        {% block main_content %}
            <p>这里是基础模板的默认内容,子模板可以覆盖。</p>
        {% endblock %}
    </main>

    <footer>
        <p>&copy; {% now "2006" %} 我的AnQiCMS网站. All rights reserved.</p>
        {% include "partial/footer_links.html" %}
    </footer>

    <script src="/public/static/js/main.js"></script>
    {% block body_extra %}{% endblock %} {# 预留额外底部脚本位置 #}
</body>
</html>

As you can see,base.htmlis defined intitle/head_extra/main_contentandbody_extraand many moreblockAreas. These areas provide customized entries for sub-templates.

Then, when we create specific page templatesfor example, an article detail pagearticle_detail.htmlwe only need to use at the beginning of the file.{% extends 'base.html' %}To declare it inherits frombase.html. Then, use the same-namedblocktag to override or add content to the corresponding area.

{% extends 'base.html' %}

{% block title %}<title>{% archiveDetail with name="Title" %} - 我的AnQiCMS网站</title>{% endblock %}

{% block head_extra %}
    <meta name="keywords" content="{% tdk with name="Keywords" %}">
    <meta name="description" content="{% tdk with name="Description" %}">
{% endblock %}

{% block main_content %}
    <article class="article-detail">
        <h1>{% archiveDetail with name="Title" %}</h1>
        <div class="meta">
            <span>发布日期:{% archiveDetail with name="CreatedTime" format="2006-01-02" %}</span>
            <span>分类:<a href="{% categoryDetail with name='Link' %}">{% categoryDetail with name='Title' %}</a></span>
        </div>
        <div class="content">
            {%- archiveDetail articleContent with name="Content" %}
            {{ articleContent|safe }}
        </div>
    </article>
    <div class="related-articles">
        <h3>相关推荐</h3>
        {% archiveList archives with type="related" limit="5" %}
        <ul>
            {% for item in archives %}
                <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
            {% endfor %}
        </ul>
        {% endarchiveList %}
    </div>
{% endblock %}

{% block body_extra %}
    <script src="/public/static/js/article_comments.js"></script>
{% endblock %}

By this means,article_detail.htmlYou no longer need to repeatedly write common code for headers, footers, navigation, etc., just focus on the unique parts such as article titles, content, and metadata.

The simplified maintenance effect brought by template inheritance is evident:

  • Reduce code redundancy:The core layout code only exists inbase.htmlone piece, avoiding a lot of repetitive pasting.
  • Unified website style:The visual style and basic structure of the website are managed bybase.htmlUnified management ensures consistency across all pages.
  • Improve maintenance efficiency:If you need to modify the website header logo, footer copyright information, or navigation structure, just changebase.htmlA file, all child templates inheriting it will be automatically updated, greatly saving maintenance time.
  • Accelerate the development process:When adding a new page, developers can quickly create based on an existing skeleton without starting from scratch, greatly improving development efficiency.
  • Reduce error rate:The centralized modification reduces omissions and errors caused by manual copying and pasting.

While usingextendsWhen labeling, there are several small details to pay attention to. The most important point is,{% extends %}The tag must be the first tag in the template file, and there should be no HTML code or other template tags before it, otherwise it will not work properly. In addition, to better organize the template, we can split some small, reusable code snippets (such as navigation menus, sidebar widgets) into separate files, and then through{% include "partial/filename.html" %}The tag is inblockIntroducing within the area, which makes the template structure more modular and clear.

The template inheritance mechanism of AnQiCMS is a very practical feature, which helps us manage the website page layout in a structured and efficient way, thus allowing us to devote more energy to high-quality content creation and website operation strategies.


Frequently Asked Questions (FAQ)

  1. If the child template does not cover a certain area of the parent templateblockwhat will be displayed?Answer: If the child template does not explicitly override a certain area defined in the parent templateblockThe area will default display the parent template in itblockThe content defined within the tag allows the parent template to provide default placeholder content, ensuring that the page can still display normally if the child template does not fully override it.

  2. extendsandincludeWhat are the differences between tags? When should they be used?Answer:extendsUsed to establish inheritance relationships between templates, defining a parent template as the "skeleton" or "blueprint" of a page, while child templates fill in content or overlay structures on it.includeIt is used to insert a standalone, reusable code snippet (such as a navigation menu, footer copyright information) at a specified location in any template. In simple terms,extendsIt is 'I build based on you', andincludeIt is 'I put this component in you'. When you want to define a basic layout for the entire page, use it.extendsWhen you want to reuse a small module in a page, useinclude.

  3. Does AnQiCMS support multi-level template inheritance, such asbase.html-u003elayout.html-u003epage.html?Answer: Yes, AnQiCMS supports multi-level template inheritance. You can make a template inherit frombase.htmlfor examplelayout.html), while thislayout.htmlcan also be used as another template (for examplepage.htmlThe parent template. This allows for the construction of more complex and refined template structure levels, flexible in meeting different page layout requirements, and further improving the reusability and maintainability of the template.

Related articles

How to reuse the code for displaying the common header or footer module in AnQiCMS templates through the `include` tag?

In AnQiCMS template design, efficient code reuse is the key to improving development efficiency and maintenance convenience.Faced with commonly used common elements on the website, such as headers, footers, navigation bars, sidebars, etc., if each page is rewritten repeatedly, it is not only time-consuming and labor-intensive, but also once modified later, it is a problem that affects the whole body.AnQiCMS is well-versed in this, providing users with a powerful template mechanism, where the `include` tag is the tool to achieve this goal.

2025-11-09

How does AnQiCMS's `lorem` tag assist in generating placeholder text for page display testing in template development?

During the development and testing phase of a website template, we often encounter a tricky problem: how to accurately evaluate the layout, style, and visual performance under different content lengths without real content?Manually filling in dummy data is not only inefficient but also difficult to simulate the richness of real content.AnQiCMS (AnQi CMS) provides a very practical auxiliary tag for template developers - `lorem`, which can easily generate placeholder text and greatly simplify the work of page display testing.### `lorem`

2025-11-09

How to control the display and SEO impact of links in the AnQiCMS friend link list according to the `nofollow` attribute?

In website operation, friend links are one of the important ways to improve website weight and expand external link resources.However, as search engine algorithms continuously evolve, how to finely manage these links, especially the application of the `nofollow` attribute, has become a topic that web administrators cannot ignore.AnQiCMS as a content management system focusing on SEO optimization, provides users with flexible link management functions, allowing you to effectively control the display and SEO impact of links.###

2025-11-09

How to display the language name, language code, and corresponding Emoji or icon of the current site in AnQiCMS?

AnQiCMS provides a flexible and efficient solution for building multilingual websites, allowing your content to easily reach global users.In a multilingual site, it is crucial to clearly display the language information of the current page to visitors and provide convenient language switching options, which are essential for improving user experience and the professionalism of the website.Today, let's discuss how to naturally display the language name, language code, and even vivid Emoji expressions or icons on the front page of your AnQiCMS website, making your multilingual website more attractive.

2025-11-09

How to use the `macro` tag in AnQiCMS to define reusable content display components and improve template efficiency?

In AnQiCMS template development, we often encounter situations where we need to repeatedly write the same or similar code blocks, such as article cards on the website, product display blocks, or buttons with specific styles.This repeated code not only reduces development efficiency, but also makes subsequent maintenance and modification more cumbersome.Luckyly, AnQiCMS provides the `macro` tag, which allows us to define reusable content display components, managing template code like writing small functions, thereby greatly enhancing template efficiency and maintainability.What is

2025-11-09

How does the `ContentTitles` tag in AnQiCMS help generate and display the table of contents on the article detail page?

In daily content creation and website operation, we often encounter long articles with a large amount of information.This kind of article is rich in content, but without a clear navigation structure, readers are easily lost in the ocean of text and find it difficult to quickly locate the part they are interested in.This not only affects the reading experience, but may also reduce the effective communication of the content.

2025-11-09

How to add categories for image resources in AnQiCMS and manage the front-end display according to categories?

In AnQiCMS (AnQiCMS), effectively organizing and managing your website image resources is the key to improving operational efficiency and front-end display quality.The image classification function not only makes the background management work orderly, but also provides a flexible foundation for the presentation of front-end page content.Below, let's take a detailed look at how to add categories for image resources in AnQiCMS and cleverly manage the display on the front end.

2025-11-09

How does AnQiCMS automatically filter or process external links in article content and control their display on the front end?

In content operation, properly handling external links in article content is an important link, it not only concerns the health of the website's SEO, but also affects the reading experience of users.AnQiCMS provides a flexible mechanism to help users automatically filter or process external links, and supports control over the front-end display methods. ### Core Function: Automatic Handling Strategy for External Links AnQiCMS provides convenient settings in the background, allowing users to manage external links in article content according to their own operational strategies.

2025-11-09