forked from philippta/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
44 lines (35 loc) · 726 Bytes
/
main.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
package main
import (
"embed"
"fmt"
"log"
"os"
"github.com/simonedbarber/go-template/html/template"
)
//go:embed templates
var FS embed.FS
func main() {
funcs := template.FuncMap{
"props": props,
}
tmpl := template.Must(template.New("home.html").Funcs(funcs).ParseFS(FS,
"templates/components/card.html",
"templates/components/navbar.html",
"templates/home.html",
))
if err := tmpl.Execute(os.Stdout, "<script>alert()</script>"); err != nil {
log.Fatal(err)
}
}
func props(v ...any) map[string]any {
if len(v)%2 != 0 {
panic("uneven number of key/value pairs")
}
m := map[string]any{}
for i := 0; i < len(v); i += 2 {
key := fmt.Sprint(v[i])
val := v[i+1]
m[key] = val
}
return m
}