How to design a basic template and allow other pages to inherit it?

Calendar 78

Building a website in Anqi CMS, an efficient and flexible template design is the key to improving development efficiency and maintaining consistency of the website.Among them, designing a basic template and allowing other pages to inherit it is the core strategy to achieve this goal.AnQi CMS uses a syntax similar to the Django template engine, making template inheritance intuitive and powerful.

Lay the Foundation: Create Your Basic Template

First, let's create the basic skeleton template of a website. All template files are stored in Anqicms./templateUnder the directory, each topic has its own independent folder. For example, if your topic name ismytheme, then the template path is/template/mytheme/.

Within this topic folder, we usually create a folder namedbash.html(or any name you like, conventionally, this is a basic layout file) file as the common basis for all pages. This file includes the overall structure of the website, such as<head>Blocks, top navigation, footer, etc., and define the areas of the page that will change according to different content as "blocks" that can be inherited and modified by the page.

Inbash.htmlIn Chinese, we use{% block 你的区块名称 %}{% endblock %}Such labels are used to define these variable areas. For example:

<!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信息,这些标签会根据当前页面自动获取,如果当前页未设置,则获取后台配置的首页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 %}

    <!-- 引入网站的样式文件,TemplateUrl 会指向当前模板文件夹的静态资源路径 -->
    <link rel="stylesheet" href="{% system with name='TemplateUrl' %}/css/style.css">
    {% block head_extra %}{% endblock %} {# 预留给子页面添加额外的头部内容,如特定页面的CSS/JS #}
</head>
<body>
    <header class="site-header">
        <div class="container">
            <!-- 网站Logo和名称 -->
            <a href="{% system with name='BaseUrl' %}" class="logo">
                <img src="{% system with name='SiteLogo' %}" alt="{% system with name='SiteName' %}">
            </a>
            <!-- 导航菜单 -->
            <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 %}
                        <ul class="sub-nav">
                            {% for subItem in item.NavList %}
                            <li class="{% if subItem.IsCurrent %}active{% endif %}">
                                <a href="{{ subItem.Link }}">{{ subItem.Title }}</a>
                            </li>
                            {% endfor %}
                        </ul>
                        {% endif %}
                    </li>
                    {% endfor %}
                </ul>
                {% endnavList %}
            </nav>
        </div>
    </header>

    <main class="site-main">
        <div class="container">
            {% block main_content %}
                <!-- 这里的默认内容,如果子页面不重写此block,则会显示 -->
                <p>这是基础模板的默认内容区域。</p>
            {% endblock %}
        </div>
    </main>

    <footer class="site-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>
            <address>
                联系人:{% contact with name="UserName" %} | 电话:{% contact with name="Cellphone" %} | 邮箱:{% contact with name="Email" %}
            </address>
        </div>
    </footer>

    <!-- 引入网站的JavaScript文件 -->
    <script src="{% system with name='TemplateUrl' %}/js/main.js"></script>
    {% block body_extra %}{% endblock %} {# 预留给子页面添加额外的脚本 #}
</body>
</html>

As can be seen, in thisbash.htmlTemplate, we have used multiple built-in tags of Anqi CMS:

  • {% system with name="..." %}: Retrieve global website configuration, such as language, website name, Logo, filing number, base URL, template static resource path, etc.
  • {% tdk with name="..." %}: Dynamically retrieve the SEO title, keywords, and description of the current page, ensuring that each page has optimized TDK information.
  • {% navList navs %}: Generate the main navigation menu of the website.
  • {% contact with name="..." %}: Obtain the website's contact information for display in the footer and other locations.
  • {% block ... %}{% endblock %}: Definedhead_extra/main_contentandbody_extraThree sections; these are areas on the subpages that can be overlaid or extended.

Modular construction: usingincludeTag

In addition to inheriting the basic template, Anqi CMS also provides{% include "你的片段文件.html" %}Tags used to introduce some small, reusable code snippets.These fragments usually do not contain the complete HTML structure but are like sidebars, breadcrumb navigation, ad slots, etc., which can be independently inserted into multiple pages.

For example, we can introducepartialCreate in the directorysidebar.htmlorbreadcrumb.html:

/template/mytheme/partial/breadcrumb.html:

<div class="breadcrumb">
    {% breadcrumb crumbs with index="首页" title=true %}
    {% for item in crumbs %}
        {% if forloop.Counter < forloop.Length %}
        <a href="{{item.Link}}">{{item.Name}}</a> &gt;
        {% else %}
        <span>{{item.Name}}</span>
        {% endif %}
    {% endfor %}
    {% endbreadcrumb %}
</div>

Thenbash.htmlor in other sub-templates, you can usemain_contentwithin the block to introduce{% include "mytheme/partial/breadcrumb.html" %}it.includeTags can help us better organize template files, reducing duplicate code.

Inheritance and override: implementation of other pages.

Now, when we need to create the home page, article detail page, category list page, and other specific pages of a website, we do not need to write the complete HTML structure from scratch. We just need to make these pages inheritbash.htmlthen rewrite as neededblockblock as needed

in the first of any sub-template file**

Related articles

How to create reusable template code snippets (macros)?

In the daily content operation of Anqi CMS, we often encounter some interface elements or code structures that need to be used repeatedly.For example, a standardized article list item, a product display card with a unified style, or a footer contact information with specific layout information.If writing this code manually every time, it is not only inefficient but also prone to inconsistent formatting issues.This is where the "Macro" feature provided by Anqi CMS becomes particularly important, as it helps us easily create reusable template code snippets, greatly enhancing the efficiency and quality of template development.###

2025-11-09

How to include a common header, footer, or sidebar template file?

When building a website on Anqi CMS, we often encounter the need: the header (Header), footer (Footer), or sidebar (Sidebar) appear repeatedly on multiple pages.If you copy code every time, not only is it inefficient, but maintenance and updates will also become extremely complex. 幸运的是,AnQi CMS is developed based on Go language, its template engine supports syntax similar to Django, providing a very flexible and powerful way to introduce these public template files, helping us to achieve efficient content management and maintenance.

2025-11-09

How to define and use custom variables in a template?

In website operation, we often need to display diverse content, which may include system preset data, as well as customized personalized information based on business needs.AnQiCMS (AnQiCMS) fully understands this need, providing flexible and diverse mechanisms, allowing you to easily define and use custom variables in templates, thus achieving precise control and personalized display of content. This article will introduce you to the various methods of defining and using custom variables in AnQiCMS templates, helping you to fully utilize these features to create websites with more expressiveness and practicality.###

2025-11-09

How to implement automatic line break display for long text?

In website content operation, we often encounter situations where we need to display a large amount of text. If these long texts are not handled properly, they may exceed the designed container, causing chaos in page layout and severely affecting user experience.AnQi CMS provides us with flexible and powerful template functions, which can easily solve the display problem of long text, especially for automatic line breaks.### Core Strategy: Using the `wordwrap` filter to implement automatic text wrapping The template system of Anqi CMS is built-in with many practical filters (Filters), among which

2025-11-09

How to display multi-language switch options for the current site?

How to easily add multilingual switching options to a website in AnQi CMS?With the continuous deepening of globalization trends, many enterprises and content operators find that having a website that supports multilingual is crucial for expanding the market and serving users in different regions.AnQi CMS understands this need, therefore it has built-in powerful multilingual support functions, allowing you to easily manage and display content in different languages. After your website content is prepared in multiple languages, the next step naturally is to provide visitors with an intuitive language switcher.This can not only improve the user experience

2025-11-09

How to add hreflang tags in templates to support multi-language SEO?

With the increasing trend of globalization, many websites are facing the challenge of providing content to users of different languages or regions.To ensure that your multilingual website performs well in search engines and accurately guides users to their preferred language version, adding the `hreflang` tag is crucial.AnQiCMS (AnQiCMS) is an enterprise-level content management system that deeply supports multilingualism and provides a convenient way to implement this feature in templates.### Understand the role of the hreflang tag `hreflang`

2025-11-09

How to display the list of dynamic Banner images configured in the website?

The website's banner area, like a dynamic promotional window, its visual appeal and content delivery efficiency directly affect the first impression of visitors to the website.A well-designed Banner that can quickly capture the user's attention and effectively showcase the brand image, latest activities, or core products.In AnQiCMS (AnQi CMS), by utilizing its powerful content management features, you can easily configure and display a dynamic banner image list, keeping your website fresh and attractive.###

2025-11-09

How to display or customize the error pages (such as 404, 500) and shutdown notifications of a website?

In the operation of the website, visitors may encounter pages that cannot be found for various reasons or temporary issues with the server, and may even be unable to access the website during maintenance and upgrades.In these situations, a well-designed error page (such as 404, 500) and a friendly shutdown prompt can not only greatly improve user experience but also maintain the professional image of the website to a certain extent and search engine optimization (SEO).AnQi CMS is an efficient content management system that provides a flexible way to display and customize these important pages.###

2025-11-09