-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
86 lines (76 loc) · 2.01 KB
/
templates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"bytes"
"html/template"
"io"
"io/ioutil"
"path/filepath"
"github.com/kjk/u"
)
var (
tmplMainPage = "mainpage.tmpl.html"
tmplArticle = "article.tmpl.html"
tmplArchive = "archive.tmpl.html"
tmplGenerateUniqueID = "generate-unique-id.tmpl.html"
tmplGoCookBook = "go-cookbook.tmpl.html"
tmplChangelog = "changelog.tmpl.html"
tmpl404 = "404.tmpl.html"
templateNames = []string{
tmplMainPage,
tmplArticle,
tmplArchive,
tmplGenerateUniqueID,
tmplGoCookBook,
tmplChangelog,
tmpl404,
"analytics.tmpl.html",
}
templatePaths []string
templates *template.Template
// dirs to search when looking for templates
tmplDirs = []string{
"www",
filepath.Join("www", "tmpl"),
filepath.Join("www", "tools"),
filepath.Join("www", "static"),
}
)
func findTemplate(name string) string {
for _, dir := range tmplDirs {
path := filepath.Join(dir, name)
if u.FileExists(path) {
return path
}
}
panicIf(true, "didn't find tamplate %s in dirs %v", name, tmplDirs)
return ""
}
func loadTemplates() {
for _, name := range templateNames {
path := findTemplate(name)
templatePaths = append(templatePaths, path)
}
templates = template.Must(template.ParseFiles(templatePaths...))
}
func execTemplateToFile(path string, templateName string, model interface{}) error {
var buf bytes.Buffer
err := templates.ExecuteTemplate(&buf, templateName, model)
panicIfErr(err)
err = ioutil.WriteFile(path, buf.Bytes(), 0644)
return err
}
func execTemplateToWriter(name string, data interface{}, w io.Writer) error {
loadTemplates()
return templates.ExecuteTemplate(w, name, data)
}
func execTemplate(path string, tmplName string, d interface{}, w io.Writer) error {
// this code path is for the preview on demand server
if w != nil {
return execTemplateToWriter(tmplName, d, w)
}
// this code path is for generating static files
netPath := netlifyPath(path)
err := execTemplateToFile(netPath, tmplName, d)
panicIfErr(err)
return nil
}