AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go programming language, providing strong support for content operations with its efficient and flexible features.In the process of template development, proficiently using its built-in Django style template engine is the key to improving efficiency.extendsTags are a powerful tool for implementing template inheritance and building a unified layout for websites.However, for this powerful function to work properly and be correctly parsed by the template engine, its placement has strict conventions.

The core of template inheritance:extendsThe "stationing" art of tags

In the world of AnQiCMS templates,extendsThe tag carries the core function of template inheritance, allowing you to define a basic layout (usually referred to as "parent template" or "masterThis greatly improves the reusability and maintainability of the template, ensuring the consistency of the overall style of the website.

Then,extendsWhere exactly must the tag be placed in the template file to work properly? The answer isIt must be the first tag in the template file. This means, in your{% extends '父模板路径.html' %}This line of code cannot have any other content before it, including but not limited to HTML code, spaces, line breaks, or even comments from the template engine itself{# ... #}or HTML comments<!-- ... -->.

Imagine this is like building a house, you must first lay a solid foundation before you can build walls and a roof.extendsThis label is the indestructible "foundation". The template engine needs to determine first which parent template the current template is based on when parsing files. IfextendsThe template engine cannot immediately recognize this as an inheritance relationship due to the presence of any other parseable or unparseable characters in front of the tag, resulting in a parsing failure.

The consequences and understanding of misplaced items

IfextendsThe tag is not placed at the beginning of the file, the template engine of AnQiCMS will not be able to correctly identify and establish inheritance relationships. This usually leads to the following types of problems:

  1. Template rendering error:The page may directly report an error, indicating that a variable or tag cannot be found because the entire inheritance chain has not been established correctly.
  2. The page is displayed incomplete:The content of the child template may be rendered independently, while the common structure of the parent template (such as header, footer, sidebar, etc.) is completely missing.
  3. Debugging is difficult:Sometimes error messages may not be very intuitive, they may just be disorganized page layouts or missing content, which increases the complexity of troubleshooting.

The key to understanding this rule lies in the working mechanism of the template engine. When the engine reads a template file, it first looks forextendsThe tag determines its parent template. Once found, it will load the parent template and prepare the one defined in the parent template.blockArea. Subsequent, the same-named one defined in the sub-template.blockThis will override or append content from the parent template. This process must be completed before handling any other page content, otherwise the template structure cannot be properly combined.

Application and suggestions in practice

In the actual development of AnQiCMS templates, in order to ensureextendsthe correctness of tags, we recommend:

  • starting from the first line:always to{% extends '你的父模板路径.html' %}Place it on the first line of the template file, ensuring there are no characters before it.
  • Definition of the parent template:In the parent template (such asbase.htmlUse it in{% block 区域名称 %}Define an area that can be rewritten or filled by child templates. For example, you can define{% block title %}Used for page titles,{% block content %}Used for the main content area.
  • Filling for child templates:In the child template, after inheriting the parent template, it only needs to use{% block 区域名称 %}and{% endblock %}to wrap the unique content of the region. The unoverriddenblockregion will automatically use the content defined in the parent template.

For example, a typical sub-template filearticle_detail.htmlIt will start like this:

{% extends 'layouts/base.html' %}

{% block title %}
    文章详情 - {{ archive.Title }}
{% endblock %}

{% block content %}
    <h1>{{ archive.Title }}</h1>
    <div class="article-meta">
        发布日期: {{ stampToDate(archive.CreatedTime, "2006-01-02") }}
    </div>
    <div class="article-body">
        {{ archive.Content|safe }}
    </div>
{% endblock %}

In this structure,{% extends 'layouts/base.html' %}clearly at the top of the file, explicitly informing the template engine of the inheritance relationship of the file.

In summary,extendsTags are the cornerstone of AnQiCMS template development, essential for building efficient and unified website layouts.Master the rule that must be placed at the beginning of the template file, which not only avoids potential parsing errors but also helps developers build high-quality website templates that are clear and easy to maintain.


Frequently Asked Questions (FAQ)

  1. Question: Besides,extendsTags, what tags must be placed in specific positions in the template file?Answer: In the Django-style template engine used by AnQiCMS,extendsIt is the only label that must be strictly placed at the beginning of the template file. Other such asinclude(used to introduce code snippets),macro(used to define reusable code blocks) andblock(Used to define content areas) and other tags can be placed at any position within the file according to logical needs, as long as they comply with their own syntax rules, such asblockThe tag needs to appear in pairs.

  2. Question: If I amextendsThere was an HTML comment before the tag (for example<!-- 我的注释 -->) Will it affect parsing?Answer: Yes, even HTML comments are considered part of the file content by the template engine.extendsThe tag must be within the file.The firstA parseable template element, which means any character before it, including spaces, new lines, HTML comments, or any other template tags, will cause the template engine to fail to correctly identify inheritance relationships, thereby causing parsing failure.

  3. Question:extendsandincludeWhat are the differences between tags? When should I use them?Answer:extendsandincludeThey are all used for template reuse, but their application scenarios are different.extendsUsed to establish inheritance relationships between parent and child templates, where the child template inherits the overall layout and structure of the parent template and usesblockRewrite or fill a specific area. It defines the "skeleton" of the web page.includeIt focuses more on inserting small, independent template fragments (such as navigation bars, sidebars, footers, etc.) into the specified position of any template file.These fragments do not have the ability to inherit the parent template structure and are usually used for reusable UI components or code snippets.extendsDefine the overall framework of the page, whileincludeis used to fill the local components within the framework.