ytdl-web/web/views.go

39 lines
654 B
Go
Raw Normal View History

2023-04-14 11:58:32 -04:00
package web
import (
"embed"
2023-04-14 16:07:57 -04:00
"encoding/json"
2023-04-14 11:58:32 -04:00
"html/template"
"net/http"
2023-04-14 16:07:57 -04:00
"net/url"
2023-04-14 11:58:32 -04:00
"github.com/gofiber/template/html"
)
//go:embed views/*
var viewsfs embed.FS
func ViewsEngine() *html.Engine {
engine := html.NewFileSystem(http.FS(viewsfs), ".html")
engine.AddFunc(
2023-04-14 16:07:57 -04:00
"unsafe", func(s string) template.HTML {
2023-04-14 11:58:32 -04:00
return template.HTML(s)
},
)
2023-04-14 16:07:57 -04:00
engine.AddFunc(
"queryEscape", func(s string) string {
return url.QueryEscape(s)
},
)
engine.AddFunc(
"jsonMarshal", func(s any) (string, error) {
j, err := json.MarshalIndent(s, "", " ")
if err != nil {
return "", err
}
return string(j), nil
},
)
2023-04-14 11:58:32 -04:00
return engine
}