-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
69 lines (59 loc) · 1.92 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 {
Name string // Template name to be looked up using jaws.LookupTemplate()
Dot any // Dot value to place in With structure
}
var _ UI = Template{} // statically ensure interface is defined
var _ EventHandler = Template{} // statically ensure interface is defined
func (t Template) String() string {
return fmt.Sprintf("{%q, %s}", t.Name, TagString(t.Dot))
}
func (t Template) JawsRender(e *Element, wr 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
var auth Auth
auth = defaultAuth{}
if f := e.Request.Jaws.MakeAuth; f != nil {
auth = f(e.Request)
}
return e.Request.Jaws.LookupTemplate(t.Name).Execute(wr, With{
Element: e,
RequestWriter: e.Request.Writer(wr),
Dot: t.Dot,
Attrs: attrstr,
Auth: auth,
})
}
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 callEventHandlers(t.Dot, e, wht, val)
}
// NewTemplate simply returns a Template{} with the members set.
//
// Provided as convenience so as to not have to name the structure members.
func NewTemplate(name string, dot any) Template {
return Template{Name: name, Dot: dot}
}
// Template renders the given template using jaws.With{Dot: dot} as data.
//
// The name argument is a string to be resolved to a *template.Template
// using Jaws.LookupTemplate().
func (rq RequestWriter) Template(name string, dot any, params ...any) error {
return rq.UI(NewTemplate(name, dot), params...)
}