Building a template cache

This commit is contained in:
2024-04-21 19:41:46 -06:00
parent 72ac372111
commit 682ca8fd9a
4 changed files with 96 additions and 33 deletions

View File

@@ -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
}

View File

@@ -1,20 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
{{template "base" .}}
</head>
<body>
{{define "content"}}
<div class="container">
<div class="row">
<div class="col">
<h1>This is de about page</h1>
<p>This is some text</p>
</div>
</div>
</div>
</body>
</html>
{{end}}

View File

@@ -0,0 +1,19 @@
{{define "base"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
{{block "css" .}}
{{end}}
<body>
{{block "content" .}}
{{end}}
{{block "js" .}}
{{end}}
</body>
</html>
{{end}}

View File

@@ -1,18 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
{{template "base" .}}
{{define "content"}}
<div class="container">
<div class="row">
<div class="col">
<h1>This is de home page</h1>
<p>Some text</p>
</div>
</div>
</div>
</body>
</html>
{{end}}