func (p Pongo2Common) Instance(name string, data interface{}) render.Render { tpl := pongo2.Must(pongo2.FromCache(path.Join(p.BasePath, name))) return &Pongo2{ Template: tpl, Name: name, Data: data, } }
// Instance should return a new Pongo2Render struct per request and prepare // the template by either loading it from disk or using pongo2's cache. func (p Pongo2Render) Instance(name string, data interface{}) render.Render { var template *pongo2.Template filename := path.Join(p.Options.TemplateDir, name) // always read template files from disk if in debug mode, use cache otherwise. if gin.Mode() == "debug" { template = pongo2.Must(pongo2.FromFile(filename)) } else { template = pongo2.Must(pongo2.FromCache(filename)) } return Pongo2Render{ Template: template, Context: data.(pongo2.Context), Options: p.Options, } }
func Pongo2() HandlerFunc { return func(c *Context) { c.Next() templateName, templateNameError := c.Get("template") templateNameValue, isString := templateName.(string) if templateNameError == nil && isString { templateData, templateDataError := c.Get("data") var template = pongo2.Must(pongo2.FromCache(templateNameValue)) err := template.ExecuteWriter(getContext(templateData, templateDataError), c.Writer) if err != nil { http.Error(c.Writer, err.Error(), http.StatusInternalServerError) } } } }