mirror of
https://github.com/nkanaev/yarr.git
synced 2025-05-24 00:33:14 +00:00
46 lines
928 B
Go
46 lines
928 B
Go
package assets
|
|
|
|
import (
|
|
"embed"
|
|
"html/template"
|
|
"io"
|
|
"io/ioutil"
|
|
"io/fs"
|
|
"os"
|
|
)
|
|
|
|
type assetsfs struct {
|
|
embedded *embed.FS
|
|
templates map[string]*template.Template
|
|
}
|
|
|
|
var FS assetsfs
|
|
|
|
func (afs assetsfs) Open(name string) (fs.File, error) {
|
|
if afs.embedded != nil {
|
|
return afs.embedded.Open(name)
|
|
}
|
|
return os.DirFS("assets").Open(name)
|
|
}
|
|
|
|
func Render(path string, writer io.Writer, data interface{}) {
|
|
var tmpl *template.Template
|
|
tmpl, found := FS.templates[path]
|
|
if !found {
|
|
tmpl = template.Must(template.New(path).Delims("{%", "%}").Funcs(template.FuncMap{
|
|
"inline": func(svg string) template.HTML {
|
|
svgfile, _ := FS.Open("graphicarts/" + svg)
|
|
content, _ := ioutil.ReadAll(svgfile)
|
|
svgfile.Close()
|
|
return template.HTML(content)
|
|
},
|
|
}).ParseFS(FS, path))
|
|
FS.templates[path] = tmpl
|
|
}
|
|
tmpl.Execute(writer, data)
|
|
}
|
|
|
|
func init() {
|
|
FS.templates = make(map[string]*template.Template)
|
|
}
|