mirror of
https://github.com/acaloiaro/newsbox
synced 2026-07-21 18:29:15 +00:00
59 lines
1.3 KiB
Go
59 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
|
||
|
|
}
|