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

Calendar 👁️ 60

It is crucial to maintain consistency in page layout, improve development efficiency, and reduce maintenance costs in website construction and content management.AnQiCMS as an efficient content management system provides a flexible template mechanism, allowing us to easily reuse common code snippets across different pages, such as the website header navigation, footer information, or sidebar.

This article will delve into how to elegantly introduce public header and footer code snippets in AnQiCMS templates, helping you better organize template files and improve the overall maintenance efficiency and user experience of the website.

AnQiCMS template working principle overview

AnQiCMS template engine is similar to the Django framework template syntax, it allows us to use.htmlFile as a template, and use specific tags to control the output and logic. Among them, double curly braces{{变量}}Used to output variable content, while single curly braces and percentage signs{% 标签 %}It is used to define conditional judgments, loop controls, and other operations such as importing template files.

Understood the basics, we can better utilize its powerful features to manage public code.

Organize the directory structure of public code snippets

In order to better manage the common code in the template, AnQiCMS encourages us to adopt a clear directory structure. Usually, you will be able to/templateFind the theme folder you are currently using in the directory (for exampledefault)。Inside this theme folder, we can create some directories and files specifically for storing common code snippets:

  1. Basic layout file (bash.htmlorbase.html):
    • In the root directory of the theme folder, you can create a file namedbash.htmlorbase.htmlThe file. This file will serve as the "master" or "skeleton" of your website, containing the common HTML structure of all pages, such as<html>/<head>/<body>tags, as well as the division of the main content area.
    • The document specifies,bash.htmlwhich can be the inherited part of the header, footer, and other parts of each page.
  2. Code snippet directory (partial/):
    • Create a folder named inside the theme folder,partial/The subdirectory. This directory is used to store some reusable, independent page components, such as:
      • header.html(website header navigation, Logo, etc.)
      • footer.html(Website footer copyright information, friend links, etc.)
      • sidebar.html(Sidebar content)
      • breadcrumb.html(Breadcrumbs navigation) etc.

In this way of organization, our template structure will become clearer and easier to manage.

Introduce the core tag of the public code:includeandextends

AnQiCMS template engine provides two main tags for code reuse:includeandextendsThey each have different application scenarios.

1. UseincludeTag: Insert an independent fragment

includeThe tag is used to insert the content of an external template file into the specified position of the current template.It is suitable for those independent, plug-and-play components, such as when you want to display the same header or footer content on multiple pages, or a standard sidebar.

Basic usage:

Assuming you have alreadypartial/created in the directoryheader.htmlandfooter.html. You can introduce them like this on any page:

