-
Notifications
You must be signed in to change notification settings - Fork 10
/
compat.go
110 lines (90 loc) · 1.97 KB
/
compat.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package kasia
import (
"io"
"io/ioutil"
"bytes"
)
// Renderowanie do tekstu lub tablicy bajtow
func (tpl *Template) RenderBytes(ctx ...interface{}) ([]byte, error) {
var buf bytes.Buffer
err := tpl.Run(&buf, ctx...)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (tpl *Template) RenderString(ctx ...interface{}) (string, error) {
bytes, err := tpl.RenderBytes(ctx...)
return string(bytes), err
}
func RenderBytes(txt string, strict bool, ctx ...interface{}) ([]byte, error) {
tpl := New()
tpl.Strict = strict
err := tpl.Parse([]byte(txt))
if err != nil {
return nil, err
}
return tpl.RenderBytes(ctx...)
}
func RenderString(txt string, strict bool, ctx ...interface{}) (string, error) {
bytes, err := RenderBytes(txt, strict, ctx...)
return string(bytes), err
}
// Funkcje i metody kompatybilne z Go template
func (tpl *Template) Execute(wr io.Writer, data interface{}) error {
return tpl.Run(wr, data)
}
func (tpl *Template) ParseFile(filename string) (err error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return
}
return tpl.Parse(data)
}
func Parse(txt string) (tpl *Template, err error) {
tpl = New()
err = tpl.Parse([]byte(txt))
if err != nil {
tpl = nil
}
return
}
func MustParse(txt string) (tpl *Template) {
tpl, err := Parse(txt)
if err != nil {
panic(err)
}
return
}
func ParseFile(filename string) (tpl *Template, err error) {
tpl = New()
err = tpl.ParseFile(filename)
if err != nil {
tpl = nil
}
return
}
func MustParseFile(filename string) (tpl *Template) {
tpl, err := ParseFile(filename)
if err != nil {
panic(err)
}
return
}
// Funkcje i metody kompatybilne z mustache.go
func (tpl *Template) Render(ctx ...interface{}) (out string) {
var err error
out, err = tpl.RenderString(ctx...)
if err != nil {
panic(err)
}
return
}
func Render(txt string, ctx ...interface{}) (out string) {
var err error
out, err = RenderString(txt, false, ctx...)
if err != nil {
panic(err)
}
return
}