-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathslot.go
50 lines (41 loc) · 900 Bytes
/
slot.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
package web
import (
"context"
"fmt"
h "github.com/theplant/htmlgo"
)
type SlotBuilder struct {
tag *h.HTMLTagBuilder
scope string
name string
}
func Slot(children ...h.HTMLComponent) (r *SlotBuilder) {
r = &SlotBuilder{
tag: h.Tag("template").Children(children...),
}
return
}
func (b *SlotBuilder) Scope(v string) (r *SlotBuilder) {
b.scope = v
return b
}
func (b *SlotBuilder) Name(v string) (r *SlotBuilder) {
b.name = v
return b
}
func (b *SlotBuilder) Children(comps ...h.HTMLComponent) (r *SlotBuilder) {
b.tag.Children(comps...)
return b
}
func (b *SlotBuilder) MarshalHTML(ctx context.Context) (r []byte, err error) {
if len(b.name) == 0 {
panic("Slot(...).Name(name) required")
}
attrName := fmt.Sprintf("v-slot:%s", b.name)
if len(b.scope) == 0 {
b.tag.Attr(attrName, true)
} else {
b.tag.Attr(attrName, b.scope)
}
return b.tag.MarshalHTML(ctx)
}