Overview:

In Go template projects, mastering the concept of base templates and blocks is essential for creating flexible and modular web applications. Here's a detailed breakdown of how base templates and blocks work in spurtCMS templates

Defining the Base Template:

The base template serves as the foundation upon which all other pages are built. In our example, the base template resides in layouts/_default/baseof.html. It defines the overall structure of the HTML document and includes placeholders for dynamic content. Here's a simplified version of the base template:

layouts/_default/baseof.html

		<!DOCTYPE html>
		<html lang="en">
		<head>
		{{template "head" .}}
		</head>
		<body>
		{{- block "header" .}}{{end}}
		{{- block "main" . }}{{end}}
		{{- block "footer" .}}{{end}}
		</body>
		{{template "script" .}}
		</html>

In this template, we use the {{block}} directive to define placeholders for the header, main content, and footer sections. These blocks will be filled in by specific templates that extend the base template.

Overriding the Base Template:

From the base template, we can define more specific templates that inherit its structure but override certain sections as needed. For example, we can create a default list template (layouts/_default/list.html) and a single page template (layouts/_default/single.html).

In the default list template, we define the "main" block specific to list views:

layouts/_default/list.html

			{{- define "main"}}
			{{- block "listmain" .}}{{end}}
			{{end}}

Similarly, in the single page template, we define the "main" block specific to single page views:

layouts/_default/single.html

			{{- define "main"}}
			{{- block "singlemain" .}}{{end}}
			{{end}}

Custom 404 Page Template:

Additionally, we can create a custom 404 page template (layouts/_default/404.html) to handle mistaken URL paths gracefully:

layouts/_default/404.html

  Your 404.html file can be set to load automatically when a visitor enter
        
This template ensures that users are provided with a helpful error message when they encounter a page not found situation.

 

The appropriate implementation of base templates and blocks in Go template projects not only promotes code organization and maintainability but also empowers developers to deliver compelling and user-centric web experiences

 

 

 

 

 

 

 

spurtCMS Templates