-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgohtml.go
45 lines (39 loc) · 1.47 KB
/
gohtml.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
// Package goaster - Helpers for using toasts with `template/html`
package goaster
import (
"context"
"fmt"
"github.com/a-h/templ"
"html/template"
)
// HTMLGenerator provides functions for generating HTML code for toast notifications.
type HTMLGenerator struct{}
// NewHTMLGenerator creates a new instance of HTMLGenerator.
func NewHTMLGenerator() *HTMLGenerator {
return &HTMLGenerator{}
}
// GoasterCSSToGoHTML generates HTML code for the toast CSS and returns it as a template.HTML.
func (g *HTMLGenerator) GoasterCSSToGoHTML() (template.HTML, error) {
html, err := templ.ToGoHTML(context.Background(), GoasterCSS())
if err != nil {
return "", fmt.Errorf("failed to generate toast CSS: %v", err)
}
return html, nil
}
// GoasterJSToGoHTML generates HTML code for the toast JS and returns it as a template.HTML.
func (g *HTMLGenerator) GoasterJSToGoHTML(t *Toaster, jsOptions *GoasterJSOptions) (template.HTML, error) {
html, err := templ.ToGoHTML(context.Background(), GoasterJS(t, jsOptions))
if err != nil {
return "", fmt.Errorf("failed to generate toast JS: %v", err)
}
return html, nil
}
// DisplayAll generates HTML code for displaying the toast and returns it as a template.HTML.
func (g *HTMLGenerator) DisplayAll(t *Toaster) (template.HTML, error) {
// Generate HTML code for displaying the toast.
html, err := templ.ToGoHTML(context.Background(), t.RenderAll())
if err != nil {
return "", fmt.Errorf("failed to generate toast HTML: %v", err)
}
return html, nil
}