How to implement page skeleton reuse through inheritance in AnQiCMS templates?

Calendar 👁️ 68

In website construction and operation, we often face a challenge: how to ensure the consistency of the overall style of the website while being able to flexibly modify the specific content of each page, and at the same time, take into account the development efficiency and the convenience of later maintenance?AnQiCMS provides a powerful and intuitive template inheritance mechanism, which is the core of solving these problems.It's like creating a "master template" for your website, allowing you to easily reuse common structures and focus on content creation.

Understand the core principle of template inheritance

In simple terms, the template inheritance feature of AnQiCMS allows us to define a basic page layout, which we call a 'page skeleton' or 'parent template', and it contains the common structural elements of all web pages, such as the header, footer, navigation bar, sidebar, and so on. In this skeleton, we will use{% block ... %}Label some 'variable areas'. The child template then inherits{% extends '父模板路径' %}this skeleton and can use the same name according to its own needs.{% block ... %}Label to fill or overwrite the reserved area in the parent template. The areas not overwritten by child templates will automatically follow the content of the parent template.

This mechanism is similar to the "slide master" in presentation software: the master defines the overall layout and design, while each slide fills in the specific content.The benefits of doing so are obvious: the common elements of the website are kept uniform, while the unique content of the page can be freely customized.

Build your page skeleton: the design of the parent template.

To take advantage of template inheritance, you first need to create a basic parent template, usually namedbase.htmland place it in the root directory of the template directory. Thisbase.htmlWill carry the basic HTML structure of your website, for example<!DOCTYPE html>/<html>/<head>and<body>.

Inbase.htmlIn which, you can layout general elements:

  • Header navigation area: Typically includes Logo, main navigation, search box, etc.
  • Page Main Layout: For example, left sidebar, right content area, etc.
  • Footer Information: Copyright statement, filing number, friend links, etc.

You need to use these fixed structures in{% block ... %}Define those areas that may change on different pages. For example, you can define atitleblock for the page title, acontentblock for the main content of the page, orstyles/scriptsblocks for introducing special styles or scripts for the page.

”`twig {# template/base.html #} <!DOCTYPE html>

<meta charset="UTF-8">
{# 页面标题,子模板可重写 #}
{% block title %}<title>我的网站</title>{% endblock %} 
{# 额外meta信息,子模板可重写 #}
{% block meta %}{% endblock %} 
<link rel="stylesheet" href="/public/static/css/common.css">
{# 页面特有样式,子模板可重写 #}
{% block styles %}{% endblock %}

Related articles

How to include external HTML fragments in AnQiCMS templates?

When building website templates in AnQiCMS, we often encounter some HTML code blocks that need to be reused, such as the website header, footer, sidebar navigation, advertisement slots, or some general content modules.If each time you repeat writing these codes in different template files, it is not only inefficient but also very麻烦 to find and update them one by one when modifications are needed.

2025-11-07

What is the difference between the variable output and logic control tags in AnQiCMS templates?

In AnQiCMS template development, we often deal with two core template elements: variable output tags and logical control tags.They all serve to present dynamic content to the user, but there are essential differences in their functional positioning, syntax form, and actual effects.Understanding and mastering their similarities and differences is the key to efficiently building AnQiCMS website templates.### Variable output label (`{{ ... }}`): The direct window for data display Imagine that your website is a beautiful showcase, and the variable output label is like the merchandise displayed in the showcase

2025-11-07

How to write AnQiCMS templates according to Django template engine syntax rules?

AnQiCMS (AnQiCMS) is an efficient and customizable content management system, with its powerful template engine being the core for achieving diverse website presentations.The template engine adopted by the system is highly similar to Django's syntax rules, which allows users familiar with web development to get started faster, even beginners can gradually master the skills of template writing through the guidance of this article.

2025-11-07

How to use AnQiCMS template tags correctly to avoid common errors?

AnQiCMS provides a powerful and flexible template system, allowing the display of website content to be highly customizable.The syntax of its template tag language draws inspiration from the Django template engine and integrates the ease of use of Blade templates, making it easy for users unfamiliar with the Go language to get started.However, to fully utilize the potential of template tags and avoid common mistakes, it is crucial to understand their correct usage and potential pitfalls.### AnQiCMS Template Tag Basics: Understanding Core Syntax and Conventions AnQiCMS template files are usually formatted with `

2025-11-07

What are the application scenarios of AnQiCMS template macro functions (macro) and include tags?

In AnQiCMS template development, in order to improve code reusability and reduce maintenance costs, we often use some powerful template functions, among which 'macro function' and 'Include tag' are two great assistants.They each have unique advantages and applicable scenarios. Understanding and appropriately applying them can make template development twice as efficient and build efficient, easy-to-maintain websites.

2025-11-07

How to display the current time and formatted timestamp in AnQiCMS template?

Displaying time on the website content, whether it is the publication date of the article, the update time of the product, or the real-time loading time of the page, can greatly enhance the user experience and the timeliness of information.AnQiCMS is a content management system developed based on the Go language, which is very intuitive and flexible in handling time and dates in templates.This article will introduce you to how to display the current time and formatted stored timestamp in AnQiCMS templates.

2025-11-07

How to customize variables and assign values using (with/set) in AnQiCMS templates?

In AnQiCMS templates, flexibly customizing and using variables is the key to achieving dynamic content and efficient template reuse.AnQiCMS's template syntax borrows the characteristics of popular template engines such as Django and Blade, making variable definition and assignment intuitive and powerful.This article will deeply explore the `with` and `set` tags, helping you to better customize variables and utilize them in AnQiCMS templates.### One, the foundation of variable customization: Understanding the core of template syntax In AnQiCMS template

2025-11-07

How to use the `archiveList` tag to display the latest article list on the AnQiCMS homepage?

On AnQiCMS, the homepage is often the first window visitors use to understand the website's content.How to efficiently and dynamically display the latest articles to attract visitors to continue browsing is the key to content operation.AnQiCMS provides a flexible and easy-to-use template tag system, where the `archiveList` tag is the core tool to achieve this goal.

2025-11-07