How to use the template inheritance feature of AnQiCMS to build a consistent page skeleton and display local content?

Calendar 👁️ 69

During the process of website construction and operation, how to efficiently manage page layout, ensure consistent visual style, and be able to flexibly adjust local content is a challenge faced by many operators.AnQiCMS provides powerful template inheritance functionality, which can help us cleverly solve these problems, realizing the structured management of website page structure and the flexible display of content.

Understanding the core concept of template inheritance

Imagine, template inheritance is like a 'blueprint' or 'master template' in architectural design.We first design a universal page skeleton, which includes the shared elements of all web pages, such as the header, footer, and navigation bar.This skeleton is called the "Base Template", which is like a general design diagram.

In this basic template, we use{% block %}Labels define some replaceable areas. These areas are like reserved spaces in buildings, such as living rooms, bedrooms, or kitchens.The child page (which inherits the basic template) can selectively fill or modify the content of these reserved spaces according to its own needs.

When a sub-template is used{% extends 'base.html' %}When a tag declaration inherits from a base template, it gains all the layout and structure of the base template. The child template can then define the same named{% block %}Label to cover or modify the content of the corresponding block in the basic template.If the sub-template does not define a block, it will directly inherit the default content of that block from the base template.It is worth noting that,{% extends %}The label must be the first label in the child template, otherwise the template inheritance will not work properly.

Establishing the "skeleton" of the page: the practice of Base template.

In AnQiCMS, we usually create a namedbase.htmlThe file serves as the basic template for our website. This template carries the core and most general layout and elements of the website. For example, it will include HTML's<html>/<head>and<body>Label, as well as the main area division of the page.

