-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtemplate.go
86 lines (69 loc) · 1.9 KB
/
template.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
// Jade.go - template engine. Package implements Jade-lang templates for generating Go html/template output.
package jade
import (
"bytes"
"io"
"net/http"
)
/*
Parse parses the template definition string to construct a representation of the template for execution.
Trivial usage:
package main
import (
"fmt"
"html/template"
"net/http"
"github.com/Joker/jade"
)
func handler(w http.ResponseWriter, r *http.Request) {
jadeTpl, _ := jade.Parse("jade", []byte("doctype 5\n html: body: p Hello #{.Word}!"))
goTpl, _ := template.New("html").Parse(jadeTpl)
goTpl.Execute(w, struct{ Word string }{"jade"})
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
Output:
<!DOCTYPE html><html><body><p>Hello jade!</p></body></html>
*/
func Parse(fname string, text []byte) (string, error) {
outTpl, err := New(fname).Parse(text)
if err != nil {
return "", err
}
bb := new(bytes.Buffer)
outTpl.WriteIn(bb)
return bb.String(), nil
}
// ParseFile parse the jade template file in given filename
func ParseFile(fname string) (string, error) {
text, err := ReadFunc(fname)
if err != nil {
return "", err
}
return Parse(fname, text)
}
// ParseWithFileSystem parse in context of a http.FileSystem (supports embedded files)
func ParseWithFileSystem(fname string, text []byte, fs http.FileSystem) (str string, err error) {
outTpl := New(fname)
outTpl.fs = fs
outTpl, err = outTpl.Parse(text)
if err != nil {
return "", err
}
bb := new(bytes.Buffer)
outTpl.WriteIn(bb)
return bb.String(), nil
}
// ParseFileFromFileSystem parse template file in context of a http.FileSystem (supports embedded files)
func ParseFileFromFileSystem(fname string, fs http.FileSystem) (str string, err error) {
text, err := readFile(fname, fs)
if err != nil {
return "", err
}
return ParseWithFileSystem(fname, text, fs)
}
func (t *tree) WriteIn(b io.Writer) {
t.Root.WriteIn(b)
}