How to conveniently introduce common header and footer code snippets in AnQiCMS templates?

Calendar 👁️ 70

In website development and operation, headers (Header) and footers (Footer) are almost indispensable parts of each page.They carry navigation, brand identification, copyright information, contact details, and other important content.However, if each page were to write these codes independently, it would not only cause a lot of redundant work, but it would also make subsequent modifications and maintenance extremely complex.Imagine if a website has hundreds of pages, and every time you need to modify the navigation menu or the footer copyright information, you have to manually adjust each one individually, how inefficient and error-prone that would be.

Fortunately, AnQiCMS provides a powerful and flexible template mechanism that can help us solve this problem elegantly.It allows us to extract common header and footer code snippets, manage them centrally, and reuse them, thereby greatly enhancing the development efficiency and maintenance convenience of the website.

Why is it crucial to manage the header and footer modularly?

The benefits of fragmenting the header and footer code and managing them centrally are obvious:

  • Improve maintenance efficiency: All header or footer modifications can be made in a single file, affecting the entire site. This greatly reduces repetitive labor and minimizes the risk of errors.
  • Ensure consistency across the entire site.: Whether it is brand vision, navigation structure, or legal statement, modular management ensures that they are consistent on all pages, improving user experience and brand professionalism.
  • Optimize team collaborationIn a project involving multiple developers, different members can focus on the development of their respective modules, reducing code conflicts and improving collaboration efficiency.
  • Improve code readability: The page template only contains core content code, making the structure clearer, easier to read and understand.
  • Beneficial for SEOAvoid excessive redundant code to allow search engine crawlers to more efficiently crawl and understand page content.

AnQiCMS uses a syntax similar to the Django template engine, which makes it very easy for content operators and developers to get started. It mainly realizes the modular management of templates through two core mechanisms: includeTags andextends.

Method one: useincludeLabel, flexible combination of code snippets

includeThe role of tags is like building blocks, pre-making commonly used small components, and then embedding them where needed. For those small pieces of code that do not constitute the overall framework of the page but appear repeatedly in multiple places, such as sidebars, breadcrumbs, independent ad blocks, and even simple headers and footers, includeIt is a very convenient choice.

Basic usage:

