-
Notifications
You must be signed in to change notification settings - Fork 4
/
render_extends_test.go
72 lines (62 loc) · 1.59 KB
/
render_extends_test.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
package easytpl_test
import (
"bytes"
"fmt"
"os"
"testing"
"github.com/gookit/easytpl"
"github.com/gookit/goutil/testutil/assert"
)
func TestUseExtends(t *testing.T) {
bf := new(bytes.Buffer)
is := assert.New(t)
r := easytpl.NewExtends(easytpl.WithDebug, easytpl.DisableLayout)
r.LoadString("base", `{{ block "header" . }}header{{ end }}
{{ block "body" . }} default body{{ end }}
{{ block "footer" . }}footer{{ end }}`)
r.LoadBytes("home", []byte(`{{ extends "base" }}
{{ define "body" }} body: hi, {{.}} {{ end }}`))
t.Run("render base", func(t *testing.T) {
err := r.Execute(bf, "base", "inhere")
is.Nil(err)
is.Equal("header\n default body\nfooter", bf.String())
bf.Reset()
})
t.Run("render home", func(t *testing.T) {
err := r.Execute(bf, "home", "inhere")
is.Nil(err)
is.Equal("header\n body: hi, inhere \nfooter", bf.String())
})
}
func Example_extends() {
r := easytpl.NewExtends()
// load root
r.LoadStrings(map[string]string{
// layout template file
"layout.tpl": `{{ block "header" . }}header{{ end }}
{{ block "body" . }}default{{ end }}
{{ block "footer" . }}footer{{ end }}`,
// current page template
"home.tpl": `{{ extends "layout.tpl" }}
{{ define "body" }}hello {{.}}{{ end }}`,
})
fmt.Println("- render 'layout.tpl'")
err := r.Execute(os.Stdout, "layout.tpl", "inhere")
if err != nil {
panic(err)
}
fmt.Println("\n- render 'home.tpl'")
err = r.Execute(os.Stdout, "home.tpl", "inhere")
if err != nil {
panic(err)
}
// Output:
// - render 'layout.tpl'
// header
// default
// footer
// - render 'home.tpl'
// header
// hello inhere
// footer
}