How to implement the reuse of the common part of AnQiCMS templates (such as headers and footers)?

Calendar 👁️ 68

In AnQiCMS, efficient website content management is inseparable from a flexible and easy-to-maintain template system.When you are creating a template, you are likely to encounter such a requirement: header, footer, navigation bar, and other common elements need to appear on every page of the website.If writing the same code for each page, it is not only time-consuming and labor-intensive, but also modifying it in the future is a slight movement and the whole body is affected.Luckyly, AnQiCMS takes advantage of the powerful features of the Django template engine and provides an elegant solution, making it easy to reuse these common parts.

The core cornerstone: build the 'skeleton' of your website——{% extends %}

Imagine that every page of your website shares a basic framework, like the basic structure of a house, including the foundation, load-bearing walls, and roof. In the AnQiCMS template system, this is achieved through{% extends %}Tags implemented. You usually create a named onebase.html(Or you can name it as needed)main.html/layout.htmlThe "master" file. This file defines the basic HTML structure of the website, including common elements for all pages, such as<html>/<head>/<body>tags, as well as headers and footers.

Inbase.htmlIn Chinese, you need to use{% block 区域名称 %}and{% endblock %}tags to define areas that can be rewritten or filled by sub-templates. For example, you can define atitleblock to place the title of the page, aheaderA block to place navigation and Logo, as well as onecontentA block to carry the unique content of each page, and one morefooterA block for footer information.

A simple onebase.htmlmight 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">
    <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>
<body>
    <header>{% block header %}{% endblock %}</header>

    <main>{% block content %}{% endblock %}</main>

    <footer>{% block footer %}{% endblock %}</footer>

    <script src="{% system with name='TemplateUrl' %}/js/main.js"></script>
    {% block body_extra %}{% endblock %}
</body>
</html>

Next, when you create specific page templates (such asindex.htmlUsed for the homepage,article_detail.htmlused for article detail pages), you just need tothe first lineUse{% extends 'base.html' %}inheritbase.htmlthe defined skeleton. Then, you can rewritebase.htmldefined in{% block %}Fill in the corresponding content.

For example, yourindex.htmlMay be used like this:

{% extends 'base.html' %}

{% block title %}首页 - {% tdk with name="Title" %}{% endblock %}

