In AnQi CMS template, how to modularly reuse the common header and footer code?

Calendar 78

In the daily operation of website, efficiency and maintainability are one of the key factors that determine the success or failure of the project.How to efficiently manage common website elements such as header navigation, footer copyright information, and so on in a content management system (CMS) is a problem that every website operations expert will think about.AnQiCMS (AnQiCMS) provides us with an elegant solution with its flexible template engine and modular design concept.

This article will delve into how to efficiently reuse common header (header) and footer (footer) code in the AnQiCMS template through modular strategies, thereby improving the development efficiency of the website, reducing maintenance costs, and ensuring consistency of the website content.

The core advantage of AnQiCMS template engine

AnQiCMS's template system is based on syntax similar to Django and Blade, which allows developers familiar with these template languages to quickly get started.The core lies in the design philosophy that 'everything can be modularized'.In AnQiCMS template conventions, we can clearly see the emphasis on common code and code snippets.The root directory of the template file is/templateEach template theme has an independent directory under it. For common headers and footers, the system recommends placing them inbash.htmlThis file is for global reference, while smaller code snippets like sidebars, breadcrumbs, and so on are recommended to be stored inpartial/In the catalog. This structured organization method is the foundation for the implementation of template modular reuse.

The main tools for implementing the modular reuse of AnQiCMS templates are two powerful tags:includeandextends. They play different roles, work together, and can build a flexible and easy-to-maintain website structure.

include: Flexible insertion of code snippets.

includeThe role of the tag is to insert an independent, reusable code snippet into any position in the current template.You can imagine it as a small brick in a Lego set, which can be freely added to any large structure without changing the shape of the entire structure.

In AnQiCMS, if you have a navigation menu, an advertisement space, or a short footer contact information module, these modules may appear on multiple pages of the website, but they do not define the overall page layout structure. At this point,includeit comes in handy.

How to use:Assuming we have a public header navigation bar, we can create a file namedpartial/header_nav.htmlwhich contains all the HTML code and AnQiCMS tags of the navigation bar (for example, usingnavListLabels to dynamically generate navigation links).

<!-- partial/header_nav.html -->
<header class="main-header">
    <div class="container">
        <a href="/" class="logo">
            <img src="{% system with name="SiteLogo" %}" alt="{% system with name="SiteName" %}">
        </a>
        <nav class="main-nav">
            <ul>
                {% navList navs %}
                    {% for item in navs %}
                        <li class="{% if item.IsCurrent %}active{% endif %}">
                            <a href="{{ item.Link }}">{{item.Title}}</a>
                            {% if item.NavList %}
                                <ul class="sub-menu">
                                    {% for subItem in item.NavList %}
                                        <li><a href="{{ subItem.Link }}">{{ subItem.Title }}</a></li>
                                    {% endfor %}
                                </ul>
                            {% endif %}
                        </li>
                    {% endfor %}
                {% endnavList %}
            </ul>
        </nav>
    </div>
</header>

In any page template that needs this navigation bar, you simply useincludeTags:

<!-- index.html 或 detail.html -->
<body>
    {% include "partial/header_nav.html" %}
    <main>
        <!-- 页面主要内容 -->
    </main>
    <!-- 其他页面部分 -->
</body>

includeThe tag also supports some advanced usage, such as, you can throughwithKeyword to pass local variables to the included template or useif_existsTo avoid errors caused by the file not existing

extends: A tool for building the page skeleton

withincludedifferent,extendsTags are used to establish inheritance relationships between templates, defining the overall structure or 'skeleton' of a page. Usually, we would create a namedbase.htmlorlayout.htmlThe master template that includes all shared HTML structure, CSS, JavaScript references, and some content that can be replacedblockarea.

How to use:First, we create a basic layout file, for examplebase.html:

