summaryrefslogtreecommitdiff
path: root/vendor/github.com/a-h/templ/fragment.go
blob: 0b8624d6f043f436eb4b2de295be8e48a055f9ec (plain)
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
70
package templ

import (
	"context"
	"io"
	"slices"
)

// RenderFragments renders the specified fragments to w.
func RenderFragments(ctx context.Context, w io.Writer, c Component, ids ...any) error {
	ctx = context.WithValue(ctx, fragmentContextKey, &FragmentContext{
		W:   w,
		IDs: ids,
	})
	return c.Render(ctx, io.Discard)
}

type fragmentContextKeyType int

const fragmentContextKey fragmentContextKeyType = iota

// FragmentContext is used to control rendering of fragments within a template.
type FragmentContext struct {
	W      io.Writer
	IDs    []any
	Active bool
}

// Fragment defines a fragment within a template that can be rendered conditionally based on the id.
// You can use it to render a specific part of a page, e.g. to reduce the amount of HTML returned from a HTMX-initiated request.
// Any non-matching contents of the template are rendered, but discarded by the FramentWriter.
func Fragment(id any) Component {
	return &fragment{
		ID: id,
	}
}

type fragment struct {
	ID any
}

func (f *fragment) Render(ctx context.Context, w io.Writer) (err error) {
	// If not in a fragment context, if we're a child fragment, or in a mismatching fragment context, render children normally.
	fragmentCtx := getFragmentContext(ctx)
	if fragmentCtx == nil || fragmentCtx.Active || !slices.Contains(fragmentCtx.IDs, f.ID) {
		return GetChildren(ctx).Render(ctx, w)
	}

	// Instruct child fragments to render their contents normally, because the writer
	// passed to them is already the FragmentContext's writer.
	fragmentCtx.Active = true
	defer func() {
		fragmentCtx.Active = false
	}()
	return GetChildren(ctx).Render(ctx, fragmentCtx.W)
}

// getFragmentContext retrieves the FragmentContext from the provided context. It returns nil if no
// FragmentContext is found or if the context value is of an unexpected type.
func getFragmentContext(ctx context.Context) *FragmentContext {
	ctxValue := ctx.Value(fragmentContextKey)
	if ctxValue == nil {
		return nil
	}
	v, ok := ctxValue.(*FragmentContext)
	if !ok {
		return nil
	}
	return v
}