How to reference common code snippets (such as headers and footers) in Anqi CMS templates to unify page display?

Calendar 👁️ 56

It is crucial to maintain consistency in the display of web pages in website operation.A unified website appearance can not only enhance brand professionalism but also optimize the user experience.AnqiCMS provides a powerful and flexible template mechanism, allowing us to efficiently manage common website elements such as headers and footers, thus avoiding redundant code writing and ensuring the consistency of the entire site style.

The AnqiCMS template system borrows the syntax of the Django template engine, the core idea of which is to allow developers to define reusable code snippets and page skeletons.This allows us to build websites like building with LEGO bricks, maintaining the overall structure stable while being able to flexibly replace and combine local modules.

Core Concept: Template Inheritance (Extends)

Imagine you are designing a building. Template inheritance is like the overall architectural blueprint of this building.All floors (specific pages) will follow this basic blueprint structure, for example, the location of load-bearing walls and elevator shafts are fixed, but the interior decoration (page content) of each room can be customized as needed.

In AnqiCMS, we usually create a namedbase.htmlThe file with a similar name serves as the overall layout file for the website. This file includes the common structure of the website, such as the HTML document type declaration,headThe area (including CSS imports, metadata, etc.), header, footer, and a reserved area for the main content.

in thisbase.htmlWe use in the file,{% block 块名 %}{% endblock %}This tag defines an area that can be filled or overridden by child templates. For example:

