How to display common website elements, such as headers and footers, uniformly through template fragments (partial)?

Calendar 👁️ 72

When managing website content in AnQi CMS, you may find that many pages have similar structures, such as top navigation, footer copyright information, sidebar, or metadata tags.If you have to rewrite this code for each page every time, it is not only inefficient, but also once you need to modify it, you have to search and update it page by page, which takes a lot of time and is easy to make mistakes.Fortunately, AnQi CMS provides a very elegant solutionTemplate Fragment (Partial)It can help us manage these public elements efficiently and uniformly.

What is a template fragment (Partial)?

In simple terms, a template snippet is a small piece of code that can be reused by other template files on a website. They typically contain common structures or functional modules of the website, such as a standalone header, a universal footer, a sidebar module, or some general page elements.<head>Area code. In AnQi CMS, these template fragments are usually stored in the current theme template directory.partial/folder, with the.htmlsuffix.

Why use template fragments?

Using template snippets to manage common elements can bring various benefits to your website operation:

  1. Maintain the unity of your website:Ensure that all page headers, footers, and other common areas maintain a consistent style and content to enhance user experience and brand image.
  2. Improve development and maintenance efficiency:Store common code in a centralized location, write once and use everywhere.When changes are needed, only update one template fragment file, and all pages referencing it will be automatically updated, saving a lot of time and effort.
  3. Code structure is clearer:Break down large pages into logically independent modules, making template files more concise, easier to read and understand, and reducing the complexity of later maintenance.
  4. Promote teamwork:Different developers can focus on their respective modules without interfering with each other, improving the team's development efficiency.

How to use template fragments in AnQi CMS?

In Anqi CMS, implementing the unified display of template fragments is very intuitive, mainly divided into two steps: creating and referencing.

Step 1: Create your template fragment file.

Firstly, you need to extract the common element code of the website from the original page and create an independent.htmlfile and place it in the directory under your themepartial/folder.

Assume your theme name isdefaultThen the directory structure of your template fragment may look like this:your_anqicms_root/template/default/partial/

For example, let's create the header (header) and footer (footer):

1. Createheader.html:This file can include your website logo, main navigation, search box, and so on.

<!-- template/default/partial/header.html -->
<header class="main-header">
    <div class="container">
        <div class="site-branding">
            <a href="{% system with name="BaseUrl" %}">
                <img src="{% system with name="SiteLogo" %}" alt="{% system with name="SiteName" %}">
            </a>
        </div>
        <nav class="main-navigation">
            <ul>
                {% navList navs %}
                    {% for item in navs %}
                        <li {% if item.IsCurrent %}class="active"{% endif %}>
                            <a href="{{ item.Link }}">{{item.Title}}</a>
                            {% if item.NavList %}
                                <ul class="sub-menu">
                                    {% for sub_item in item.NavList %}
                                        <li {% if sub_item.IsCurrent %}class="active"{% endif %}><a href="{{ sub_item.Link }}">{{sub_item.Title}}</a></li>
                                    {% endfor %}
                                </ul>
                            {% endif %}
                        </li>
                    {% endfor %}
                {% endnavList %}
            </ul>
        </nav>
        <div class="contact-info">
            联系我们: {% contact with name="Cellphone" %}
        </div>
    </div>
</header>

On the aboveheader.htmlIn the snippet, we used the built-in function of Anqi CMS.systemTag to get the website logo and name, as well asnavListTags to dynamically generate navigation menus, and usecontactTags to display contact information.

2. Createfooter.html:This file can include copyright information, filing number, friendship links, and so on.

<!-- template/default/partial/footer.html -->
<footer class="main-footer">
    <div class="container">
        <p>{% system with name="SiteCopyright" %}</p>
        <p><a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">{% system with name="SiteIcp" %}</a></p>
        <div class="friend-links">
            友情链接:
            {% linkList friendLinks %}
                {% for link_item in friendLinks %}
                    <a href="{{link_item.Link}}" {% if link_item.Nofollow == 1 %} rel="nofollow"{% endif %} target="_blank">{{link_item.Title}}</a>
                {% endfor %}
            {% endlinkList %}
        </div>
    </div>
</footer>

Here, we also took advantage ofsystemtags to obtain the copyright and record information of the website, as well aslinkListtags to display the links

Step two: Refer to the template fragment in your main template

After creating the template fragment file, you can use it in any main template file of the website (such as the homepageindex/index.html, article detail pagearchive/detail.htmlSingle Pagepage/detail.htmletc.){% include "partial/your_partial_file.html" %}to reference them.

For example, in a typical page structure, you can organize your main template file like this:

<!-- template/default/index/index.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">
    <!-- 引入页面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/style.css">
    <!-- 更多head内容 -->
