-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
69 lines (59 loc) · 1.66 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
package jaws
import (
"fmt"
"html/template"
"io"
"strings"
"github.com/linkdata/jaws/what"
)
type Template struct {
Dot interface{}
*template.Template
}
var _ UI = Template{} // statically ensure interface is defined
var _ EventHandler = Template{} // statically ensure interface is defined
// GetTemplate resolves 'v' to a *template.Template or panics.
func (rq *Request) MustTemplate(v interface{}) (tp *template.Template) {
switch v := v.(type) {
case *template.Template:
tp = v
case string:
if rq.Jaws.Template == nil {
panic(fmt.Errorf("Jaws.Template is nil, can't look up %q", v))
}
tp = rq.Jaws.Template.Lookup(v)
}
if tp == nil {
panic(fmt.Errorf("expected template or string, not %v", v))
}
return
}
func (rq *Request) MakeTemplate(templ, dot interface{}) Template {
return Template{Template: rq.MustTemplate(templ), Dot: dot}
}
func (t Template) String() string {
return fmt.Sprintf("{%q, %s}", t.Template.Name(), TagString(t.Dot))
}
func (t Template) JawsRender(e *Element, w io.Writer, params []interface{}) error {
if expandedtags, err := TagExpand(e.Request, t.Dot); err != ErrIllegalTagType {
e.Request.tagExpanded(e, expandedtags)
}
var sb strings.Builder
for _, s := range e.ParseParams(params) {
sb.WriteByte(' ')
sb.WriteString(string(s))
}
attrs := template.HTMLAttr(sb.String()) // #nosec G203
return t.Execute(w, With{
Element: e,
RequestWriter: e.Request.Writer(w),
Dot: t.Dot,
Attrs: attrs,
})
}
func (t Template) JawsUpdate(e *Element) {
// does nothing
}
func (t Template) JawsEvent(e *Element, wht what.What, val string) error {
return callEventHandler(t.Dot, e, wht, val)
}