/
context.go
112 lines (96 loc) · 2.44 KB
/
context.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
111
112
package multitemplate
import (
"bytes"
"html/template"
"io"
"strings"
)
func NewContext(data interface{}) *Context {
c := &Context{}
c.Yields = make(map[string]string)
c.Blocks = make(map[string]RenderedBlock)
c.Dot = data
c.output = newPouchWriter()
return c
}
type RenderedBlock struct {
Content template.HTML
Type Ruleset
}
type Ruleset string
const (
User Ruleset = ""
HTML Ruleset = "<"'."
CSS Ruleset = "ZgotmplZ"
JS Ruleset = `"\u003c\"'."`
)
// A Context allows you to setup more specialized template executions,
// like those involving layouts
type Context struct {
// Main template to be rendered, not layout
Main string
mainContent RenderedBlock
// Layout for rendering
Layout string
executingLayout bool
currentMode string
// Templates set for yields
Yields map[string]string
// Blocks (pre-rendered HTML)
Blocks map[string]RenderedBlock
// Base RenderArgs for the template
Dot interface{}
// Name of the parent template
parent string
// internal, for exec
tmpl *Template
// blocks need this
output *pouchWriter
}
func (c *Context) openableScope() bool {
canNest := !c.output.nesting()
hasParent := c.parent != ""
forthcomingLayout := c.Layout != "" && !c.executingLayout
inactiveView := strings.TrimSpace(c.output.root.String()) == ""
openableTemplate := hasParent || (forthcomingLayout && inactiveView)
return canNest && openableTemplate
}
func (c *Context) exec(name string, dot interface{}) (RenderedBlock, error) {
b := bytes.Buffer{}
// Replace the output buffer so we don't have stale data hanging around
// We need to have the rest of the context hanging around.
temp := c.output
c.output = newPouchWriter()
e := c.tmpl.ExecuteTemplate(&b, name, dot)
c.output = temp
return RenderedBlock{template.HTML(b.String()), HTML}, e
}
func (c *Context) execWithFallback(name string, f fallback, dot interface{}) (RenderedBlock, error) {
if c.Yields[name] != "" {
return c.exec(c.Yields[name], dot)
}
if rb, ok := c.Blocks[name]; ok {
return rb, nil
}
return c.exec(string(f), dot)
}
func (c *Context) Close(w io.Writer) error {
if c.parent != "" {
temp := c.parent
c.parent = ""
for temp != "" {
c.output.Reset()
e := c.tmpl.Tmpl.ExecuteTemplate(c.output, temp, c.Dot)
if e != nil {
return e
}
temp = c.parent
}
}
if c.output.err != nil {
return c.output.err
}
_, e := io.WriteString(w, c.output.root.String())
return e
}
type fallback string