From 733304c88f51df710174fd74c86c24c79fcf5b47 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 17 Dec 2019 13:15:10 +0100 Subject: [PATCH] Allow Lua plugins to inject all templates --- README.md | 2 +- plugin_lua.go | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e5fb1d5..847298b 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ The entry point is at `plugins//main.lua`. API: * `koushin.on_render(name, f)`: prior to rendering the template `name`, call - `f` with the template data + `f` with the template data (the special name `*` matches all templates) * `koushin.set_filter(name, f)`: set a template function * `koushin.set_route(method, path, f)`: register a new HTTP route, `f` will be called with the HTTP context diff --git a/plugin_lua.go b/plugin_lua.go index b02752d..638f17c 100644 --- a/plugin_lua.go +++ b/plugin_lua.go @@ -68,7 +68,7 @@ func (p *luaPlugin) setRoute(l *lua.LState) int { return 0 } -func (p *luaPlugin) Inject(name string, data interface{}) error { +func (p *luaPlugin) inject(name string, data interface{}) error { f, ok := p.renderCallbacks[name] if !ok { return nil @@ -86,6 +86,13 @@ func (p *luaPlugin) Inject(name string, data interface{}) error { return nil } +func (p *luaPlugin) Inject(name string, data interface{}) error { + if err := p.inject("*", data); err != nil { + return err + } + return p.inject(name, data) +} + func (p *luaPlugin) LoadTemplate(t *template.Template) error { t.Funcs(p.filters)