{% block header %}
    {# 首页的头部内容,可能会引入一个单独的导航文件 #}
    {% include 'partial/header_nav.html' %}
{% endblock %}

{% block content %}
    <h1>欢迎来到我们的网站</h1>
    <p>这里是网站的最新内容...</p>
    {# 其他首页特有的内容 #}
{% endblock %}

{% block footer %}
    {# 首页的页脚,也可能引入公共页脚 #}
    {% include 'partial/footer_info.html' %}
{% endblock %}

By{% extends %}You only need to maintain a basic layout file, all page structure updates can be completed here, which greatly improves development and maintenance efficiency.

Fine-grained splitting: Embeddable reusable code snippet -{% include %}

In addition to the overall structure of the website, you often need to reuse smaller code snippets, such as specific navigation menus, sidebars, comment forms, contact information modules, etc. AnQiCMS through{% include %}Tags make this process extremely simple.

According to the AnQiCMS template production conventions, these reusable code snippets are usually stored in/templateIn the directory where you have created the template folderpartial/in the subdirectory. For example, you can havepartial/header_nav.htmla file to specifically define the top navigation of the website, orpartial/footer_info.htmlto include copyright and contact information.

When you need to embed these snippets in a template, just use it simply:

{# 引入顶部导航 #}
{% include 'partial/header_nav.html' %}

{# 引入页脚信息 #}
{% include 'partial/footer_info.html' %}

{% include %}The strength lies in its ability to flexibly pass data. If you want a contained file to display specific information, you can pass variables throughwithkeywords:

{# 假设header_nav.html需要一个当前页面名称变量 #}
{% include 'partial/header_nav.html' with current_page_name="关于我们" %}

Inpartial/header_nav.htmlin, you can use{{ current_page_name }}Get and display this value. You can also add if the included file may not exist,if_existsto avoid the page from crashing due to missing files:

{% include 'partial/ad_banner.html' if_exists %}

so ifad_banner.htmlIt exists, it will be introduced; if it does not exist, it will be ignored gracefully without interrupting the page rendering.

Advanced encapsulation: Functions in templates ——{% macro %}

For more complex, repetitive UI components with logical processing, AnQiCMS also provides{% macro %}A tag that allows you to define a code block similar to a function in a template.Macros can accept parameters and generate different HTML content based on the parameters.This is especially useful in scenarios where list items, card components, and other similar structures need to be rendered with different data.

For example, you can define a macro to render each item in the article list:

{# 定义在 partial/archive_macros.html #}
{% macro render_archive_item(archive) %}
    <li class="archive-item">
        <a href="{{ archive.Link }}">
            <img src="{{ archive.Thumb }}" alt="{{ archive.Title }}">
            <h3>{{ archive.Title }}</h3>
            <p>{{ archive.Description|truncatechars:100 }}</p>
            <span>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</span>
        </a>
    </li>
{% endmacro %}

Then in your article list page template, you can first import this macro and then call it:

”`html {% extends ‘base.html’ %} {% import ‘partial/archive_macros.

Related articles

How to flexibly specify independent display templates for single-page content?

AnQiCMS (AnQiCMS) provides excellent flexibility in website content management, especially in the display of single-page content, allowing us to specify completely independent display templates for different single pages.This means that your "About Us" page can have a unique layout and design, distinctly different from pages like "Contact Us" or "Terms of Service", without modifying the overall template structure of the website.This fine-grained control brings great freedom to the content presentation of the website.Many times, certain pages on our website carry important brand image displays

2025-11-08

How to use custom templates to display content for specific documents or categories?

In the daily operation of websites, the flexibility of content display is often the key to whether a website can stand out and meet the diverse needs of users.AnQiCMS (AnQiCMS) fully understands this point and provides powerful custom template features, allowing content to not only be efficiently managed but also presented in a way that conforms to brand characteristics and user habits. ## Unlock the Custom Template Ability of AnQi CMS AnQi CMS, as an enterprise-level content management system, has always focused on the customization needs of content display from the very beginning.

2025-11-08

How to design and deploy a template independently for mobile website?

With the popularity of smartphones, the user experience of mobile websites has become a key factor in determining the success of a website.A well-designed mobile website can not only effectively enhance customer satisfaction, but is also crucial for search engine optimization (SEO).AnQiCMS fully considers this requirement, providing flexible and diverse mobile template solutions, especially supporting independent design and deployment of mobile template, so that your website can show its posture on different devices.### Get to know AnQiCMS's mobile template strategy In AnQiCMS

2025-11-08

What kinds of responsive or adaptive template display modes does AnQiCMS support?

In today's mobile internet age, the user experience of websites is no longer limited to computer screens.Whether it is a smartphone, tablet, or various sizes of displays, the website must be presented in **status**.AnQiCMS understands this and therefore offers a variety of display options in the template to ensure your content reaches every visitor seamlessly. ### One, Adaptive Template Mode (Responsive) Let's talk about the most common "adaptive template" mode first.The core of this pattern lies in 'adaptability', which allows the website to 'change with no change'

2025-11-08

How to use the `system` tag to display the website name and logo on the page?

## Easily Solved: How to Gracefully Display Your Website Name and Logo on AnQiCMS Page It is crucial to display brand identity when operating a website.The website name and logo are not only the key elements for visitors to identify your brand, but also the foundation for enhancing the professionalism and trust of the website.AnQiCMS provides a直观而强大的template tag system that allows you to easily call and display these core information on any page of the website.Among them, the `system` tag is specifically designed to obtain this type of global site configuration. Next

2025-11-08

How to dynamically display the website filing number and copyright information through the `system` tag in the footer?

When operating a website, the filing number and copyright information are an indispensable part of the website. They are not only the symbol of the legitimate operation of the website but also reflect the respect for original content.For AnQiCMS users, managing this information and displaying it dynamically through the footer is a convenient and efficient operation.AnQiCMS provides an intuitive backend setting and flexible template tags, allowing you to easily meet this requirement, saving the trouble of manually modifying the code on each page.

2025-11-08

How to conveniently display the company's contact phone number and address with the `contact` tag?

In website operation, clearly and conveniently displaying the company's contact information is a key link to enhance user trust and promote business communication.Whether it is to seek cooperation, provide customer support, or simply to let visitors understand the location of the company, contact information plays a vital role.AnQiCMS fully understands this and provides us with an extremely convenient way to manage and display this information through its powerful template tag system.

2025-11-08

How to dynamically generate the page's Title, Keywords, and Description (TDK) to optimize SEO display?

In website operation, Title (title), Keywords (keywords) and Description (description) - abbreviated as TDK, are the core elements of search engine optimization (SEO).They directly affect the display of the website on the search engine results page (SERP), thereby determining the click-through rate and traffic of the website.For a content-rich website, manually setting TDK for each page is neither realistic nor efficient.

2025-11-08