Building a template cache
This commit is contained in:
@@ -1,17 +1,75 @@
|
|||||||
package render
|
package render
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var functions = template.FuncMap{}
|
||||||
|
|
||||||
// RenderTemplate renders templates using html/template
|
// RenderTemplate renders templates using html/template
|
||||||
func RenderTemplate(w http.ResponseWriter, tmpl string) {
|
func RenderTemplate(w http.ResponseWriter, tmpl string) {
|
||||||
parsedTemplate, _ := template.ParseFiles("./templates/" + tmpl)
|
|
||||||
err := parsedTemplate.Execute(w, nil)
|
tc, err := CreateTemplateCache()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("error parsing template:", err)
|
log.Fatal(err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,12 @@
|
|||||||
<!DOCTYPE html>
|
{{template "base" .}}
|
||||||
<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>
|
{{define "content"}}
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<h1>This is de about page</h1>
|
<h1>This is de about page</h1>
|
||||||
<p>This is some text</p>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
{{end}}
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
19
templates/base.layout.tmpl
Normal file
19
templates/base.layout.tmpl
Normal 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}}
|
||||||
@@ -1,18 +1,12 @@
|
|||||||
<!DOCTYPE html>
|
{{template "base" .}}
|
||||||
<html lang="en">
|
|
||||||
<head>
|
{{define "content"}}
|
||||||
<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>
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<h1>This is de home page</h1>
|
<h1>This is de home page</h1>
|
||||||
|
<p>Some text</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
{{end}}
|
||||||
</html>
|
|
||||||
Reference in New Issue
Block a user