<!-- template/你的模板目录/base.html -->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>{% tdk with name="Title" siteName=true %}</title>
    <!-- 引入公共 CSS/JS 等 -->
    <link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/main.css">
    {# 这里可以放置网站图标、SEO关键词描述等通用头部信息 #}
</head>
<body>
    <header>
        {# 这里我们将引入页头公共代码片段 #}
    </header>

    <main>
        {% block content %}
            {# 这是主要内容区域,每个具体页面将在这里填充其独特内容 #}
        {% endblock %}
    </main>

    <footer>
        {# 这里我们将引入页脚公共代码片段 #}
    </footer>

    <script src="{% system with name="TemplateUrl" %}/js/main.js"></script>
    {# 这里可以放置其他全局性的 JS 脚本 #}
</body>
</html>

When we need to create a specific page (such as the homepage, article detail page), we just need to declare it inherits from the template file of that page in the first linebase.htmland then through the same named{% block %}Label to fill or override the corresponding content area in the parent template:

<!-- template/你的模板目录/index.html (首页示例) -->
{% extends 'base.html' %} {# 声明此模板继承自 base.html #}

{% block content %}
    <section class="banner">
        {# 这里是首页独有的轮播图内容 #}
        {% bannerList banners %}{% for item in banners %}<img src="{{item.Logo}}" alt="{{item.Alt}}">{% endfor %}{% endbannerList %}
    </section>

    <section class="latest-articles">
        <h2>最新文章</h2>
        <ul>
            {% archiveList archives with type="list" moduleId="1" limit="5" %}
                {% for item in archives %}
                    <li><a href="{{item.Link}}">{{item.Title}}</a></li>
                {% endfor %}
            {% endarchiveList %}
        </ul>
    </section>
    {# 首页的其他特定内容 #}
{% endblock %}

By using template inheritance, we successfully separated the common layout of the website from the page-specific content, greatly improving the maintainability of the template.

Flexible reference of code snippets (Include)

If template inheritance is the "skeleton" of a website, thenincludeThe tag is the "lego bricks" that make up the skeleton or the "muscles" on the skeleton.It is used to insert smaller, reusable code snippets (such as navigation menus, sidebars, copyright information blocks) into the specified location of any template file.

These small code snippets are usually stored in the template directory.partial/In the subdirectory, it is convenient to manage. For example, you can createpartial/header.htmlStore the header content,partial/footer.htmlStore the footer content.

Inbase.htmlOr any other template, the way to refer to these fragments is very simple:

<!-- 在 base.html 中引用页头片段 -->
<header>
    {% include 'partial/header.html' %} {# 引入专门的页头文件 #}
</header>

<!-- 在 base.html 中引用页脚片段 -->
<footer>
    {% include 'partial/footer.html' %} {# 引入专门的页脚文件 #}
</footer>

includeTags also support some advanced usage:

  • Passing variablesYou can usewithkeywords to pass additional variables to the included fragments. For example, if your page header needs to display some specific information about the current page:
    
    {% include 'partial/header.html' with current_page='index' user_name='游客' %}
    
    Thenpartial/header.htmlYou can use it in{{ current_page }}and{{ user_name }}.
  • Conditional inclusionIf you are not sure if a segment exists, you can useif_existsto avoid errors:
    
    {% include 'partial/custom_sidebar.html' if_exists %}
    
    In this way, if the file exists, it will be included, and if it does not exist, it will be ignored.

Application in practice: Page header, footer, and navigation

Now, let's combine the built-in tag feature of AnqiCMS and see how to reference dynamic data in these common code snippets to achieve unified display of website content.

1. A unified page header (partial/header.html)

The page header usually includes the website logo, main navigation, search box, etc. Through the system tags provided by AnqiCMS, we can easily obtain these dynamic data.

`

Related articles

How to configure content automatic external link filtering in Anqi CMS and how it affects the display of front-end links?

In website content operation, the management of external links is a vital aspect that cannot be ignored.It not only relates to user experience, but also directly affects the website's search engine optimization (SEO) performance.AnQi CMS knows this and therefore provides a flexible and practical external link handling mechanism to help content operators efficiently manage external links in articles.Where is the AnQi CMS external link configuration? AnQi CMS provides a very convenient function to handle external link issues, and you can easily configure it in the system background.The specific path is: log in to your AnQi CMS backend

2025-11-08

How to set up automatic image compression in Anqi CMS to speed up page loading?

Optimize images, speed up the website: AnqiCMS Automatic Compression Settings Guide Website access speed is one of the key factors in user experience and an important consideration for search engine optimization (SEO).And large images are often the culprit that slows down website loading speed.Fortunately, AnqiCMS, as a fully functional content management system, is built with a series of powerful image optimization features, which help us easily deal with this issue, while keeping the website smooth in providing rich visual content.Today, we will take a detailed look at how to use AnqiCMS

2025-11-08

Does AnQi CMS support the automatic conversion of uploaded images to WebP format for optimized display?

In the era where content is king, the loading speed of website images directly affects user experience and search engine optimization (SEO) performance.Efficient image management and optimization strategies are crucial for any website committed to providing high-quality content services.Many website operators are actively seeking solutions that can automatically optimize images, especially those converted to WebP format.So, for those who are using or considering using AnQiCMS, does this system support automatic conversion of images to WebP format after upload to optimize display?The answer is affirmative

2025-11-08

How to format a timestamp into a readable date and time format and display it in AnQi CMS?

In website content operations, the presentation of time is often ignored, but it is an important detail for improving user experience and content professionalism.The original timestamp number lacks meaning for ordinary users. Formatting it into a clear and easy-to-read date and time format makes the content more appealing.AnQi CMS knows this, providing a simple and powerful way to handle such needs.### Understanding the timestamp in AnQi CMS In AnQi CMS, whether it is the publication time (`CreatedTime`) or the update time (`UpdatedTime`)

2025-11-08

How to display a custom prompt message during the website shutdown maintenance of AnQi CMS?

During the operation of the website, we sometimes have to temporarily close the website for maintenance in order to upgrade the system, optimize data, or adjust functions.How to clearly and friendly convey this status to visitors, avoiding confusion or loss of users, is particularly important.Safe CMS provides a set of built-in solutions that allow you to easily customize the prompt information for your website during maintenance shutdown. ### Understanding the shutdown mechanism of AnQi CMS The shutdown feature of AnQi CMS is designed to be very intuitive and flexible.When you enable the closed site mode, the system will automatically intercept all access requests to the website content

2025-11-08

How to display multi-language switch links and the current language identifier in Anqi CMS?

In AnQi CMS, implementing multi-language switch links and current language identification is a very practical feature that can help your website easily face global users and enhance the internationalization level of your website.Aqicms provides intuitive and powerful template tags, allowing you to flexibly display these elements on the website front end. ### One, understand the multilingual capabilities of Anqicms Anqicms has considered the needs of multilingual sites from the very beginning and provides comprehensive multilingual content management support.This means you can create and manage independent content for different languages in the background, such as articles

2025-11-08

How to automatically add hyperlinks to URLs and email addresses in article content of AnQi CMS?

In content management and website operations, automatically converting URLs and email addresses in the text to clickable hyperlinks can greatly enhance user experience and optimize the internal link structure of the website to some extent.For AnQiCMS users, implementing this feature is not through simple backend settings, but through the specific "filter" provided by its powerful template engine.This design approach gives website owners great flexibility and control, allowing them to finely manage the generation of links according to the needs of different content areas.

2025-11-08

How to truncate a long string and display an ellipsis in AnQi CMS templates?

The flexibility of content display is crucial in the template making process of Anqi CMS.When it is necessary to display a long text within a limited space, such as an article abstract, product description, or brief introduction, truncating the strings with an ellipsis is a common optimization method. It can effectively maintain the neat layout of the page and guide users to click and view the full content.AnQi CMS is developed based on Go language, its template engine syntax is similar to Django, and it provides powerful filter (filters) functions that can easily meet this requirement.### Skillfully Utilize Filters

2025-11-08