From 10d4172f3c614d3351e69971fc7c7981a0f7af1a Mon Sep 17 00:00:00 2001 From: Muyao CHEN Date: Wed, 26 Jun 2024 22:25:04 +0200 Subject: [PATCH] Working with layouts --- pkg/render/render.go | 2 +- templates/about.page.tmpl | 31 +++++++++---------------------- templates/base.layout.tmpl | 24 ++++++++++++++++++++++++ templates/home.page.tmpl | 31 +++++++++---------------------- 4 files changed, 43 insertions(+), 45 deletions(-) create mode 100644 templates/base.layout.tmpl diff --git a/pkg/render/render.go b/pkg/render/render.go index bd0a925..9212e06 100644 --- a/pkg/render/render.go +++ b/pkg/render/render.go @@ -8,7 +8,7 @@ import ( // renderTemplate renders a HTML template file func RenderTemplate(w http.ResponseWriter, tmpl string) { - parsedTemplate, _ := template.ParseFiles("./templates/" + tmpl) + parsedTemplate, _ := template.ParseFiles("./templates/"+tmpl, "./templates/base.layout.tmpl") err := parsedTemplate.Execute(w, nil) if err != nil { fmt.Println("error parsing template:", err) diff --git a/templates/about.page.tmpl b/templates/about.page.tmpl index b8c05cc..4c0281b 100644 --- a/templates/about.page.tmpl +++ b/templates/about.page.tmpl @@ -1,24 +1,11 @@ - - - - - - - About page - - - - - -
-
-
-

This is the about page

-

This is the content

-
+{{template "base" .}} +{{define "content"}} +
+
+
+

This is the about page

+

This is the content

- - - +
+{{end}} diff --git a/templates/base.layout.tmpl b/templates/base.layout.tmpl new file mode 100644 index 0000000..2e4467c --- /dev/null +++ b/templates/base.layout.tmpl @@ -0,0 +1,24 @@ +{{define "base"}} + + + + + + + About page + + {{block "css" .}} + {{end}} + + + + {{block "content" .}} + {{end}} + + {{block "js" .}} + {{end}} + + + +{{end}} diff --git a/templates/home.page.tmpl b/templates/home.page.tmpl index ecde361..a53af73 100644 --- a/templates/home.page.tmpl +++ b/templates/home.page.tmpl @@ -1,24 +1,11 @@ - - - - - - - Home page - - - - - -
-
-
-

This is the home page

-

This is the content

-
+{{template "base" .}} +{{define "content"}} +
+
+
+

This is the home page

+

This is the content

- - - +
+{{end}}