How to modularize the header, footer, and other common parts in a template and reference them on each page to display uniformly?

Calendar 👁️ 70

In website operation, maintaining the unity and efficient management of website pages is the key to improving user experience and operational efficiency.For AnQiCMS (AnQiCMS) users, modularizing the header, footer, navigation bar, and other common parts not only ensures consistency in the overall visual style of the site but also greatly simplifies the maintenance and update work in the future.Strong and flexible template system provided by AnqiCMS, allowing you to easily achieve this goal.

Understand the template architecture of AnQiCMS

AnQiCMS template files are usually stored in/templatethe directory, and.htmlwith a suffix. The system design emphasizes modularity, encouraging developers and content operators to extract repetitive code snippets and form independent modules.Among the most core concepts are the 'template inheritance' (extends)and "template reference"(include)

Imagine building a house,extendsIt is like defining the basic structure diagram of a house (such as how many floors, where the living room and bedrooms are), andincludeIt is used to fill in the details of the structure diagram (such as the sofa and TV accessories in the living room). By using them properly, we can build a unified and easy-to-maintain website.

Core modular tool:extendswithinclude

  1. extends: The 'skeleton' of building a website extendsTags are used to specify a basic template for the current template to inherit its content. This is very suitable for defining the overall layout of a website, such as<head>regions, main,divThe structure, as well as the reference position of the common header and footer. Typically, we would create a namebase.html(or as recommended in the AnQiCMS documentation)bash.htmlThe file serving as the skeleton template of the website. This file contains the common HTML structure and placeholders (viablocktags defined), for example:

    • <html>and<head>Tags including character sets, viewport settings, website titles (TDK), global style sheets, and references to JavaScript files.
    • Page content<body>The general structure, such as the top navigation area, content main area, sidebar area, and bottom footer area.
    • blockis a tagextendsThe core mechanism, which defines the areas that child templates can override or extend.
  2. includeFill reusable 'component' includeTags allow you to insert the content of one template file directly into another. This is very suitable for components that appear repeatedly on multiple pages and are relatively independent, such as:

    • Website Header (Header): Typically includes Logo, main navigation, search box, etc.
    • Website Footer (Footer): Usually contains copyright information, filing number, contact information, friendship links, etc.
    • Sidebar (Sidebar): May contain hot articles, ad spaces, category lists, etc.
    • Breadcrumb navigation (Breadcrumbs): Shows the current page's position in the website structure. These reusable code snippets are usually stored in one place.partial/Under the directory, convenient for management and search.

Practice: Modularize your header, footer, and navigation.

Below, we demonstrate how to use AnQiCMS template tags and structure to modularize the common parts of a website with specific code examples.

Step 1: Create the basic layout filebase.html

