How to organize reusable display modules in a template through `include`, `extends`, and other tags?

Calendar 👁️ 66

Secure CMS template: Use wiselyincludeandextendsCreate efficient and reusable display modules

When you use Anqi CMS to build a website, you will quickly find that it is very flexible in organizing content display.Especially when dealing with templates, the built-in Django template engine syntax allows us to build pages efficiently like stacking blocks.Today, let's talk about how to cleverly useincludeandextendsThese powerful tags are used to organize and manage reusable display modules on your website, making your templates cleaner and easier to maintain.

Understanding the foundation of Anqi CMS templates.

Before we delve deeper, let's review the basic conventions of the Anqi CMS template. Template files usually begin with.htmlwith suffix, and stored uniformly in/templateUnder the directory. The styles, JavaScript scripts, and static resources you need will be placed in/public/static/Within the directory. In the template, we use double curly braces{{ 变量 }}Output content, using single curly braces and percent signs{% 标签 %}Control logic, which is very similar to the Django template engine syntax, and it's very natural to get started with it.

includeLabel: Pluggable code snippet

Imagine the header, footer, sidebar, or some ad space on your website, this content appears on almost every page.If you copy and paste on each page, it will take a lot of time, and it will also be very麻烦 to modify in the future. At this time,includethe tag comes into play.

includeThe tag allows you to embed an independent HTML snippet (or code snippet) into the current template file.Its basic syntax is very simple, you just need to specify the path of the template file to be imported.Generally, these areincludeIncoming code snippets will be placed in a dedicated directory, such as/template/你的模板目录/partial/, which is convenient for management.

For example, if the content of your header (header) is relatively independent, you can place itpartial/header.html:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>{{ title|default:"默认标题" }}</title>
    <meta name="keywords" content="{{ keywords|default:"默认关键词" }}">
    <link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/style.css">
</head>
<body>
    <header class="main-header">
        <h1><a href="{% system with name="BaseUrl" %}">{% system with name="SiteName" %}</a></h1>
        <nav>
            {% navList mainNavs %}
                <ul>
                    {% for item in mainNavs %}
                        <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
                    {% endfor %}
                </ul>
            {% endnavList %}
        </nav>
    </header>

Then, when you need to use the header on any page, simply introduce it:

{% include "partial/header.html" %}

Sometimes, you may not be sure if a fragment file exists, or you want it to be introduced only when it exists, in which case you can addif_exists:

{% include "partial/sidebar.html" if_exists %}

In addition,includeThe tag allows you to pass specific variables to the imported snippet. For example, we want to pass the aboveheader.htmla customtitleandkeywords:

{% include "partial/header.html" with title="我的首页标题" keywords="SEO优化关键词" %}

If you only want to letincludeThe segment uses the variables you explicitly pass, not inheriting all variables from the current template, and can bewithAdd afteronly:

{% include "partial/header.html" with title="我的首页标题" keywords="SEO优化关键词" only %}

Thus,header.htmlonly recognizetitleandkeywordsthese two variables, and other variables will not be inherited.

extendsLabel: Build the overall framework of the website

If we sayincludeIt is used to insert small building blocks, thenextendsLike designing an overall blueprint or framework for your website.It allows you to define a basic template (usually referred to as a "master" or "layout template"), which includes the common structure of a website, such as navigation, header, footer, and the division of content areas, etc.

