How to use the template inheritance mechanism to unify and display the website page structure?

Calendar 👁️ 63

When building a website, maintaining consistency in page structure, improving development efficiency, and reducing maintenance costs is the key to website operation.AnQiCMS (AnQiCMS) with its powerful features based on the Django template engine syntax, provides us with an efficient template inheritance mechanism, allowing easy unified management and flexible display of the website page structure.

1. The core concept of template inheritance: Building the website skeleton

Imagine that your website is like a house composed of different rooms.Each room (page) has its unique functions and content, but they share the same foundation, roof, and outer wall structure.In the template mechanism of AnQiCMS, this shared structure is what we call the 'master template' or 'basic template', usually namedbase.html.

The basic template defines the most core and general layout elements of a website, such as the header (header), footer (footer), main navigation (navigation), sidebar (sidebar), and general containers for page content, etc. Inbase.htmlIn Chinese, we use{% block 块名称 %}{% endblock %}The tag is used to define an area that can be overridden by the sub-template.These blocks are like reserved spaces for houses, sub-templates can fill or modify these spaces according to their own needs.

For example, a typicalbase.htmlMay contain the following structure:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}默认网站标题 - {% system with name="SiteName" %}{% endblock %}</title>
    <meta name="keywords" content="{% tdk with name="Keywords" %}">
    <meta name="description" content="{% tdk with name="Description" %}">
    <link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/main.css">
    {% block head_extra %}{% endblock %} {# 允许子模板添加额外的CSS或JS #}
</head>
<body>
    <header>
        {% block header %}
            <!-- 网站通用头部内容,如Logo、顶部菜单 -->
            <div class="logo"><img src="{% system with name="SiteLogo" %}" alt="{% system with name="SiteName" %}"></div>
            {% navList mainNav with typeId="1" %} <!-- 调用主导航 -->
                <!-- 导航列表循环 -->
            {% endnavList %}
        {% endblock %}
    </header>

    <main class="container">
        {% block main_content %}
            <aside class="sidebar">
                {% block sidebar %}{% endblock %} {# 侧边栏内容 #}
            </aside>
            <article class="content">
                {% block content %}
                    <!-- 默认页面内容区域 -->
                {% endblock %}
            </article>
        {% endblock %}
    </main>

    <footer>
        {% block footer %}
            <!-- 网站通用页脚内容,如版权信息、联系方式 -->
            <p>{% system with name="SiteCopyright" %}</p>
            <p>{% contact with name="Address" %}</p>
        {% endblock %}
    </footer>

    {% block body_end_scripts %}{% endblock %} {# 允许子模板添加页面底部的JS #}
</body>
</html>

In the sub-template, for example, the article list pagearchive_list.htmlWe just need to use it at the beginning of the file{% extends 'base.html' %}Tags, to inheritbase.htmlAll the structures. Next, by defining the same named{% block %}Label, we can easily overwrite or append the corresponding block content of the parent template.

`html{% extends 'base.html' %}

{% block title %}Article List - {% categoryDetail with name=“Title” %} - {% system with name=“SiteName” %}{% endblock %}

{% block head_extra %}

<link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/archive-list.css">

{% endblock %}

{% block sidebar %}

<!-- 这里放置文章列表页特有的侧边栏内容,例如热门文章、分类导航等 -->
<h3>文章分类</h3>
{% categoryList categories with moduleId="1" parentId="0" %}
    <ul>
        {% for cat in categories %}
            <li><a href="{{ cat.Link }}">{{ cat.Title }}</a></li>
        {% endfor %}
    </ul>
{% endcategoryList %}

{% endblock %}

{% block content %}

<h1>{% categoryDetail with name="Title" %}</h1>
<div class="archive-list">
    {% archiveList archives with type="page" limit="10" %}
        {% for item in archives %}
            <div class="archive-item">
                <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
                <p class="description">{{ item.Description }}</p>
                <span class="date">{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
                <span class="views">{{ item.Views }} 阅读</span>
            </div>
        {% empty %}
            <p>暂无文章内容。</p>
        {% endarchiveList %}
    {% endarchiveList %}
</div>
{% pagination pages with show

Related articles

How to reference and display common code snippets (such as header, footer) in AnQiCMS templates?

It is crucial to maintain consistency in page structure and code maintainability in website construction and content management.For a content management system like AnQiCMS, effectively referencing and managing common code snippets, such as the website header (Header) and footer (Footer), is the key to achieving this goal.Through AnQiCMS's flexible template mechanism, even users with little understanding of technical details can easily implement these features, thus building a website with clear structure and easy maintenance.###

2025-11-08

How to define and display temporary variables in the template?

In Anqi CMS template development, mastering how to define and display temporary variables in the template is a very practical skill.It can help us handle data more flexibly, optimize template structures, and write more concise and efficient code.Temporary variables are like the "small tags" you grab on the fly while making a page, used to temporarily store some data for later use.### Why do we need temporary variables? Imagine a scenario: you might need to get a value from a label and then perform a series of operations on it (such as truncating strings, formatting time)

2025-11-08

How to conditionally display different content or styles within a loop?

In website content display, we often encounter the need to display different content or assign unique styles to elements according to the order of data cycles, specific attributes, or states.For example, odd and even rows in the list need different background colors, special markings for certain types of products, or when a field is empty, hide the corresponding area.With its flexible and powerful Django-like template engine, AnQi CMS makes these requirements simple and easy to implement.By cleverly combining loop tags (`{% for ... %}`) and conditional judgment tags (`{% if ... %}`)

2025-11-08

How to display the names and introduction information of different user groups?

Displaying different user group names and introduction information in AnQi CMS is an important part of website personalized operation and content distribution.Whether you want to display exclusive identification for VIP members or provide customized content for users with different permissions, Anqi CMS's flexible template tag system can help you easily achieve this. ### Understanding the Core Concepts of User Group Information To display the name and introduction of the user group, it is first necessary to clarify two key points: whether the user is logged in, and which user group the user belongs to. Safe CMS provides powerful template tags

2025-11-08

How to dynamically display the current year or a specified date on a website?

In website operation, we often need to display some dynamic time information, such as the copyright year at the bottom of the website (such as “© 2023-Current Year”), the publication or update time of articles, or the deadline for specific events, etc.These dynamic dates not only maintain the timeliness of website information, but also improve user experience, and in some cases, are also beneficial for SEO. The AnQi CMS provides a series of flexible template tags that allow you to easily display the current year or a specified date dynamically at any location on the website.We will discuss in detail how to make use of these features.

2025-11-08

How does AnQi CMS display or hide specific content based on user group permissions?

In daily website operation, we often encounter such needs: some content is intended for everyone to see, while some is only open to specific members, or shows different information for users of different levels.AnQiCMS (AnQiCMS) provides powerful user group permission management features, allowing us to flexibly control the visibility of website content and achieve the 'thousand faces of content'. ### Why do you need to display or hide content by user group?Imagine you are running a website that offers a variety of services, including free basic articles and paid in-depth reports

2025-11-08

How to display the document list matching the keywords on the search results page?

When visitors come to your website and hope to quickly find the information they need, a highly efficient and accurate on-site search feature is particularly important.AnQiCMS (AnQiCMS) took this into consideration from the beginning of its design, providing you with a flexible and powerful content management and template system, allowing you to easily display a list of documents matching the search keywords on the search results page.Next, let's see how to implement this function together.### Learn about AnQi CMS search mechanism AnQi CMS has built-in powerful search capabilities, it will automatically index the content you publish

2025-11-08

How to prevent content collection interference code and watermark display in AnQi CMS?

Today, with the increasing richness of digital content, the value of original content becomes more and more prominent, but the problems of content plagiarism and collection that come with it also make many content creators and operators headache.AnQiCMS (AnQiCMS) fully understands this pain point, and therefore built-in strong content protection mechanisms from the very beginning of the system design, including anti-crawling interference codes and image watermark functions.These features are designed to help users effectively defend original copyright, ensure content exclusivity, and thereby enhance content value.

2025-11-08