diff options
author | Simon Ser <contact@emersion.fr> | 2020-01-10 19:34:59 +0100 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2020-01-10 19:34:59 +0100 |
commit | c38b1d47f978bc36858b51b8da9159173eff69ff (patch) | |
tree | 0e072cba7d84d04f0b42231e3e647d306bff8b3b | |
parent | 24718f1ac4f892b0e304189ddc21825ff59fb28d (diff) | |
download | alps-c38b1d47f978bc36858b51b8da9159173eff69ff.tar.gz alps-c38b1d47f978bc36858b51b8da9159173eff69ff.zip |
Fix panic when injecting a template with nil data
-rw-r--r-- | template.go | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/template.go b/template.go index c8c3d6a..dfb9a97 100644 --- a/template.go +++ b/template.go @@ -88,8 +88,19 @@ func (r *renderer) Render(w io.Writer, name string, data interface{}, ectx echo. // ectx is the raw *echo.context, not our own *Context ctx := ectx.Get("context").(*Context) + var renderData RenderData + if data == nil { + renderData = &struct { BaseRenderData }{ *NewBaseRenderData(ctx) } + } else { + var ok bool + renderData, ok = data.(RenderData) + if !ok { + return fmt.Errorf("data passed to template '%v' doesn't implement RenderData", name) + } + } + for _, plugin := range ctx.Server.plugins { - if err := plugin.Inject(ctx, name, data.(RenderData)); err != nil { + if err := plugin.Inject(ctx, name, renderData); err != nil { return fmt.Errorf("failed to run plugin '%v': %v", plugin.Name(), err) } } |