Suppose you have already saved the header code inpartial/header.htmlthe file, the footer code is saved inpartial/footer.htmlthe file (partialThe directory is where AnQiCMS recommends storing code snippets, and you can include them in any page template in the following way:

{# 引入公共页头 #}
{% include "partial/header.html" %}

{# 页面核心内容 #}
<main>
    <h1>这是页面的主要内容</h1>
    <p>这里是页面的详细信息...</p>
</main>

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

Pass variables:

Sometimes, the header or footer introduced may need some specific information of the current page. For example, the header may need to display the title of the current page. You can usewithkeywords to directincludePassing variables in the template:

{% include "partial/header.html" with pageTitle="当前文章详情" %}

Thenpartial/header.htmlcan be used like ordinary variables{{ pageTitle }}.

Only pass specified variables:

If you want the template you are introducing to use only the variables you explicitly pass and not inherit all the variables of the current template, you can addonlyKeyword:

{% include "partial/header.html" with pageTitle="当前文章详情" only %}

Conditional inclusion:

If a code snippet is not required on all pages or you want it to be included only when the file exists, you can useif_exists:

{# 如果 partial/sidebar.html 存在,则引入,否则忽略 #}
{% include "partial/sidebar.html" if_exists %}

includeThe tag's advantages are light and flexible, it is suitable for those relatively independent code snippets that can be freely combined on different pages or different layouts.

Method two: useextendsThe tag builds the template skeleton.

If we sayincludeIs it building a puzzle, thenextendsIt's more like designing the layout framework of a newspaper. It allows you to define a 'master' template (usually namedbase.html),which includes the common structure of a website, such as the entire HTML file framework, header, footer, sidebar, and so on, and uses{% block %}Label the areas that can be rewritten or filled by the sub-template.

Define the master templatebase.html:

<!DOCTYPE html>
<html lang="{% system with name='Language' %}">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}{% tdk with name="Title" siteName=true %}{% 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/style.css">
    {% block head_extra %}{% endblock %} {# 预留给子模板添加额外的head内容 #}
</head>
<body>
    <header>
        {% include "partial/top_nav.html" %} {# 顶部导航,可以是include进来的 #}
        <h1>{% block page_header %}默认页面标题{% endblock %}</h1>
    </header>

    <div class="container">
        {% block content %}
            {# 这里是页面的主要内容区域,由子模板填充 #}
            <p>欢迎来到 AnQiCMS 网站!</p>
        {% endblock %}
    </div>

    <footer>
        {% include "partial/footer.html" %} {# 页脚,也可以是include进来的 #}
        <p>&copy; {% now "2006" %} {% system with name="SiteName" %} All Rights Reserved.</p>
    </footer>

    <script src="{% system with name='TemplateUrl' %}/js/main.js"></script>
    {% block body_extra %}{% endblock %} {# 预留给子模板添加额外的body底部内容 #}
</body>
</html>

Inbase.htmlIn it, we use{% block title %}/{% block head_extra %}/{% block page_header %}/{% block content %}and{% block body_extra %}Defined areas that can be replaced or expanded.

Sub-templatearticle_detail.htmlInherit and override:

{% extends 'base.html' %} {# 必须是模板文件的第一行 #}

{# 重写页面标题,显示文章标题 #}
{% block title %}{{ archive.Title }} - {% system with name="SiteName" %}{% endblock %}

{# 为当前页面添加特定的CSS或JS #}
{% block head_extra %}
    <link rel="stylesheet" href="{% system with name='TemplateUrl' %}/css/article.css">
{% endblock %}

{# 重写页面大标题 #}
{% block page_header %}
    <h1>{{ archive.Title }}</h1>
    <p>发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</p>
{% endblock %}

{# 填充文章内容区域 #}
{% block content %}
    <article>
        <div>{{ archive.Content|safe }}</div>
    </article>
    <aside>
        {# 这里可以放相关文章、评论区等 #}
    </aside>
{% endblock %}

{# 为当前页面添加特定的JS脚本 #}
{% block body_extra %}
    <script src="{% system with name='TemplateUrl' %}/js/article_detail.js"></script>
{% endblock %}

ByextendsThe tag, the child template clearly inheritsbase.htmlstructure, focusing only on its unique content and style, greatly simplifying page development. Please remember, `{

Related articles

Does AnQiCMS support customizing the display style and content of 404 and 500 error pages?

In website operation, every detail of user experience is crucial, even when users accidentally visit a non-existent page (404 error) or when there are internal issues with the server (500 error), a well-designed, friendly error page can effectively reduce user loss and even guide them back to the correct path.AnQiCMS (AnQiCMS) fully understands this and provides users with a flexible way to customize these critical error pages, ensuring that the website maintains a professional and consistent user experience in any situation.### Core Feature Revelation

2025-11-07

How to flexibly display personalized field content on the front-end page after customizing the content model?

With the increasing diversity of modern website content, relying solely on traditional content types such as 'articles' or 'products' often fails to meet complex business needs.The AnQiCMS custom content model feature is specifically designed to address this challenge, allowing us to create highly customized content structures based on actual business scenarios.But how can we carefully design these personalized contents in the background and then flexibly and efficiently present them on the website front-end?This requires us to deeply understand the powerful template tag system of AnQi CMS.###

2025-11-07

How does AnQiCMS utilize modular design for template secondary development and personalized display adjustment?

AnQiCMS is an enterprise-level content management system based on the Go language, and its powerful modular design is one of its core advantages.This design concept is not only reflected in the system architecture, but also deeply affects the flexibility of secondary template development and personalized display adjustment, allowing website operators to easily create unique and highly customizable content platforms. **The Foundation of AnQiCMS Modular Design** AnQiCMS can achieve efficient and flexible template customization, which is inseparable from its underlying modular architecture.

2025-11-07

How to ensure that the encoding format of AnQiCMS template files is correct and avoid display of garbled characters on the page?

In website operation, the display of乱码is undoubtedly one of the most annoying problems, it not only severely affects user experience, but may also damage your brand image.For friends using AnQiCMS, encountering such problems, the encoding format of the template file is often the primary object to investigate.Ensure that your AnQiCMS template files use the correct encoding format, which is the basis for ensuring the normal display of website content and providing a smooth user experience.

2025-11-07

How to apply an independent template file for a specific category or single page in AnQiCMS?

In AnQiCMS, in order to make your website more personalized and flexible, you can apply independent template files to specific content categories, single pages, or even single articles.This ability allows you to design exclusive visual styles and functional layouts for different content types or important pages, greatly enhancing the customization degree of the website.AnQiCMS provides two main ways to achieve this goal: one is to rely on predefined naming conventions for automatic matching, and the other is to manually specify template files in the background.### 1.

2025-11-07

How to configure the pseudo-static rules of AnQiCMS to optimize URL structure and improve search engine friendliness?

The URL structure of a website, like a business card, not only displays to users but also is an important clue for search engines to understand the content and hierarchy of the website.A clear, concise, and semantically rich URL that can greatly enhance user experience and significantly improve the search engine optimization (SEO) of a website, helping content achieve better rankings.Our CMS is well-versed in this, built-in with powerful static feature, allowing website operators to easily configure and optimize URL structure.Why is the structure of URL important for SEO?Dynamic URLs typically contain question marks, equals signs, and various parameters, such as

2025-11-07

How to set up the 301 redirect function of AnQiCMS to avoid SEO negative impact after content adjustment?

In website operations, the continuous optimization and adjustment of content is the norm, whether it is to redesign pages, adjust the classification structure, or delete outdated content, these operations may cause the original URL of the website to change.When these changes occur, if not handled properly, they may have a negative impact on the website's search engine optimization (SEO) performance, such as traffic loss, ranking decline, and even damage to user experience.Luckyly, AnQiCMS provides a complete 301 redirect management feature that can help us effectively avoid these issues.###

2025-11-07

How to generate and manage a website's Sitemap in AnQiCMS to ensure effective crawling by search engines?

In website operation, it is crucial to ensure that search engines can efficiently crawl and index your content.A sitemap (site map) is like a detailed website navigation map, clearly telling search engines what pages are on your website, how important they are, and when they were last updated.AnQiCMS is a system focused on enterprise-level content management, which provides convenient and powerful functions for generating and managing Sitemaps, helping your website to be better understood and indexed by search engines.### AnQiCMS Automatically Generated

2025-11-07