forked from go-fuego/fuego
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.go
More file actions
63 lines (53 loc) · 1.53 KB
/
html.go
File metadata and controls
63 lines (53 loc) · 1.53 KB
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
package fuego
import (
"context"
"fmt"
"html/template"
"io"
)
// CtxRenderer can be used with [github.com/a-h/templ]
// Example:
//
// func getRecipes(ctx fuego.ContextNoBody) (fuego.CtxRenderer, error) {
// recipes, err := ctx.store.GetRecipes(ctx.Context())
// if err != nil {
// return nil, err
// }
//
// return recipeComponent(recipes), nil // recipeComponent is templ component
// }
type CtxRenderer interface {
Render(context.Context, io.Writer) error
}
// Templ is a shortcut for [CtxRenderer], which can be used with [github.com/a-h/templ]
type Templ = CtxRenderer
// Renderer can be used with [github.com/maragudk/gomponents]
// Example:
//
// func getRecipes(ctx fuego.ContextNoBody) (fuego.CtxRenderer, error) {
// recipes, err := ctx.store.GetRecipes(ctx.Context())
// if err != nil {
// return nil, err
// }
//
// return recipeComponent(recipes), nil // recipeComponent is gomponents component
// }
type Renderer interface {
Render(io.Writer) error
}
// Gomponent is a shortcut for [Renderer], which can be used with [github.com/maragudk/gomponents]
type Gomponent = Renderer
// HTML is a marker type used to differentiate between a string response and an HTML response.
// To use templating, use [Ctx.Render].
type HTML string
// H is a shortcut for map[string]any
type H map[string]any
// loadTemplates
func (s *Server) loadTemplates(patterns ...string) error {
tmpl, err := template.ParseFS(s.fs, patterns...)
if err != nil {
return fmt.Errorf("failed to parse templates: %w", err)
}
s.template = tmpl
return nil
}