A typicalbase.htmlIt may be organized like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    {% block title %}
        <title>默认网站标题</title>  <!-- 子页面可重写此块,不重写则使用默认 -->
    {% endblock %}
    {% comment %} 这里可以放置其他全局的meta标签、CSS链接 {% endcomment %}
    <link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/global.css">
    {% block head_extra %}{% endblock %} {# 预留给子页面添加额外头部内容 #}
</head>
<body>

<div class="header">
    {% comment %} 通常这里会包含网站Logo、主导航等固定内容 {% endcomment %}
    <img src="{% system with name="SiteLogo" %}" alt="{% system with name="SiteName" %}" />
    {% navList navs %}
        <ul>
            {%- for item in navs %}
                <li><a href="{{ item.Link }}">{{item.Title}}</a></li>
            {% endfor %}
        </ul>
    {% endnavList %}
</div>

<div class="main-content-wrapper">
    <div class="sidebar">
        {% include 'partial/aside.html' %} {# 引入侧边栏局部片段 #}
    </div>
    <div class="main-area">
        {% block content %}
            <h4>这里是主内容区的默认占位符</h4>
        {% endblock %}
    </div>
</div>

<div class="footer">
    {% comment %} 通常这里会包含版权信息、备案号、友情链接等 {% endcomment %}
    <p>{% system with name="SiteCopyright" %}</p>
    <p><a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">{% system with name="SiteIcp" %}</a></p>
    {% linkList friendLinks %}
        {% if friendLinks %}
            <span>友情链接:</span>
            {% for item in friendLinks %}
                <a href="{{item.Link}}" {% if item.Nofollow == 1 %} rel="nofollow"{% endif %} target="_blank">{{item.Title}}</a>
            {% endfor %}
        {% endif %}
    {% endlinkList %}
</div>

{% block scripts %}{% endblock %} {# 预留给子页面添加JS脚本 #}
</body>
</html>

in thisbase.htmlIn it, we definedtitle/head_extra/contentandscriptsand many moreblock. TheseblockIt will become a 'slot' that can be freely filled or overridden by the child page.

Related articles

How to reference the common template code snippets (such as header and footer) in AnQiCMS to unify the display of the page?

It is crucial to maintain a unified page style in website content operation to enhance user experience and maintenance efficiency.Imagine if your website had hundreds or even thousands of pages, and each page's header, footer, or sidebar needed to be manually modified, that would be an enormous project.Fortunately, AnQiCMS provides a powerful and flexible template mechanism that can help you easily refer to and manage common code snippets, ensuring the consistency of the website display.AnQiCMS's template system borrows the syntax of the Django template engine, making it easy to use and powerful.it passes `

2025-11-08

How to define temporary variables in templates to assist in content display logic?

When using AnQiCMS for website content management, the flexibility of the template is crucial for achieving diverse content display.At times, we need not only to output data directly, but also to perform some temporary processing, calculation, or judgment based on business logic, at which point defining temporary variables in the template becomes particularly useful.This not only makes the template code more tidy, but also avoids repeated retrieval or calculation of the same data, thereby improving rendering efficiency and maintainability.The template engine syntax of AnQi CMS is similar to Django template engine

2025-11-08

How to format a timestamp into a readable date and time format for display in AnQiCMS?

The website content is updated frequently, each article, product, or comment has its creation and update time point.However, these times are usually stored in the background in the form of a series of numbers, which is what we commonly call timestamps.It is not friendly to display these timestamps to visitors, we need to convert them into a clear date and time format.In AnQiCMS, formatting these timestamps into the date and time format we are accustomed to is actually very simple and intuitive.The system comes with a powerful template tag that can help us easily achieve this.###

2025-11-08

How to use if/for tags in a template to control the conditional display and looping display of content?

When building and operating a website, the dynamic display and flexible management of content are key to improving user experience and operational efficiency.AnQiCMS (AnQiCMS) understands this point, therefore its template engine provides powerful and intuitive conditional judgment (`if`) and loop traversal (`for`) tags, allowing you to easily control the display logic of content and achieve highly customized page layouts.The template syntax of AnQi CMS borrows the concise style of Django template engine, with a very low learning cost.By mastering these core tags

2025-11-08

How does AnQiCMS use static caching technology to improve page loading speed and content display efficiency?

In today's fast-paced online world, the loading speed and display efficiency of websites have become key indicators of user experience and search engine rankings.A responsive website can not only effectively retain visitors but also stand out in fierce competition.AnQiCMS (AnQiCMS) understands this, providing strong support for users to achieve fast website access and efficient content presentation through its built-in static caching technology.Understand static caching: The core mechanism of website acceleration To understand how AnQi CMS works, you first need to understand what static caching is

2025-11-08

How to prevent AnQiCMS website content from being maliciously scraped and add watermarks to images?

In the digital age, the value of original content is self-evident; they are the core of attracting traffic, building brands, and improving SEO rankings for websites.However, the risk of malicious collection of content and theft of images is also increasing.This not only harms the original creators' labor成果, but may also cause the website's ranking in search engines to be damaged, affecting brand reputation.As a website operator, we naturally hope to effectively protect our digital assets.The Anqi CMS is an efficient and comprehensive content management system that has always given full consideration to the importance of content security from the beginning of its design

2025-11-08

How to control access and display permissions for different user groups on AnQiCMS to achieve content monetization?

In the practice of content operation, effectively managing different users' access rights to content is a key link in realizing the commercialization of content value.Many content creators and enterprises hope to provide differentiated content services for users at different levels, such as exclusive paid membership, VIP content preview, or unlocking more in-depth information based on user levels.AnQiCMS provides a set of effective and flexible functions that help us easily meet these needs.### Core Mechanism: User Grouping and Content Permission Hierarchy AnQiCMS

2025-11-08

How to configure the thumbnail processing method of AnQiCMS to ensure the unified display effect of images on the front end?

In AnQiCMS, the unified display effect of images is crucial for the overall aesthetics and user experience of the website, especially for thumbnails.Whether it is an article list, product display, or category page, maintaining the consistent size and style of thumbnails can make the website look more professional and easier to browse.AnQiCMS provides flexible configuration options, allowing us to easily achieve this goal. ### Thumbnail location settings: Content settings are crucial To start configuring thumbnails, we need to log in to the AnQiCMS backend management interface. Find "System Settings" in the left navigation bar

2025-11-08