How to include common header, footer, and other code snippets in AnQiCMS templates to unify the display style?

Calendar 👁️ 60

How to include common header, footer, and other code snippets in AnQiCMS templates to unify the display style?

When building a website, whether it is a personal blog, a corporate website, or a marketing site, maintaining consistency in page visuals and functionality is crucial.Imagine if each page's top navigation, footer copyright information, and even the sidebar were all different, the user experience would suffer greatly, and the professionalism of the website would also be greatly reduced.AnQiCMS provides a powerful and flexible template engine that can help us easily achieve unified style management of the website, the core practice of which is to modularize and uniformly introduce common header, footer, and other frequently used code snippets.

In this way, we can not only ensure that each page of the website has a consistent brand identity and operational experience, but also greatly improve development efficiency and the convenience of later maintenance.When the navigation structure, contact information, or copyright statement of a website needs to be updated, we only need to modify one piece of code to synchronize the update of all related pages, thus avoiding the麻烦 of modifying each page one by one.

How should we operate specifically in AnQiCMS?

Understand the basics of AnQiCMS template engine

AnQiCMS's template engine adopts a syntax style similar to Django, which is very friendly for friends familiar with template development. Its template files are usually written in.htmlwith suffix, and stored uniformly in/templatedirectory. In the template file, we mainly use two types of markers:

  • double curly braces{{ 变量名 }}: is used to output variable content, such as website name, article title, etc.
  • curly brace followed by percent sign{% 标签名 参数 %}used for controlling logical flow, such as conditional judgment, loop traversal, and importing other template files.

To achieve the unity of page style, we mainly use the two core features of the template engine:Template inheritance (extendsandblock)andTemplate inclusion (include).

Template inheritance: building the skeleton of the website

