The Path to Building AnQi CMS Templates:extendswithincludeThe Core Differences and Application Practices of Tags

In today's efficient iterative website development, the flexible use of template engines is the key to improving efficiency and ensuring project maintainability.As an experienced website operations expert, I am well aware that AnQiCMS, with its powerful performance in Go language and Django-style template syntax, provides an excellent solution for content management.Its template system inextendsandincludeTwo tags are the core of building efficient and reusable page structures. However, many beginners often confuse the functions and application scenarios of the two when using them.

Today, let's delve into AnQiCMSextendswithincludeThe main differences of the tags, combined with practical application scenarios, help you better understand and apply them to manage page relationships, and build a website that is both beautiful and easy to maintain.

The definers of the page skeleton:extendsTag

Imagine you are building a website, which includes multiple pages such as the homepage, article detail page, product list page, and so on.These pages often share the same header navigation, footer copyright information, as well as sidebar and other layout elements.If every page repeats the same common code, it is not only inefficient, but also any subsequent modification will affect the whole body.

At this time,extendsTags are like the 'architect' of your website, allowing you to define a generic 'master' or 'skeleton' template (usually namedbase.htmlIt includes the common HTML structure and layout of all pages. You can use it in this template.blockLabels to reserve areas that can be filled or overlaid. For example, you can define ablock titleUsed for page title, oneblock contentused for the main content of the page.

When you create specific sub-page templates (such asindex.htmlorarticle.html), just in the file'sthe first lineUse{% extends 'base.html' %}It can inherit all the structure of the parent template. Subsequently, you can overwrite the definitions of the parent template in the sub-template.blockFill in or modify the corresponding area content. In this way, the sub-template can focus on its own unique content without concerning about the repeated code in the common part.

The core idea: extendsLabels establish an 'inheritance' relationship, where the child template 'embeds' its content into the reserved area of the parent template. It is mainly used to define and manage the overall layout and structure of the page.

The assembler of content segments:includeTag

withextendsThe macro layout is different,includeTags are more like the 'module assembler' of a website. It is used to insert a small, independent and reusable code snippet (partial) into any template that needs it.These fragments can be part of a navigation menu, a user login form, breadcrumb navigation, social media share buttons, or even complex ad code.

For example, you might have apartial/sidebar.htmlFile, which defines the content of the website sidebar. Inbase.htmlor any other template that requires a sidebar, you just need to use{% include 'partial/sidebar.html' %}Introduce its content.

includeThe strength of the tag lies in its flexibility. It can be called at any position in the template and will inherit all variables of the current template by default.Moreover, AnQiCMS also provides additional control options, such as usingwithPass specific variables to the included template or useonlyKeyword limits passing only the specified variables to avoid unnecessary context pollution. Moreover, if you are not sure whether the template file to be included exists, you can useif_existsAdjectives to avoid program errors, ensuring the website runs smoothly.

The core idea: includeThe tag implements a "composition" relationship, which "joins" an external content fragment to the specified position in the current template.It is mainly used for reusing small, independent UI components or content modules.

Core differences and application scenarios: when to useextendsWhen to chooseinclude?

UnderstoodextendsandincludeTheir respective mechanisms, the differences are also natural:

  1. Relationship types:

    • extendsEstablishing isInheritance relationship between parent and childA child template can only inherit one parent template and must be declared at the beginning of the file.
    • includeEstablishing isContent composition relationship. A template can include any number of other template fragments and can be included at any position.
  2. Scope of action:

    • extendsFocusing onthe overall page layout and structure, defining the page's skeleton.
    • includeFocusing onReuse of local content fragmentsFilled in the specific modules in the skeleton.
  3. Content transmission and control:

    • extendsDefault passing the complete context to the child template, the child template through rewritingblockto fill in the content.
    • includeThe default also passes the complete context, but it can bewithoronlyPrecisely control the variables passed to the included template.
  4. Compulsiveness and flexibility:

    • extendsIt must be the first tag of the template file, which means a file is either a parent template or inherits from a parent template, it determines the overall form of the page.
    • includeIt can be used anywhere in the template because it simply inserts the file content at the current position without affecting the overall structure.

Consideration of practical application scenarios:

  • Selectextends:When you need to define a unified visual style and layout for the entire website or a specific area of the website (such as the admin management page, blog front page), for example, a page that includes a fixed header, navigation, sidebar, and footer. You can create abase.htmlThen all the specific pagesextendsit.
  • Selectinclude:When you have some small, reusable UI components, such as a card for displaying the author information of an article, a pagination component, a comment form, or a user login status display module in the top navigation bar.These modules can be reused in different pages, even in different positions on the same page, without changing the overall layout of the page.

AnQiCMS provides these two tags, which perfectly combine the design patterns of inheritance and composition, allowing template development to ensure consistency in overall structure while also allowing flexible splitting and reuse of local code.Mastering their differences and usage will greatly enhance your efficiency and quality in building websites on AnQiCMS.


Frequently Asked Questions (FAQ)

1. Can I be in aextendsinherited sub-template, andincludeother template fragments?

Of course, this is a very common usage. You can rewrite any of the ones in the sub-templateblockinternally, or freely use any other nonblockarea in the sub-templateincludeInsert the content snippet you need. For example, in inheritingbase.htmlofarticle.htmltemplate, you can{% block content %}InternallyincludeOnepartial/article_meta.htmlto display the author and publication date of the article.

2. OneincludeThe template file coming in can beextendsanother template, right?

Technically, it is not recommended to do this as it may cause logical confusion and difficulties in maintenance.extendsThe tag must be the first tag in the template file, it defines the inheritance relationship of the current file. If you try to place it in a file that is usually as a content fragmentincludeto be placed inextendsThen this file will beincludeatextendsThe declaration will not be parsed correctly or may cause unexpected behavior because it has been included in a larger context. Usually, it isincludeThe template snippet should be a relatively independent, self-contained HTML block.

3. If Iincludeused a non-existent template file, what would happen? Is there a way to avoid errors?

AnQiCMS in processingincludeWhen labeling, if the specified template file cannot be found, an error will be reported by default. However, you can useif_existsA modifier can elegantly handle this situation. For example: `{% include “partial/