// Render takes a Beego context, template name and a Context (map[string]interface{}). // The template is parsed and cached, and gets executed into beegoCtx's ResponseWriter. // // Templates are looked up in `templates/` instead of Beego's default `views/` so that // Beego doesn't attempt to load and parse our templates with `html/template`. func Render(beegoCtx *context.Context, tmpl string, ctx Context) error { template, err := p2.FromCache(path.Join(templateDir, tmpl)) if err != nil { panic(err) } var pCtx p2.Context if ctx == nil { pCtx = p2.Context{} } else { pCtx = p2.Context(ctx) } if xsrf, ok := beegoCtx.GetSecureCookie(beego.BConfig.WebConfig.XSRFKey, "_xsrf"); ok { pCtx["_xsrf"] = xsrf } // Only override "flash" if it hasn't already been set in Context if _, ok := ctx["flash"]; !ok { if ctx == nil { ctx = Context{} } ctx["flash"] = readFlash(beegoCtx) } return template.ExecuteWriter(pCtx, beegoCtx.ResponseWriter) }
// Same as Render() but returns a string func RenderString(tmpl string, ctx Context) (string, error) { template, err := p2.FromCache(path.Join(templateDir, tmpl)) if err != nil { panic(err) } var pCtx p2.Context if ctx == nil { pCtx = p2.Context{} } else { pCtx = p2.Context(ctx) } // str, _ := template.Execute(pCtx) // return str return template.Execute(pCtx) }