</head>
<body>
    <!-- 引入页头 -->
    {% include "partial/header.html" %}

    <main class="main-content">
        <div class="container">
            <!-- 这里是当前页面的具体内容,例如文章列表、产品展示、单页内容等 -->
            <h1>欢迎来到安企CMS搭建的网站!</h1>
            <p>这是首页的独有内容。</p>
            {% archiveList archives with type="list" limit="5" %}
                {% for item in archives %}
                    <article>
                        <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
                        <p>{{ item.Description|truncatechars:100 }}</p>
                    </article>
                {% endfor %}
            {% endarchiveList %}
        </div>
    </main>

    <!-- 引入页脚 -->
    {% include "partial/footer.html" %}

    <script src="{% system with name="TemplateUrl" %}/js/main.js"></script>
    <!-- 更多脚本 -->
</body>
</html>

This way, you can reuse ` whenever you need as many pages as you want

Related articles

How to customize the display template for specific documents, categories, or single pages?

When using AnQiCMS to manage website content, we often need to assign unique display styles and functional layouts to specific content types or unique pages.This kind of requirement is very common in content operation, for example, a product introduction page may need richer picture display and parameter comparison, while a news article focuses on the reading experience of text;Or it is the "About Us" page on the website, which needs to show a unique corporate culture.AnQi CMS provides flexible template customization features, allowing you to easily meet these personalized display needs

2025-11-08

How to choose the appropriate template type for the AnQiCMS website to optimize cross-device display effects?

It is crucial to ensure that the website displays well on different devices when building and operating a modern website.A website that provides a smooth experience on all screen sizes has become a norm as users access websites through various devices such as smartphones, tablets, and computers, not only improving user satisfaction but also helping to effectively disseminate content and optimize search engine rankings.AnQiCMS (AnQiCMS) offers various template types to meet this need, helping us flexibly deal with cross-device display challenges.

2025-11-08

How to ensure UTF-8 encoding of template files to avoid Chinese content from displaying garbled?

When using AnQi CMS to build and manage a website, you may encounter situations where Chinese content is displayed as garbled characters. This is usually not a problem with the system itself, but rather due to inconsistent encoding of template files.By ensuring that the template file uses UTF-8 encoding, you can completely resolve this problem, allowing the website content to be presented clearly and accurately to visitors. AnQi CMS was designed with the pursuit of efficiency, flexibility, and ease of use. It excels in supporting multiple languages and SEO optimization, and proper coding is the foundation for leveraging these advantages.

2025-11-08

How to control the conditional display and loop display of AnQiCMS page content with Django template syntax?

In the flexible and powerful AnQiCMS content management system, the dynamic display of page content cannot do without the support of template syntax.Mastering conditional display and loop display of these two core functions can help us present data more efficiently and accurately on the website front-end.AnQiCMS uses a template syntax similar to Django, making it easy to create content templates that are both familiar and easy to use.

2025-11-08

How to correctly call and display the system configuration information in AnQiCMS templates?

Manage website content in AnQiCMS, it is not just to publish articles and products, but also includes the basic information, contact information, SEO metadata and other system-level configurations of the website.These configuration information is usually required to be displayed correctly on all pages of the website, such as displaying the website logo and name in the page header, displaying the copyright and filing number in the footer, or displaying the company's phone number and address on the contact us page.Properly and flexibly invoking these system configurations is the key to ensuring consistency, ease of maintenance, and SEO-friendliness of the website.AnQiCMS uses syntax similar to the Django template engine

2025-11-08

How to dynamically display the contact information of the website in AnQiCMS templates?

In website operation, clearly and accurately displaying the website's contact information is a key link in building trust with users and providing support.A contact method that is easy to find and keep updated, which can not only improve the user experience but also help users find you quickly when necessary.AnQiCMS as an efficient content management system provides a very flexible and intuitive way for you to dynamically display and manage this important information in website templates.

2025-11-08

How to set and display search engine friendly TDK (Title, Description, Keywords) for AnQiCMS pages?

Search engine optimization (SEO) is an indispensable part of website operation, and the Title (title), Description (description), and Keywords (keywords) - abbreviated as TDK - of the website page are the foundation of SEO.They are the first step for search engines to understand page content and a key factor for users to decide whether to click on search results.

2025-11-08

How to implement a multi-level menu and content联动 display on the AnQiCMS website navigation list?

The website's navigation system is like the skeleton of a building, it not only guides users to browse but also directly affects the exposure of content and search engine optimization.AnQiCMS provides a flexible and powerful navigation management function, allowing you to easily build multi-level menus and achieve deep linkage with website content, thereby enhancing user experience and content distribution efficiency. ### Flexible configuration, build a multi-level navigation skeleton The starting point of implementing a multi-level menu in AnQiCMS is the navigation settings in the background.You can find all configuration items related to website navigation under the 'Background Settings' and 'Navigation Settings' first.

2025-11-08