add support for pongo2.TemplateLoader

This commit is contained in:
Darko Luketic 2020-03-19 21:10:06 +01:00
parent c562e32f91
commit 1977a5f4ff
2 changed files with 15 additions and 4 deletions

View File

@ -1,3 +1,9 @@
# pongo2 for gin
github.com/flosch/pongo2 master branch is used.
If you'd like to use pongo2.v3 see github.com/dalu/pongo2v3
```go ```go
package main package main
@ -11,12 +17,12 @@
func main () { func main () {
r := gin.Default() r := gin.Default()
pr := ginpongo2.New("templates", gin.IsDebugging())
// default is the TemplateSet name
// templates is the basePath // templates is the basePath
// gin.IsDebugging() means if gin is in debug/dev mode then templates // gin.IsDebugging() means if gin is in debug/dev mode then templates
// will always be reloaded from disk, otherwise from cache and never // will always be reloaded from disk, otherwise from cache and never
// reloaded. // reloaded.
pr := ginpongo2.New("templates", gin.IsDebugging())
ginpongo2.Suffix = ".html.twig" // this is default ginpongo2.Suffix = ".html.twig" // this is default
// when ever you render something reference it by prefix // when ever you render something reference it by prefix
// e.g. frontpage = frontpage.html.twig // e.g. frontpage = frontpage.html.twig
@ -33,3 +39,5 @@
r.Run(":8080") r.Run(":8080")
} }
``` ```
You can also supply TemplateLoaders. See [pongo2.TemplateLoader](https://pkg.go.dev/github.com/flosch/pongo2?tab=doc#TemplateLoader) documentation. It allows you to for example embed resources from a virtual filesystem.

View File

@ -46,7 +46,7 @@ func (r *PongoRenderer) WriteContentType(w http.ResponseWriter) {
} }
} }
func New(basedir string, debug bool) *Pongo { func New(basedir string, debug bool, loaders ...pongo2.TemplateLoader) *Pongo {
p := new(Pongo) p := new(Pongo)
p.set = pongo2.NewSet("html") p.set = pongo2.NewSet("html")
p.set.Debug = debug p.set.Debug = debug
@ -54,5 +54,8 @@ func New(basedir string, debug bool) *Pongo {
basedir = basedir + "/" basedir = basedir + "/"
} }
p.basedir = basedir p.basedir = basedir
if len(loaders) > 0 {
p.set.AddLoader(loaders...)
}
return p return p
} }