From 682ca8fd9a8b694b92e26d0964eb745d0c5811c5 Mon Sep 17 00:00:00 2001 From: betology Date: Sun, 21 Apr 2024 19:41:46 -0600 Subject: [PATCH] Building a template cache --- pkg/render/render.go | 66 +++++++++++++++++++++++++++++++++++--- templates/about.page.tmpl | 26 ++++++--------- templates/base.layout.tmpl | 19 +++++++++++ templates/home.page.tmpl | 18 ++++------- 4 files changed, 96 insertions(+), 33 deletions(-) create mode 100644 templates/base.layout.tmpl diff --git a/pkg/render/render.go b/pkg/render/render.go index 5536bd8..0203f0d 100644 --- a/pkg/render/render.go +++ b/pkg/render/render.go @@ -1,17 +1,75 @@ package render import ( + "bytes" "fmt" + "log" "net/http" + "path/filepath" "text/template" ) +var functions = template.FuncMap{} + // RenderTemplate renders templates using html/template func RenderTemplate(w http.ResponseWriter, tmpl string) { - parsedTemplate, _ := template.ParseFiles("./templates/" + tmpl) - err := parsedTemplate.Execute(w, nil) + + tc, err := CreateTemplateCache() if err != nil { - fmt.Println("error parsing template:", err) - return + log.Fatal(err) } + + t, ok := tc[tmpl] + if !ok { + log.Fatal(err) + } + + buf := new(bytes.Buffer) + + _ = t.Execute(buf, nil) + + _, err = buf.WriteTo(w) + if err != nil { + fmt.Println("Error writing template to browser", err) + } + +} + +// CreateTemplateCache creates atemplate cache as a map +func CreateTemplateCache() (map[string]*template.Template, error) { + + myCache := map[string]*template.Template{} + + pages, err := filepath.Glob("./templates/*.tmpl") + if err != nil { + return myCache, err + } + + for _, page := range pages { + name := filepath.Base(page) + fmt.Println("Page is currently", page) + + ts, err := template.New(name).Funcs(functions).ParseFiles(page) + if err != nil { + return myCache, err + } + + matches, err := filepath.Glob("./templates/*.layout.tmpl") + if err != nil { + return myCache, err + } + + if len(matches) > 0 { + ts, err = ts.ParseGlob("./templates/*.layout.tmpl") + if err != nil { + return myCache, err + } + } + + myCache[name] = ts + + } + + return myCache, nil + } diff --git a/templates/about.page.tmpl b/templates/about.page.tmpl index c485d3c..f08994d 100644 --- a/templates/about.page.tmpl +++ b/templates/about.page.tmpl @@ -1,20 +1,12 @@ - - - - - - Document - +{{template "base" .}} - - -
-
-
-

This is de about page

-

This is some text

+ +{{define "content"}} +
+
+
+

This is de about page

+
-
- - +{{end}} \ No newline at end of file diff --git a/templates/base.layout.tmpl b/templates/base.layout.tmpl new file mode 100644 index 0000000..bd1d55a --- /dev/null +++ b/templates/base.layout.tmpl @@ -0,0 +1,19 @@ +{{define "base"}} + + + + + + Document + + + {{block "css" .}} + {{end}} + + {{block "content" .}} + {{end}} + {{block "js" .}} + {{end}} + + +{{end}} \ No newline at end of file diff --git a/templates/home.page.tmpl b/templates/home.page.tmpl index 68c9e55..11c97ba 100644 --- a/templates/home.page.tmpl +++ b/templates/home.page.tmpl @@ -1,18 +1,12 @@ - - - - - - Document - - - +{{template "base" .}} + +{{define "content"}}
-

This is de home page

+

This is de home page

+

Some text

- - +{{end}} \ No newline at end of file