Skip to content

Commit

Permalink
added 31 and created a simple dynamically growing cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jagottsicher committed Mar 3, 2023
1 parent 7d6fe86 commit e4b4d6b
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,62 @@ package render
import (
"fmt"
"html/template"
"log"
"net/http"
)

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

var tc = make(map[string]*template.Template)

func RenderTemplate(w http.ResponseWriter, t string) {
var tmpl *template.Template
var err error

// 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")
}

tmpl = tc[t]

err = tmpl.Execute(w, nil)
if err != nil {
log.Println(err)
}
}

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

// parse the template
tmpl, err := template.ParseFiles(templates...)
if err != nil {
return err
}

// add template to cache (map)
tc[t] = tmpl

return nil
}

0 comments on commit e4b4d6b

Please sign in to comment.