In your template folder (for example/template/default/), create a file namedbase.html. This file will be the base for all pages.

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    {# 通过 tdk 标签获取页面的标题、关键词和描述。siteName=true 会自动在标题后附加网站名称 #}
    <title>{% tdk with name="Title" siteName=true %}</title>
    <meta name="keywords" content="{% tdk with name="Keywords" %}">
    <meta name="description" content="{% tdk with name="Description" %}">
    
    {# 引入全局 CSS 文件,TemplateUrl 标签获取模板静态文件地址 #}
    <link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/style.css">
    
    {# 预留一个区域供子页面添加特定的 head 内容,例如页面独有的 CSS 或 JS #}
    {% block head_extra %}{% endblock %}
</head>
<body>
    {# 引入公共的页头部分 #}
    {% include "partial/header.html" %}

    <main class="main-content">
        {# 这是主体内容区域,每个子页面将在此填充自己的独特内容 #}
        {% block content %}{% endblock %}
    </main>

    {# 引入公共的页脚部分 #}
    {% include "partial/footer.html" %}

    {# 预留一个区域供子页面添加页面底部的 JS 脚本 #}
    {% block body_extra %}{% endblock %}
</body>
</html>

Second step: Create a common component file

Create one under your template directorypartialfolder (for example `/template/

Related articles

What page adaptation modes are supported by AnQiCMS templates, and how to select and implement responsive display?

In the multi-screen era, users access websites through various devices, which has become the norm.To ensure that the website can provide a smooth and friendly experience on any device, page adaptation has become an indispensable part of website construction.AnQiCMS (AnQi Content Management System) fully considers this requirement, providing a variety of flexible template adaptation modes to help users easily cope with display challenges on different devices.

2025-11-08

How to use Django template engine syntax to display variables and logic structures?

AnQiCMS (AnQiCMS) uses a template engine syntax similar to Django in template creation. This design philosophy aims to provide content operators and developers with a powerful and easy-to-use tool, allowing them to more flexibly control the display of website content.By mastering this template syntax, you can easily display dynamic data on the website front end and control the presentation logic of content based on specific conditions.### One, the core composition of template syntax: variables and logical structures The template syntax of AnQi CMS mainly consists of two major parts

2025-11-08

How to ensure that AnQiCMS template files are encoded in UTF-8 to avoid garbled page display?

During the process of building a website with AnQiCMS, you may occasionally encounter the situation where the displayed content is garbled, especially Chinese characters.This not only affects the aesthetics and user experience of the website, but may also have a negative impact on search engine optimization (SEO).The problem of garbled characters is usually related to the inconsistent encoding format of template files, and ensuring that AnQiCMS template files are saved in UTF-8 encoding is a key step to solving this problem.Why UTF-8 Encoding is Crucial?

2025-11-08

How should AnQiCMS template files be named and organized to achieve **display effects??

In Anqi CMS, the display effect of the website is closely related to the naming and organization of the template files.A well-planned template structure not only makes the website look neat and beautiful but also greatly improves development efficiency, facilitates later maintenance, and ensures that content is presented in different scenarios. ### The Foundation of Template Files: `/template` Directory and `config.` All visual presentations of Anqi CMS website start from the `/template` directory.This is the home of all template files. Each set of independent templates

2025-11-08

How to customize the display template for specific articles, categories, or single pages to achieve personalized layout?

In website operation, providing exclusive display methods for specific content can significantly improve user experience and content marketing effectiveness.AnQiCMS (AnQiCMS) is well-versed in this field, providing flexible and diverse template customization features, allowing you to easily create a unique personalized layout for articles, categories, or single pages. The AnQi CMS realizes personalized template customization in two main ways: one is to follow specific **template file naming conventions**, the system will automatically identify and apply;Secondly, it is manually specified in the background management interface to use a custom template file.--- ### One

2025-11-08

How to implement conditional (if/else) dynamic display of content and layout in a template?

In website operations and frontend development, we often need to flexibly display content or adjust the page layout according to different situations.This dynamic ability is the core value of the conditional judgment tag (`if/else`) in AnQiCMS templates.The Anqi CMS template engine is simple and powerful, allowing us to set logic on the page like writing program code, making the website content show endless possibilities.### The Dynamic Beauty of AnQi CMS Template

2025-11-08

How to loop through a list of data in a template and display it (using for loop), supporting counting and reversing?

In Anqi CMS template, efficiently displaying list data is an indispensable part of website content operation.Whether it is to display the latest articles, product lists, category directories, or customized data sets, flexible looping through these data and fine-grained control can greatly enhance the performance and user experience of the website.The Anqi CMS provides a powerful and easy-to-use template engine, its `for` loop tag is rich in features, supporting not only basic iteration but also easily implementing counting, reversal, and more advanced operations.### Core Concept: `for`

2025-11-08

How to avoid extra blank lines when template logical tags (such as if, for) are rendered on the page?

When developing templates in AnQiCMS, we often find that even though the template code itself looks neat, the final rendered HTML page may still contain some unexpected blank lines.These blank lines do not affect the page function, but may make the HTML source code look less tidy, and even in some extreme optimization scenarios, it may bring a slight increase in file size.For users who pursue code aesthetics and concise output, how to effectively avoid these redundant blank lines is a topic worth discussing.

2025-11-08