From 89c369f21ef7f19219414bfa9f2d84742f808706 Mon Sep 17 00:00:00 2001 From: f0x Date: Thu, 8 Jul 2021 15:14:05 +0200 Subject: [PATCH] provide noescape function for templates --- internal/router/router.go | 8 ++++++++ internal/web/base.go | 13 +++++++++++++ web/template/index.tmpl | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/internal/router/router.go b/internal/router/router.go index a77b7071e..4c2c53ba7 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -21,6 +21,7 @@ package router import ( "context" "fmt" + "html/template" "net/http" "time" @@ -46,6 +47,8 @@ type Router interface { AttachMiddleware(handler gin.HandlerFunc) // Attach 404 NoRoute handler AttachNoRouteHandler(handler gin.HandlerFunc) + // Set Template function map + SetTemplateFuncMap(functions template.FuncMap) // Start the router Start() // Stop the router @@ -98,6 +101,11 @@ func (r *router) Stop(ctx context.Context) error { return r.srv.Shutdown(ctx) } +// Set Template function map +func (r *router) SetTemplateFuncMap(functions template.FuncMap) { + r.engine.SetFuncMap(functions) +} + // New returns a new Router with the specified configuration, using the given logrus logger. // // The given DB is only used in the New function for parsing config values, and is not otherwise diff --git a/internal/web/base.go b/internal/web/base.go index 8b2152767..10c4d0afa 100644 --- a/internal/web/base.go +++ b/internal/web/base.go @@ -20,6 +20,7 @@ package web import ( "fmt" + "html/template" "net/http" "os" "path/filepath" @@ -84,6 +85,10 @@ func (m *Module) NotFoundHandler(c *gin.Context) { }) } +func noescape(str string) template.HTML { + return template.HTML(str) +} + // Route satisfies the RESTAPIModule interface func (m *Module) Route(s router.Router) error { @@ -101,5 +106,13 @@ func (m *Module) Route(s router.Router) error { // 404 handler s.AttachNoRouteHandler(m.NotFoundHandler) + s.SetTemplateFuncMap(template.FuncMap{ + "noescape": noescape, + }) + + if err != nil { + return fmt.Errorf("error setting router FuncMap: %s", err) + } + return nil } diff --git a/web/template/index.tmpl b/web/template/index.tmpl index 4fb9dd146..de84d430f 100644 --- a/web/template/index.tmpl +++ b/web/template/index.tmpl @@ -9,7 +9,7 @@

This is the default landing page, you can edit it from ./web/template/index.tmpl

- {{.instance.ShortDescription | noescape}} + {{.instance.ShortDescription |noescape}}