Skip to content

Commit

Permalink
Template cache part two
Browse files Browse the repository at this point in the history
  • Loading branch information
tsawler committed Feb 8, 2021
1 parent a6157de commit d90b6db
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package render

import (
"bytes"
"fmt"
"html/template"
"log"
"net/http"
"path/filepath"
)
Expand All @@ -11,20 +13,29 @@ var functions = template.FuncMap{}

// RenderTemplate renders a template
func RenderTemplate(w http.ResponseWriter, tmpl string) {
_, err := RenderTemplateTest(w)
tc, err := CreateTemplateCache()
if err != nil {
fmt.Println("Error getting template cache", err)
log.Fatal(err)
}

parsedTemplate, _ := template.ParseFiles("./templates/" + tmpl)
t, ok := tc[tmpl]
if !ok {
log.Fatal(err)
}

buf := new(bytes.Buffer)

err = parsedTemplate.Execute(w, nil)
_ = t.Execute(buf, nil)

_, err = buf.WriteTo(w)
if err != nil {
fmt.Println("error parsing template:", err)
fmt.Println("error writing template to browser", err)
}

}

func RenderTemplateTest(w http.ResponseWriter) (map[string]*template.Template, error) {
// CreateTemplateCache creates a template cache as a map
func CreateTemplateCache() (map[string]*template.Template, error) {

myCache := map[string]*template.Template{}

Expand All @@ -35,7 +46,6 @@ func RenderTemplateTest(w http.ResponseWriter) (map[string]*template.Template, e

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
Expand Down

0 comments on commit d90b6db

Please sign in to comment.