extendsLabels are always placed at the top of the template file, declaring which parent template the current template inherits from. For example, we create a parent template file namedbase.htmlwhich defines the basic structure of the page:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    {# 这个title块是可被子模板重写的区域 #}
    {% block title %}
        <title>默认网站标题 - {% system with name="SiteName" %}</title>
    {% endblock %}
    <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/global.css">
    {% block head_extra %}{# 预留给子模板添加额外head内容 #}{% endblock %}
</head>
<body>
    <div class="wrapper">
        <header>
            {# 这里通常会include页眉代码片段 #}
            {% include "partial/header.html" %}
        </header>

        <nav class="main-nav">
            {# 导航栏代码,可能也是一个include片段 #}
        </nav>

        <main class="content-area">
            <aside class="sidebar">
                {# 侧边栏内容,也可以是include片段 #}
            </aside>
            <article class="main-content">
                {# 这个content块是页面的主体内容,子模板会在这里填充 #}
                {% block content %}
                    <p>这是母版中定义的默认内容。</p>
                {% endblock %}
            </article>
        </main>

        <footer>
            {# 这里通常会include页脚代码片段 #}
            {% include "partial/footer.html" %}
        </footer>
    </div>
    {% block body_extra %}{# 预留给子模板添加额外body内容 #}{% endblock %}
</body>
</html>

In the master template, you need to useblocktags to define areas that can be overwritten or filled by child templates. When a child template inherits this master, it can optionally overwrite theseblockThe content of the area. If the sub-template does not re

Related articles

How to configure 301 redirect rules to avoid SEO impact after content adjustment?

Website content operation is a continuous job, with the development of the business, content updates, adjustments, and even optimization of the website structure can lead to changes in page URLs.However, if these URL changes are not handled properly, they often have a significant negative impact on the website's search engine optimization (SEO), such as a drop in rankings, loss of traffic, and so on.This is when the 301 redirect becomes a key tool for us to avoid these negative impacts and maintain the SEO health of the website.### Understanding the value of 301 redirect In simple terms, 301 redirect (Moved

2025-11-07

How to categorize image resources and display them on the front end according to categories?

Images are an indispensable part of a website's content, as they can not only beautify the page but also effectively convey information and enhance the user experience.However, with the increase in the number of images on a website, how to efficiently manage these image resources and flexibly display them on the front page as needed has become a challenge for many website operators.AnQiCMS (AnQiCMS) is well-versed in this field, providing a user-friendly and powerful image resource management system that helps us easily manage these visual elements.

2025-11-07

How to define and use variables for content display in AnQiCMS templates?

AnQiCMS (AnQiCMS) boasts its efficient architecture based on the Go language and flexible template mechanism, making content presentation both powerful and intuitive.In website content operation, we often need to display dynamic information, such as article titles, product prices, contact information, etc., which is inseparable from defining and using variables in templates.Understanding how to flexibly use variables in the AnQiCMS template is the key to building and maintaining an efficient, personalized website.The AnQi CMS template engine has borrowed the syntax from Django, making it very easy to understand the definition and reference of variables. Essentially

2025-11-07

How to reference and display external static resources (such as CSS, JS) in the template?

Referencing and displaying external static resources in AnQiCMS templates, such as CSS style sheets and JavaScript scripts, is a key step in building a functional and visually appealing website.Understand the working principle and recommended practices, which can make our website development work more efficient and flexible.

2025-11-07

How to display a friendly prompt page to users during website maintenance or upgrade?

During the operation of the website, whether it is system upgrades, data migration, or routine maintenance, it is inevitable that there will be situations where the website needs to be temporarily closed or some functions cannot be accessed.How to convey clear and friendly information to users visiting the website at this time, to avoid confusion or direct loss of users, is the key to improving user experience and maintaining brand image.lucky enough, AnQiCMS provides a simple and efficient mechanism to help us easily deal with such situations. ### Why is it necessary to have a friendly maintenance page for website upgrades?

2025-11-07

How to display independent content and configuration for each site in a Docker-deployed AnQiCMS multi-site environment?

In today's internet operations, many companies or individuals often need to manage multiple websites to serve different brands, products, or target audiences.AnQiCMS (AnQiCMS) leverages its powerful multi-site management capabilities to easily meet this requirement on a single core system.When combined with Docker, a popular containerization technology, how to ensure that each site can run efficiently and independently display its content and configuration has become a topic worth discussing.

2025-11-07

How to efficiently manage and display independent content and data for multiple sites in AnQiCMS?

Managing the content and data of multiple websites is often a time-consuming and error-prone challenge for many businesses and content operators.Different brands, product lines, or independent sites for different regions or language audiences often mean that separate content management systems need to be independently built and maintained, leading to分散 of resources, increased workload, and even making it difficult to achieve effective collaboration management.

2025-11-07

How to customize the content model to flexibly display articles, products, events, and other types of information?

In today's increasingly diverse era of content marketing, the types of information that websites need to carry are also becoming more varied.Relying solely on the general types of "articles" and "products" often fails to meet the personalized display needs of corporate or self-media operators, such as posting event notifications, job postings, case studies, or industry reports.At this time, the customized content model feature provided by AnQiCMS (AnQiCMS) has become a powerful tool for us to flexibly manage and display these diverse pieces of information.Content model, in a word, is a structural blueprint for content.

2025-11-07