slot.go 785 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package web
  2. import (
  3. "context"
  4. "fmt"
  5. h "github.com/theplant/htmlgo"
  6. )
  7. type SlotBuilder struct {
  8. tag *h.HTMLTagBuilder
  9. scope string
  10. name string
  11. }
  12. func Slot(children ...h.HTMLComponent) (r *SlotBuilder) {
  13. r = &SlotBuilder{
  14. tag: h.Tag("template").Children(children...),
  15. }
  16. return
  17. }
  18. func (b *SlotBuilder) Scope(v string) (r *SlotBuilder) {
  19. b.scope = v
  20. return b
  21. }
  22. func (b *SlotBuilder) Name(v string) (r *SlotBuilder) {
  23. b.name = v
  24. return b
  25. }
  26. func (b *SlotBuilder) MarshalHTML(ctx context.Context) (r []byte, err error) {
  27. if len(b.name) == 0 {
  28. panic("Slot(...).Name(name) required")
  29. }
  30. attrName := fmt.Sprintf("v-slot:%s", b.name)
  31. if len(b.scope) == 0 {
  32. b.tag.Attr(attrName, true)
  33. } else {
  34. b.tag.Attr(attrName, b.scope)
  35. }
  36. return b.tag.MarshalHTML(ctx)
  37. }