newsbox/actions/render.go
Adriano Caloiaro 8ae801ae5f
Initial commit
2023-02-15 08:57:48 -08:00

58 lines
1.3 KiB
Go

package actions
import (
"bytes"
"newsbox/public"
"newsbox/templates"
"text/template"
"github.com/gobuffalo/buffalo/render"
)
var r *render.Engine
func init() {
r = render.New(render.Options{
// HTML layout to be used for all HTML requests:
HTMLLayout: "application.plush.html",
TemplateEngines: map[string]render.TemplateEngine{
".tmpl": GoTemplateEngine,
},
// fs.FS containing templates
TemplatesFS: templates.FS(),
// fs.FS containing assets
AssetsFS: public.FS(),
// Add template helpers here:
Helpers: render.Helpers{
// for non-bootstrap form helpers uncomment the lines
// below and import "github.com/gobuffalo/helpers/forms"
// forms.FormKey: forms.Form,
// forms.FormForKey: forms.FormFor,
},
})
}
func GoTemplateEngine(input string, data map[string]interface{}, helpers map[string]interface{}) (string, error) {
// since go templates don't have the concept of an optional map argument like Plush does
// add this "null" map so it can be used in templates like this:
// {{ partial "flash.html" .nilOpts }}
data["nilOpts"] = map[string]interface{}{}
t := template.New(input)
if helpers != nil {
t = t.Funcs(helpers)
}
t, err := t.Parse(input)
if err != nil {
return "", err
}
bb := &bytes.Buffer{}
err = t.Execute(bb, data)
return bb.String(), err
}