-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
55 lines (44 loc) · 1.25 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
package jaws
import (
"fmt"
"html/template"
"io"
"strings"
"github.com/linkdata/jaws/what"
)
type Template struct {
Dot interface{}
*template.Template
}
// 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:
tp = rq.Jaws.Template.Lookup(v)
}
if tp == nil {
panic(fmt.Errorf("expected template, 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))
}
var _ UI = (*Template)(nil) // statically ensure interface is defined
func (t Template) JawsRender(e *Element, w io.Writer, params []interface{}) {
e.Tag(t.Dot)
attrs := parseParams(e, params)
maybePanic(t.Execute(w, With{Element: e, Dot: t.Dot, Attrs: strings.Join(attrs, " ")}))
}
func (t Template) JawsUpdate(e *Element) {
// does nothing
}
var _ EventHandler = (*Template)(nil) // statically ensure interface is defined
func (t Template) JawsEvent(e *Element, wht what.What, val string) error {
return callEventHandler(t.Dot, e, wht, val)
}