From 4ba637b3c3596d6fb7d073d70f23e4f421949bdd Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Wed, 18 Oct 2023 12:44:41 -0700 Subject: echo: allow plugins to hook templates --- echo/echo_default.go | 6 ++++++ echo/interfaces.go | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/echo/echo_default.go b/echo/echo_default.go index cbe43cb..8440fe2 100644 --- a/echo/echo_default.go +++ b/echo/echo_default.go @@ -61,6 +61,7 @@ type EchoConfig struct { TrustedProxyIPRanges []string EmbeddedTemplates fs.FS DiskTemplates fs.FS + ProvideTemplateStore []WantsTemplateStore TemplateFunctions template.FuncMap CombinedHostLogFile string RedirectToWWW bool @@ -227,6 +228,11 @@ func (w *EchoWrapper) configureTemplates(c *EchoConfig) error { templates = c.EmbeddedTemplates } + // Configure plugins that want access to the template store + for _, w := range c.ProvideTemplateStore { + w.ConfigureTemplateStore(templates) + } + // Only install template handlers if the path is set if templates != nil { tr, err := NewTemplateRenderer(templates, "*.tpl", c.TemplateFunctions, w.Debug) diff --git a/echo/interfaces.go b/echo/interfaces.go index f3380c6..1f3957a 100644 --- a/echo/interfaces.go +++ b/echo/interfaces.go @@ -1,6 +1,8 @@ package echo import ( + "io/fs" + "github.com/labstack/echo/v4" ) @@ -18,3 +20,9 @@ type URLRouter interface { Group(string, ...echo.MiddlewareFunc) *echo.Group Add(string, string, echo.HandlerFunc, ...echo.MiddlewareFunc) *echo.Route } + +// WantsTemplateStore is an interface for template helper functions that +// need access to the template store for the running echo instance. +type WantsTemplateStore interface { + ConfigureTemplateStore(fs.FS) +} -- cgit v1.2.3