{# 引入头部代码片段 #}
{% include "partial/header.html" %}

<main class="page-content">
    {# 页面具体内容 #}
</main>

{# 引入尾部代码片段 #}
{% include "partial/footer.html" %}

Here"partial/header.html"It is a path relative to your theme root directory.

Advanced usage:

  • if_exists: Safe importIf you are not sure if the file to be imported exists, you can useif_existskeywords, so that even if the file does not exist, the template will not report an error, but will simply ignore the import.
    
    {% include "partial/header.html" if_exists %}
    
  • withPass variableSometimes, the segment you introduce may need some specific variables from the parent template. You can usewithkeywords to pass variables to the included template.
    
    {% include "partial/header.html" with pageTitle="当前页面标题" %}
    
    Inheader.htmlin, you can use{{ pageTitle }}To retrieve this variable.
  • only: Limit the scope of the variableIf you only want the template included to access the variables you pass throughwithInstead of inheriting all the variables from the parent template, you can useonlykeywords.
    
    {% include "partial/header.html" with pageTitle="当前页面标题" only %}
    

2. UseextendsLabel: Build the page skeleton

extendsThe label is used to implement template inheritance, it allows you to create a basic layout (usuallybase.html),where the overall structure of the page and some reusable content areas (throughblocktag definition). Other pages can inherit this basic layout and then fill or override theseblockArea. This is very useful for building websites with a unified layout.

Basic usage:

First, create yourbase.htmlfile as the common layout of the website:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    {% block head_meta %}
    {# 页面特有的 meta 标签或 CSS 链接 #}
    {% endblock %}
    <title>{% block title %}默认网站标题{% endblock %}</title>
    {# 引入公共 CSS 文件,通常放置在 /public/static/your_theme/css 目录下 #}
    <link rel="stylesheet" href="{% system with name='TemplateUrl' %}/css/style.css">
    {% include "partial/head_custom.html" if_exists %} {# 可选:引入自定义的头部代码,如统计代码等 #}
</head>
<body>

    {% include "partial/header.html" %} {# 引入头部代码片段 #}

    <div class="container">
        {% block content %}
        {# 这里是每个页面具体内容区域,等待子模板填充 #}
        {% endblock %}
    </div>

    {% include "partial/footer.html" %} {# 引入尾部代码片段 #}

    {# 引入公共 JS 文件 #}
    <script src="{% system with name='TemplateUrl' %}/js/main.js"></script>
    {% block scripts %}
    {# 页面特有的 JavaScript 代码 #}
    {% endblock %}
</body>
</html>

Then, in your specific page template (such asindex/index.htmlorarchive/detail.htmlIn it, you can inheritbase.htmlAnd fill in the content:

"twig {# Must be the first line of the template file #} {% extends 'base.html' %}

{% block title %}My Home Page - {% endblock %} {# Override the default title, it will be appended before the default title in base.html #}

{% block content %}

<h1>欢迎来到我的网站</h1>
<p>这是首页的专属内容。</p>

Related articles

How to use AnQiCMS built-in tags to retrieve and display system configuration information (such as website name, Logo)?

It is crucial to maintain consistency and ease of updates in website operation and management.AnQiCMS as an efficient content management system fully considers this point, providing powerful built-in tag functions, allowing the global configuration information of the website (such as website name, Logo, filing number, etc.) to be easily dynamically obtained and displayed in the front-end template.This means you do not need to hardcode, just make a change in one place in the background, and all pages referencing this information will update automatically, greatly enhancing the maintainability and operational efficiency of the website.

2025-11-07

How does AnQiCMS improve website page loading speed and display experience through static caching?

In the digital age, the loading speed of websites has become a key indicator of user experience and search engine rankings.A website that responds quickly can effectively enhance user satisfaction, reduce the bounce rate, and bring better exposure to the content.AnQiCMS is a content management system focused on high performance and efficiency, and the static caching mechanism plays a crucial role in improving website loading speed.

2025-11-07

How to ensure that AnQiCMS multilingual content switches and displays correctly on the front end?

Today, making a website support multilingual switching and display has become an important link for many enterprises to expand into the international market.AnQiCMS with its flexible architecture provides an efficient way to achieve this goal.This article will deeply explore how to configure and manage multilingual content in AnQiCMS, ensuring that they can be correctly switched and displayed on the front-end website, thus providing a high-quality browsing experience for users of different languages.

2025-11-07

How does AnQiCMS optimize the URL structure through pseudo-static rules to improve search engine results display?

In today's digital marketing environment, a website's URL structure is not just an access address, but also a key element for user experience and search engine optimization (SEO).A clear and meaningful URL can enhance user trust and help search engines better understand the content of the page, thereby improving the display and ranking of the website in search results.AnQiCMS (AnQi Content Management System) fully understands this, and therefore its design in the field of pseudostatic rules provides powerful tools for website operators to optimize URL structure and stand out in search engines.

2025-11-07

How does AnQiCMS achieve adaptive display for PC and mobile pages?

In today's era of coexistence of multiple devices, whether a website can present a good user experience on different screen sizes is directly related to its influence and commercial value.Whichever device the user accesses, whether it is a desktop computer, laptop, tablet, or smartphone, they all expect to see a page with clear layout and easy operation.AnQiCMS (AnQiCMS) is well aware of this core requirement and therefore integrated a variety of flexible adaptive display strategies from the outset to ensure that your website can be presented in **status** on any device.AnQi CMS provides three main website display modes

2025-11-07

How to add recommended attributes to AnQiCMS documents to affect the display sorting order on the front page list and detail pages?

In Anqi CMS, the recommended attribute is a very practical feature in content operation.It can help us classify content for marking, which in turn affects how the content is displayed on the front list or detail page, such as highlighting, prioritizing sorting, and even changing their layout style.Understand and make good use of these properties, which will make your website content management more flexible and efficient. ### One, set recommended properties for documents in the background To add recommended properties to a document, you first need to log in to the AnQi CMS backend management interface. 1. Enter the document editing page

2025-11-07

How to automatically extract document content to generate a summary or thumbnail for list display in AnQiCMS?

How to present content in a list page in an attractive way while not requiring a lot of manual work in website operation?This is often a problem that troubles operators. AnQiCMS understands this pain point and provides us with a set of effective solutions through its powerful automation features, especially excelling in the automatic generation and application of document summaries and thumbnails.### Automatically extract document summaries, say goodbye to manual writing Imagine that after each article is published, you need to manually write a concise summary, what a time-consuming task that is

2025-11-07

How to use the AnQiCMS Tag feature to associate and display related documents?

In today's increasingly complex content management, how to efficiently organize and present website content, allowing users to easily find the information they are interested in, while also improving the website's search engine performance, is a problem that every website operator is thinking about.AnQiCMS provides a series of powerful features to meet these challenges, among which, the Tag feature is undoubtedly a great tool for connecting and enriching content relationships.Tag, in the literal sense, is like various small tags attached to articles or products, which can cross traditional classification boundaries and bring together topics with the same theme

2025-11-07