-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
55 lines (48 loc) · 1.14 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
package main
import (
"bytes"
"fmt"
"html/template"
"io"
)
type PageTemplateSetup struct {
baseDir string
templateFileList []string
content interface{}
}
func getPartials(fm FileMap) []string {
var result []string
for key := range fm.files {
if len(key) < 10 {
continue
}
if key[:8] == "partials" {
result = append(result, fm.skipPath+key+".html")
}
}
return result
}
func getTemplateFileList(fm FileMap, page PageTemplateSetup) []string {
fileNames := getPartials(fm)
for _, name := range page.templateFileList {
fileNames = append(fileNames, page.baseDir+name+".html")
}
return fileNames
}
func createTemplate(absoluteTemplatePathes []string) (*template.Template, error) {
return template.ParseFiles(absoluteTemplatePathes...)
}
func createPageFromTemplate(wr io.Writer, page PageTemplateSetup, fm FileMap) {
t, err := createTemplate(getTemplateFileList(fm, page))
if err != nil {
fmt.Println(err.Error())
return
}
buf := new(bytes.Buffer)
err = t.ExecuteTemplate(buf, "baseof", page.content)
if err != nil {
println(err.Error())
}
fmt.Println("Temlpate: " + buf.String())
wr.Write(buf.Bytes())
}