2020-02-20 16:28:55 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"html/template"
|
2021-08-08 13:33:04 +03:00
|
|
|
"io/fs"
|
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
2020-02-20 16:28:55 +03:00
|
|
|
)
|
|
|
|
|
2021-08-08 13:33:04 +03:00
|
|
|
type TemplateMap map[string]*template.Template
|
|
|
|
|
2020-02-20 16:28:55 +03:00
|
|
|
func Json(data interface{}) template.JS {
|
|
|
|
d, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return template.JS(d)
|
|
|
|
}
|
2021-01-17 11:24:09 +03:00
|
|
|
|
|
|
|
func ToRunes(s string) (r []string) {
|
|
|
|
for _, c := range []rune(s) {
|
|
|
|
r = append(r, string(c))
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
2021-08-08 13:33:04 +03:00
|
|
|
|
|
|
|
func LoadTemplates(templateFs fs.FS, funcs template.FuncMap) (TemplateMap, error) {
|
|
|
|
tpls := template.New("").Funcs(funcs)
|
|
|
|
templates := make(map[string]*template.Template)
|
|
|
|
|
|
|
|
files, err := fs.ReadDir(templateFs, ".")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
tplName := file.Name()
|
|
|
|
if file.IsDir() || path.Ext(tplName) != ".html" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
templateFile, err := templateFs.Open(tplName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
templateData, err := ioutil.ReadAll(templateFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
templateFile.Close()
|
|
|
|
|
|
|
|
tpl, err := tpls.New(tplName).Parse(string(templateData))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
templates[tplName] = tpl
|
|
|
|
}
|
|
|
|
|
|
|
|
return templates, nil
|
|
|
|
}
|