Template inheritance is the cornerstone of building a unified website layout. It allows us to create a "basic template" (usually namedbase.htmlor based ondesign-director.mdrecommend naming in thebash.htmlwhere it defines the overall structure of the website, such as the HTML document declaration,headsections, header, main content area, footer, etc. The variable parts of the basic template are then accessed throughblockDefine the tag.

For example, a basic template (base.html) might look like this:

<!DOCTYPE html>
<html lang="{% system with name='Language' %}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    {# 引入页面的TDK信息 #}
    <title>{% tdk with name="Title" siteName=true %}</title>
    <meta name="keywords" content="{% tdk with name="Keywords" %}">
    <meta name="description" content="{% tdk with name="Description" %}">
    {%- tdk canonical with name="CanonicalUrl" %}
    {%- if canonical %}
    <link rel="canonical" href="{{canonical}}" />
    {%- endif %}
    
    {# 引入公共样式表 #}
    <link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/main.css">
    {% block head_extra %}{% endblock %} {# 预留额外头部内容的区块 #}
</head>
<body>
    <header id="header">
        {% block header %}
            {% include "partial/header.html" %} {# 引入公共页头片段 #}
        {% endblock %}
    </header>

    <main id="main-content">
        {% block content %}{% endblock %} {# 页面主体内容的区块,由子模板填充 #}
    </main>

    <footer id="footer">
        {% block footer %}
            {% include "partial/footer.html" %} {# 引入公共页脚片段 #}
        {% endblock %}
    </footer>

    {# 引入公共脚本文件 #}
    <script src="{% system with name="TemplateUrl" %}/js/main.js"></script>
    {% block scripts_extra %}{% endblock %} {# 预留额外脚本的区块 #}
</body>
</html>

in thisbase.htmlIn it, we definedtitle/keywords/descriptionThese TDK information through{% tdk %}Dynamically retrieve and use tags{% system with name="TemplateUrl" %}Get the static resource path of the template, ensure that styles and scripts can be loaded correctly. The key is{% block header %}/{% block content %}and{% block footer %}Labels, they are areas that child templates can override and fill in.

Any page that wants to maintain a consistent style, such as the home page (index.html) or article detail page (archive/detail.html) Just use it at the top of the file{% extends "base.html" %}Inherit this basic template and then fill or override as neededblockAs defined in it.

For example, an article detail page (archive/detail.html): May be inherited and filled like this:

{% extends "base.html" %}

{% block head_extra %}
    <link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/detail.css">
{% endblock %}

{% block content %}
    <div class="container article-detail">
        <h1>{% archiveDetail with name="Title" %}</h1>
        <div class="meta-info">
            <span>发布日期: {{ stampToDate(archive.CreatedTime, "2006-01-02") }}</span>
            <span>分类: <a href="{% categoryDetail with name='Link' %}">{% categoryDetail with name='Title' %}</a></span>
        </div>
        <article class="content">
            {%- archiveDetail archiveContent with name="Content" %}
            {{ archiveContent|safe }}
        </article>
        {# 显示上一篇/下一篇文章 #}
        <nav class="pagination-nav">
            {% prevArchive prev %}
            {% if prev %}
                <a href="{{ prev.Link }}">上一篇: {{ prev.Title }}</a>
            {% else %}
                <span>没有了</span>
            {% endif %}
            {% endprevArchive %}
            {% nextArchive next %}
            {% if next %}
                <a href="{{ next.Link }}">下一篇: {{ next.Title }}</a>
            {% else %}
                <span>没有了</span>
            {% endif %}
            {% endnextArchive %}
        </nav>
    </div>
{% endblock %}

{% block scripts_extra %}
    <script src="{% system with name="TemplateUrl" %}/js/detail.js"></script>
{% endblock %}

As can be seen, the article detail page focuses only on its unique 'content' part, while the header, footer, and other common elements arebase.htmlresponsible for introducing and managing.

Templates include: modular code snippets

includeTags are used to introduce smaller, independent, reusable code snippets. They are very suitable for those in multipleblockMay appear, or not part of the overall layout but functionally independent modules, such as navigation menus, sidebars, copyright information blocks, contact card, etc. AnQiCMS recommends placing these fragments inpartial/directory.

Abovebase.htmlAs an example, it goes through{% include "partial/header.html" %}and{% include "partial/footer.html" %}It introduced separate header and footer files.

A common header (partial/header.html) Example:

<div class="top-bar">
    <div class="container">
        欢迎访问{% system with name="SiteName" %}!
        电话:{% contact with name="Cellphone" %}
    </div>
</div>
<div class="main-header">
    <div class="container">
        <a href="{% system with name="BaseUrl" %}" class="logo">
            <img src="{% system with name="SiteLogo" %}" alt="{% system with name="SiteName" %} Logo">
        </a>
        <nav class="main-nav">
            {% navList navs %}
            <ul>
                {%- for item in navs %}
                <li class="{% if item.IsCurrent %}active{% endif %}">
                    <a href="{{ item.Link }}">{{item.Title}}</a>
                    {%- if item.NavList %}
                    <dl>
                        {%- for inner in item.NavList %}
                            <dd class="{% if inner.IsCurrent %}active{% endif %}">
                                <a href="{{ inner.Link }}">{{inner.Title}}</a>
                            </dd>
                        {% endfor %}
                    </dl>
                    {% endif %}
                </li>
                {% endfor %}
            </ul>
            {% endnavList %}
        </nav>
    </div>
</div>

And the common footer (partial/footer.html) Example may include:

`html

Related articles

How to use the loop (for) tag to traverse and display data lists in AnQiCMS templates?

In AnQiCMS template development, mastering how to effectively display dynamic data lists is the key to building a feature-rich website.Whether it is the latest article, product display, or category navigation, all of these rely on the loop mechanism in the template.AnQiCMS's template system borrows the simplicity and power of Django's template engine, with the `for` loop tag being the core tool for data traversal.### Understanding the basics of `for` loop tags When we need to repeatedly display a series of structurally similar data on the page, the `for` loop comes into play

2025-11-08

How to use conditional judgment (if/elif/else) logic to control the display status of content in AnQiCMS templates?

In the template development of Anqi CMS, effectively controlling the display status of content is the key to achieving high customization and an excellent user experience.Conditional logic, especially the `if`/`elif`/`else` statements, injects intelligence into the template, allowing it to present information flexibly based on different data or situations.A deep understanding and proficient application of these logic will make your website content more dynamic and interactive.

2025-11-08

How to configure AnQiCMS to implement adaptive, code adaptation, or PC+Mobile independent site display mode?

How can websites provide a smooth and consistent user experience across different screen sizes in today's multi-device co-existing internet environment, which is a core challenge for operators.AnQiCMS is well-versed in this field, providing users with flexible and diverse website display mode configurations. Whether your needs are simple and efficient or finely customized, you can find the appropriate solutions. The Anqi CMS currently supports three mainstream website display modes: adaptive mode, code adaptation mode, and PC+Mobile independent site mode.

2025-11-08

What grammar structures does the AnQiCMS template engine support to control the dynamic display of content?

In AnQiCMS, the template engine plays a core role, responsible for displaying the dynamic data obtained from the backend in the layout and style we preset to the visitors.Understanding the supported syntax structure is like mastering a language to 'talk' with the website content, which allows us to flexibly control the display of information, thereby creating highly personalized and dynamic website experiences.AnQiCMS's template engine adopts a syntax style similar to the Django template engine, which makes it easy for Go language developers as well as users familiar with other template languages to master easily

2025-11-08

What naming conventions do AnQiCMS template files support to customize the display of specific pages or categories?

In AnQiCMS, in order to achieve personalized display of website pages or categories, we do not always have to write complex logic from scratch. The system provides a very flexible and intuitive naming convention for template files.These conventions allow us to create template files with specific names, enabling the system to automatically recognize and apply them to customize the display of different content types.First, understanding the basic structure of the template file is crucial.All template files are stored in the `/template` directory and use `.html` as the file extension.For example, a set named

2025-11-08

How does the `system` tag display the website name, Logo, record number, and copyright information on the AnQiCMS website?

When building and operating a website, the website's name, logo, filing number, and copyright information are important elements that constitute the professional image and legality of the website.They are not only the identity of the website, but also the foundation of user trust.For users of AnQiCMS, the system provides a convenient and efficient way to manage and display these core information, which is through the powerful `system` tag.This article will delve into how to use the `system` tag on the AnQiCMS website to flexibly display the website name, Logo, filing number, and copyright information

2025-11-08

How does the `contact` tag display contacts, phone numbers, addresses, and social media links on the AnQiCMS page?

In website operation, clearly and accurately displaying contact information is crucial for building user trust and promoting communication and interaction.AnQiCMS provides a very convenient way to manage and display this information, that is, through the `contact` tag.This tag helps us easily call the preset contact name, phone number, address, and even social media links on any page of the website, without hard coding, greatly improving content management efficiency.### First step: Configure contact information in the AnQiCMS backend Before we start using `contact`

2025-11-08

How to dynamically generate and display the page's Title, Keywords, and Description in AnQiCMS using the `tdk` tag?

In website operation, Title, Keywords, and Description (abbreviated as TDK) are the foundation of Search Engine Optimization (SEO).They not only describe the page content to search engines, but also directly affect the click intention of users on the search results page.AnQiCMS as a content management system focused on, deeply understands the importance of TDK, and therefore provides a powerful and flexible mechanism that allows users to easily dynamically generate and finely manage the website TDK.### How to manage TDK in AnQiCMS

2025-11-08