Skip to content

Commit

Permalink
added lecture 32 (part 1 static cache)
Browse files Browse the repository at this point in the history
  • Loading branch information
jagottsicher committed Mar 6, 2023
1 parent e4b4d6b commit 6916dc5
Showing 1 changed file with 45 additions and 36 deletions.
81 changes: 45 additions & 36 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,73 @@
package render

import (
"fmt"
"bytes"
"html/template"
"log"
"net/http"
"path/filepath"
)

// rendernTemplate serves as a wrapper and renders
// a layout and a template from folder /templates to a desired writer
func RenderTemplateTemp(w http.ResponseWriter, tpml string) {
parsedTemplate, _ := template.ParseFiles("./templates/"+tpml, "./templates/base-layout.tpml")
err := parsedTemplate.Execute(w, nil)
func RenderTemplate(w http.ResponseWriter, tpml string) {
// create a template cache
tc, err := createTemplateCache()
if err != nil {
fmt.Println("error parsing template:", err)
log.Fatalln("error creating template cache ", err)
}
}

var tc = make(map[string]*template.Template)
// get the right template from cache
t, ok := tc[tpml]
if !ok {
log.Fatalln("template not in cache for some reason ", err)
}

func RenderTemplate(w http.ResponseWriter, t string) {
var tmpl *template.Template
var err error
// store result in a buffer and double-check if it is a valid value
buf := new(bytes.Buffer)

// check to see if we already have the template in our cache
_, inMap := tc[t]
if !inMap {
// need to create the template
log.Println("creating template and adding to cache")
err = createTemplateCache(t)
if err != nil {
log.Println(err)
}
} else {
// we have the template in the cache
log.Println("using cached template")
err = t.Execute(buf, nil)
if err != nil {
log.Println(err)
}

tmpl = tc[t]

err = tmpl.Execute(w, nil)
// render that template
_, err = buf.WriteTo(w)
if err != nil {
log.Println(err)
}
}

func createTemplateCache(t string) error {
templates := []string{
fmt.Sprintf("./templates/%s", t),
"./templates/base-layout.tpml",
}
func createTemplateCache() (map[string]*template.Template, error) {
theCache := map[string]*template.Template{}

// parse the template
tmpl, err := template.ParseFiles(templates...)
// get all available files *-page.tpml from folder ./templates
pages, err := filepath.Glob("./templates/*-page.tpml")
if err != nil {
return err
return theCache, err
}

// add template to cache (map)
tc[t] = tmpl
// range through the slice of *-page.tpml
for _, page := range pages {
name := filepath.Base(page)
ts, err := template.New(name).ParseFiles(page)
if err != nil {
return theCache, err
}

matches, err := filepath.Glob("./templates/*-layout.tpml")
if err != nil {
return theCache, err
}

if len(matches) > 0 {
ts, err = ts.ParseGlob("./templates/*-layout.tpml")
if err != nil {
return theCache, err
}
}

return nil
theCache[name] = ts
}
return theCache, nil
}

0 comments on commit 6916dc5

Please sign in to comment.