GetAsset templates function

This commit is contained in:
DarkCat09 2023-10-16 10:10:19 +04:00
parent a9f287412f
commit 072b28e618
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3

31
app.go
View file

@ -1,6 +1,10 @@
package main package main
import ( import (
"io/fs"
"path/filepath"
"strings"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2" "github.com/gofiber/template/html/v2"
) )
@ -21,5 +25,32 @@ func main() {
func setupTemplates() *html.Engine { func setupTemplates() *html.Engine {
engine := html.New("./views", ".html") engine := html.New("./views", ".html")
// TODO: enable only on DEV=true in env vars
engine.Reload(true)
assets := make(map[string]string)
filepath.WalkDir("./public/assets", func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
name := d.Name()
parts := strings.Split(name, ".")
var key string
if len(parts) < 2 {
key = name
} else {
key = parts[0] + "." + parts[len(parts)-1]
}
assets[key] = path
}
return nil
})
engine.AddFunc("GetAsset", func(key string) string {
return assets[key]
})
// TODO: i18n
return engine return engine
} }