How to reference the common header and footer files in AnQiCMS templates?

Calendar 👁️ 79

As an experienced website operations expert, I fully understand how important it is to maintain consistency in website structure and development efficiency in a content management system.Especially when using a high-efficiency and flexible system like AnQiCMS, it is reasonable to refer to common header and footer files, which can not only greatly improve the maintenance efficiency of templates, but also ensure the consistency of the overall style.Today, let's delve into how to achieve this goal in AnQiCMS templates.

AnQiCMS provides great convenience for developers and operators with its high-performance architecture based on the Go language and Django-style template syntax.It supports us in managing content in a modular way, and the common parts in the template, such as the website's navigation bar, footer information, CSS, and JavaScript references, are excellent practice points for modular design.By cleverly using the template tags provided by AnQiCMS, we can easily achieve the reference of public files.

applyincludeTags: Implementing flexible reuse of local code

In the AnQiCMS template system,includeTags are the most direct and commonly used way to refer to public code snippets.It allows us to insert small, independent template files into any main template that needs them.Imagine, the header of your website includes Logo, main navigation, search box and other elements, which almost appear on every page.If each page repeats the writing of this code, then once it needs to be modified, the workload will be huge.includeLabels are the tools to solve this problem.

Usually, we would place these reusable code snippets in the template directory.partial/In the subfolder. For example, you can create it in the directory of the current template you are usingpartial/header.htmlandpartial/footer.html.

Let's take a look at what these files might contain:

template/你的模板目录/partial/header.html

<!DOCTYPE html>
<html lang="{% system with name='Language' %}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <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/style.css">
    <!-- 引入其他公共CSS或JS文件 -->
</head>
<body>
    <header class="main-header">
        <div class="logo">
            <a href="{% system with name="BaseUrl" %}">
                <img src="{% system with name="SiteLogo" %}" alt="{% system with name="SiteName" %}">
            </a>
        </div>
        <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>
    </header>
    <main>

template/你的模板目录/partial/footer.html

    </main>
    <footer class="main-footer">
        <p>{% system with name="SiteCopyright" %}</p>
        <p><a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">{% system with name="SiteIcp" %}</a></p>
        <!-- 引入其他公共JS文件,如统计代码等 -->
        {{- pluginJsCode|safe }}
    </footer>
</body>
</html>

Next, in the main page template file, for exampleindex.html/article/detail.htmlIn wait, simply useincludeTags to introduce them:

template/你的模板目录/index.html

{% include "partial/header.html" %}

    <section class="hero-section">
        <h1>欢迎来到 {% system with name="SiteName" %}</h1>
        <p>{% tdk with name="Description" %}</p>
    </section>

    <!-- 首页的其他内容 -->

{% include "partial/footer.html" %}

Thus,header.htmlandfooter.htmlThe content will be rendered toindex.htmlIf one day you need to modify the website logo or navigation structure, you just need to modifyheader.htmlA file, all pages that reference it will be updated accordingly, greatly enhancing maintenance efficiency.

Furthermore,includeTags also support some advanced usage:

  • if_existsIf you are not sure whether a file included exists, you can use{% include "partial/sidebar.html" if_exists %}, even if the file does not exist, it will not cause the page to report an error.
  • withYou can pass additional variables to the included template. For example,{% include "partial/ad.html" with ad_type="banner" ad_id="123" %}, inad.htmlYou can use it in{{ ad_type }}and{{ ad_id }}.

applyextendsTag: Build the global template skeleton

When you need to define a unified overall layout for a website, such as all pages sharing the same basic HTML structure, CSS and JS file references, while only the content of specific areas changes,extendsThe label is very powerful. It implements the concept of template inheritance, like the master page in Office software, where you define a "skeleton" template, and other sub-templates fill in and modify it on this basis.

We usually create a file namedbase.htmlas the basic framework of a website. In this file, we define the common structure of the page and throughblockLabels delineate areas where child templates are allowed to override or extend.

template/你的模板目录/base.html