<!-- base.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>{% block title %}{% tdk with name="Title" siteName=true %}{% endblock %}</title>
    <meta name="keywords" content="{% block keywords %}{% tdk with name="Keywords" %}{% endblock %}">
    <meta name="description" content="{% block description %}{% tdk with name="Description" %}{% endblock %}">
    <link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/main.css">
    {% block head_extra %}{% endblock %} {# 允许子模板插入额外的头部内容 #}
</head>
<body>
    {% block header %}
        {# 默认头部内容,可以在这里include一个公共头部文件 #}
        {% include "partial/header_nav.html" %}
    {% endblock %}

    <main id="content">
        {% block content %}
            {# 留给子模板填充主要内容 #}
            <p>这里是默认内容。</p>
        {% endblock %}
    </main>

    {% block footer %}
        <footer class="main-footer">
            <div class="container">
                <p>&copy; {% now "2006" %} {% system with name="SiteName" %}. All Rights Reserved.</p>
                <p>{% system with name="SiteCopyright"|safe %}</p>
                <nav class="footer-nav">
                    <ul>
                        {% navList navs with typeId=2 %} {# 假设typeId=2是底部导航 #}
                            {% for item in navs %}
                                <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
                            {% endfor %}
                        {% endnavList %}
                    </ul>
                </nav>
            </div>
        </footer>
    {% endblock %}

    <script src="{% system with name="TemplateUrl" %}/js/main.js"></script>
    {% block body_extra %}{% endblock %} {# 允许子模板插入额外的JS脚本 #}
</body>
</html>

Next, any other page templates, such as the homepageindex.htmlOr article detail pagedetail.htmlcan inherit thisbase.html:

<!-- index.html -->
{% extends "base.html" %}

{% block title %}
    安企CMS - 首页 - {% system with name="SiteName" %}
{% endblock %}

{% block content %}
    <h1>欢迎来到安企CMS网站</h1>
    <p>这里是首页的独有内容。</p>
    {# 首页可能包含更多独特的模块,可以继续include其他partial文件 #}
{% endblock %}

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

It should be noted that,{% extends %}The label must be the first label in the template, and there must be no other content before it, including spaces or new lines.Otherwise, the AnQiCMS template engine will not be able to correctly parse the inheritance relationship.

The practice of modular reuse**

CombineincludeandextendsWe can build a highly modular and maintainable AnQiCMS template:

  1. **Define the Master Layout (Master Layout)

Related articles

Does AnQiCMS's Tag feature provide an intelligent recommendation mechanism similar to 'Recommended Tag' or 'Related Tag'?

As an experienced website operations expert, I am well aware of the core role of tags (Tag) in content management and SEO optimization.A high-efficiency tagging system can not only help users find the content they are interested in quickly, but also improve the efficiency of search engine crawling and the overall weight of the website.AnQiCMS is a system focused on enterprise-level content management, emphasizing efficiency and scalability. The implementation of its Tag function, as well as whether it integrates an intelligent recommendation mechanism, is naturally a focus for content operators.Today, let's delve deeply into the Tag feature of AnQiCMS

2025-11-07

How does AnQiCMS handle the associated Tag of a deleted document? Will these Tags be automatically cleaned up?

As an experienced website operations expert, I know the importance of content management systems (CMS) in daily operations.AnQiCMS with its efficient and flexible features has become our excellent content management assistant.Today, let's delve deeply into a question that is often overlooked in content operation but is crucial: what happens to the Tag (label) associated with a document deleted in AnQiCMS?Will these Tags be automatically cleaned by the system?

2025-11-07

Does using a large number of tags affect the performance and loading speed of the AnQiCMS website?

In website operation, Tag tags are undoubtedly a tool for content organization and SEO optimization.However, many operators have a question in their hearts: Will the extensive use of Tag tags slow down the performance and loading speed of the website like an invisible shackle?Especially for AnQiCMS (AnQiCMS) users who pursue the ultimate user experience and search engine optimization, this is an issue worth pondering over.Today, as an experienced operation expert, I will deeply analyze the performance of AnQiCMS in this aspect.

2025-11-07

How to ensure that the AnQiCMS Tag custom URL remains unique throughout the entire site?

As an experienced website operations expert, I know that every URL carries the value of the website content and the expectations of search engines.In a highly efficient and flexible content management system like AnQiCMS, fine-grained operation is particularly important, especially for Tag (tag) elements that carry the intent of content aggregation and SEO, the uniqueness management of its custom URL is an issue that we must understand and handle properly.Why is the uniqueness of Tag custom URL crucial?In AnQiCMS, Tag is used as a powerful content organization method

2025-11-07

How to ensure that the template file is included when using the `include` tag to avoid page errors?

As an experienced website operations expert, I fully understand how important modularization and reusability are in the construction and maintenance of websites.AnQiCMS (AnQiCMS) leverages its efficient architecture based on the Go language and Django-like template engine, providing great flexibility for content display.Among them, the `include` tag is one of the core functions for implementing template reuse.However, powerful tools also need to be used with caution, otherwise they may bring unexpected 'surprises' to the website - such as page errors caused by the non-existent template files. Today

2025-11-07

How to pass specific variables to an included sub-template without sharing all variables of the parent template?

As an experienced website operations expert, I know that modularization and reusability of templates are crucial in building and maintaining websites.AnQiCMS, with its powerful Django template engine syntax, provides us with great flexibility, allowing us to easily divide the page into manageable small pieces, also known as sub-templates.

2025-11-07

In Anqi CMS template, how can the `include` tag achieve more efficient component-based development and maintenance?

As an experienced website operations expert, I fully understand that in the current era of rapid iteration and high efficiency, the efficiency and maintainability of template development are of great importance to the success of the project.AnQiCMS (AnQiCMS) provides a solid foundation for us with its high-performance architecture based on the Go language and flexible modular design.Among many tools to improve development efficiency, the `include` tag provided by Anqi CMS template is undoubtedly a key tool for achieving more efficient modular development and maintenance.

2025-11-07

How to pass multiple parameters to the `include` tag and what is the correct syntax and separator?

As an experienced website operations expert, I am well aware that how to flexibly use the template function in a high-efficiency content management system like AnQiCMS, especially the `include` tag, is crucial for building a maintainable and easily expandable website.Today, let's delve into the correct syntax and separator when you need to pass multiple parameters to a template fragment using the `include` tag.

2025-11-07