forked from Joker/jade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
62 lines (50 loc) · 1.16 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
// Jade.go - template engine. Package implements Jade-lang templates for generating Go html/template output.
package jade
import (
"io/ioutil"
"path/filepath"
)
/*
Parse parses the template definition string to construct a representation of the template for execution.
Trivial usage:
package main
import (
"fmt"
"github.com/Joker/jade"
)
func main() {
tpl, err := jade.Parse("tpl_name", "doctype 5: html: body: p Hello world!")
if err != nil {
fmt.Printf("Parse error: %v", err)
return
}
fmt.Printf( "Output:\n\n%s", tpl )
}
Output:
<!DOCTYPE html>
<html>
<body>
<p>Hello world!</p>
</body>
</html>
*/
func Parse(name, text string) (string, error) {
outTpl, err := newTree(name).Parse(text, LeftDelim, RightDelim, make(map[string]*tree))
if err != nil {
return "", err
}
return outTpl.String(), nil
}
// ParseFile parse the jade template file in given filename
func ParseFile(filename string) (string, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
s := string(b)
name := filepath.Base(filename)
return Parse(name, s)
}
func (t *tree) String() string {
return t.Root.String()
}