Browse Source

Add Context to Plugin.Inject

This allows to access the request metadata and the session from injectors.
master
Simon Ser 4 years ago
parent
commit
020e27fe45
No known key found for this signature in database GPG Key ID: FDE7BE0E88F5E48
4 changed files with 7 additions and 7 deletions
  1. +1
    -1
      plugin.go
  2. +4
    -4
      plugin_go.go
  3. +1
    -1
      plugin_lua.go
  4. +1
    -1
      template.go

+ 1
- 1
plugin.go View File

@@ -18,7 +18,7 @@ type Plugin interface {
SetRoutes(group *echo.Group)
// Inject is called prior to rendering a template. It can extend the
// template data by setting new items in the Extra map.
Inject(name string, data RenderData) error
Inject(ctx *Context, name string, data RenderData) error
// Close is called when the plugin is unloaded.
Close() error
}


+ 4
- 4
plugin_go.go View File

@@ -40,14 +40,14 @@ func (p *goPlugin) SetRoutes(group *echo.Group) {
group.Static("/plugins/"+p.p.Name+"/assets", pluginDir+"/"+p.p.Name+"/public/assets")
}

func (p *goPlugin) Inject(name string, data RenderData) error {
func (p *goPlugin) Inject(ctx *Context, name string, data RenderData) error {
if f, ok := p.p.injectFuncs["*"]; ok {
if err := f(data); err != nil {
if err := f(ctx, data); err != nil {
return err
}
}
if f, ok := p.p.injectFuncs[name]; ok {
return f(data)
return f(ctx, data)
}
return nil
}
@@ -114,7 +114,7 @@ func (p *GoPlugin) TemplateFuncs(funcs template.FuncMap) {
}

// InjectFunc is a function that injects data prior to rendering a template.
type InjectFunc func(data RenderData) error
type InjectFunc func(ctx *Context, data RenderData) error

// Inject registers a function to execute prior to rendering a template. The
// special name "*" matches any template.


+ 1
- 1
plugin_lua.go View File

@@ -86,7 +86,7 @@ func (p *luaPlugin) inject(name string, data RenderData) error {
return nil
}

func (p *luaPlugin) Inject(name string, data RenderData) error {
func (p *luaPlugin) Inject(ctx *Context, name string, data RenderData) error {
if err := p.inject("*", data); err != nil {
return err
}


+ 1
- 1
template.go View File

@@ -87,7 +87,7 @@ func (r *renderer) Render(w io.Writer, name string, data interface{}, ectx echo.
ctx := ectx.Get("context").(*Context)

for _, plugin := range ctx.Server.Plugins {
if err := plugin.Inject(name, data.(RenderData)); err != nil {
if err := plugin.Inject(ctx, name, data.(RenderData)); err != nil {
return fmt.Errorf("failed to run plugin '%v': %v", plugin.Name(), err)
}
}


Loading…
Cancel
Save