`html &lt;!DOCTYPE html&gt;

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% block head_meta %}
<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 %}
{% endblock head_meta %}

{% block head_css %}
<link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/style.css">
<!-- 全局CSS引用 -->
{% endblock head_css %}

{% block head_js %}
<!-- 全局JS引用 -->
{% endblock head_js %}

{% block header %}
<header class="main-header">
    <div class="logo">
        <a href="{% system with name="BaseUrl" %}">
            <img src="{% system with name="SiteLogo" %}" alt="{% system with name="SiteName" %}">
        </a>
    </div>
    <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>
</header>
{% endblock header %}

<main>
    {% block content %}
    <!-- 子模板将在这里填充具体内容 -->
    {% endblock content %}
</main>

{% block footer %}
<footer class="main-footer">
    <p>{% system with name="SiteCopyright" %}</p>
    <p><a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">{% system with name="SiteIcp" %}</a>

Related articles

How to determine if a variable is empty or execute conditional logic in AnQiCMS templates?

As an experienced website operation expert, I am well aware of how crucial precise control of template logic is when building and maintaining a high-efficiency, user-friendly website.Especially when using a content management system like AnQiCMS, which is based on Go language and focuses on performance and flexibility, it is possible to flexibly judge the status of variables and execute conditional logic according to different situations, which is the foundation for ensuring the correct display of content, avoiding page errors, and improving the user experience.The template engine of AnQi CMS uses a tagging style similar to Django

2025-11-07

How to use a for loop to iterate over a data list in AnQiCMS template?

## Master AnQiCMS Templates: The Art of Using `for` Loops to Traverse Data Lists Hello, all AnQiCMS operation partners!In daily website content operations, we often need to display a series of data, whether it is the latest article list, product category navigation, or comment details, it is inseparable from the traversal and display of the data set.AnQiCMS as an efficient and flexible content management system, deeply understands the importance of templates in content presentation. Today

2025-11-07

What control flow tags does AnQi CMS template language support (such as if, for)?

## AnQi CMS template language: if, for, allowing flexible control of content display As an experienced website operation expert, I deeply understand the importance of a flexible and efficient content management system to a company.AnQiCMS (AnQiCMS) stands out in the content operation field with its high-performance architecture based on the Go language and its highly customizable nature.In daily content display and page construction, control flow tags in template languages, such as `if` and `for`, are undoubtedly the core tools for us to achieve dynamic and personalized content presentation. Today

2025-11-07

How to get the thumbnail or Logo of Tag in AnQiCMS template?

As an experienced website operation expert, I am well aware that how to flexibly use template tags to display a variety of content in a content management system is the key to enhancing the attractiveness and user experience of a website.AnQiCMS with its concise and efficient architecture and powerful template functions, provides us with great convenience.Today, let's delve into a common and practical need in content operation: how to obtain the thumbnail or Logo of a Tag in the AnQiCMS template.

2025-11-07

How to implement nested category display in AnQiCMS template

As an experienced website operations expert, I am well aware of the value of a flexible and efficient content management system for the success of a website.AnQiCMS (AnQiCMS) relies on its high-performance architecture based on the Go language and the Django-style template engine, bringing great convenience and infinite possibilities to content display.Today, let's delve into a very common requirement in actual operation: how to implement nested display of multi-level categories in AnQiCMS templates, making your website structure clearer and enhancing the user experience.

2025-11-07

What are the common filters supported by AnQiCMS for processing template data (such as string truncation, date formatting)?

In the Anqi CMS template world, data is not just raw text or numbers, they are the foundation of excellent website content.However, the original data often needs to be polished before it can present itself in a certain posture to the user.At this time, the template filter has become an indispensable helper to us.They are like a Swiss Army knife for data processing, capable of performing various transformations, formatting, and refining template variables, bringing new luster to the data and meeting all kinds of display needs.As an experienced website operation expert

2025-11-07

How to format timestamp display in AnQiCMS template?

As an experienced website operation expert, I am well aware that the timeliness and presentation of content are crucial to user experience.In an efficient content management system like AnQiCMS, even a seemingly simple date display contains operational wisdom to enhance the professionalism and readability of the website.Today, let's delve into how to beautifully transform the original timestamps into user-friendly date formats in the AnQiCMS template.

2025-11-07

How to define and use variables (set, with) in AnQiCMS templates?

Mastery in AnQiCMS templates: Unlocking the Power of Variables (Deep Analysis of set and with) As an experienced website operations expert, I know that a flexible and efficient content management system is crucial for the success of a website.AnQiCMS with its concise and efficient architecture and support for Django template engine syntax, provides us with great convenience.In template development, being proficient in variable definition and assignment is undoubtedly the key to enhancing template flexibility and maintainability.

2025-11-07