-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplates.go
More file actions
50 lines (43 loc) · 1.1 KB
/
Copy pathtemplates.go
File metadata and controls
50 lines (43 loc) · 1.1 KB
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
package main
// templates module
//
// Copyright (c) 2019 - Valentin Kuznetsov <vkuznet@gmail.com>
//
import (
"bytes"
"html/template"
"path/filepath"
)
// TmplRecord represent template record
type TmplRecord map[string]interface{}
// consume list of templates and release their full path counterparts
func fileNames(tdir string, filenames ...string) []string {
flist := []string{}
for _, fname := range filenames {
flist = append(flist, filepath.Join(tdir, fname))
}
return flist
}
// parse template with given data
func parseTmpl(tdir, tmpl string, data interface{}) string {
buf := new(bytes.Buffer)
filenames := fileNames(tdir, tmpl)
t := template.Must(template.New(tmpl).ParseFiles(filenames...))
err := t.Execute(buf, data)
if err != nil {
panic(err)
}
return buf.String()
}
// Templates structure holds our HTML template
type Templates struct {
html string
}
// Tmpl method for Templates structure
func (t Templates) Tmpl(tdir, tfile string, tmplData map[string]interface{}) string {
if t.html != "" {
return t.html
}
t.html = parseTmpl(Config.Templates, tfile, tmplData)
return t.html
}