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