GetAsset templates function
This commit is contained in:
parent
a9f287412f
commit
072b28e618
1 changed files with 31 additions and 0 deletions
31
app.go
31
app.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue