provide noescape function for templates

This commit is contained in:
f0x 2021-07-08 15:14:05 +02:00
commit 89c369f21e
3 changed files with 22 additions and 1 deletions

View file

@ -21,6 +21,7 @@ package router
import ( import (
"context" "context"
"fmt" "fmt"
"html/template"
"net/http" "net/http"
"time" "time"
@ -46,6 +47,8 @@ type Router interface {
AttachMiddleware(handler gin.HandlerFunc) AttachMiddleware(handler gin.HandlerFunc)
// Attach 404 NoRoute handler // Attach 404 NoRoute handler
AttachNoRouteHandler(handler gin.HandlerFunc) AttachNoRouteHandler(handler gin.HandlerFunc)
// Set Template function map
SetTemplateFuncMap(functions template.FuncMap)
// Start the router // Start the router
Start() Start()
// Stop the router // Stop the router
@ -98,6 +101,11 @@ func (r *router) Stop(ctx context.Context) error {
return r.srv.Shutdown(ctx) 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. // 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 // The given DB is only used in the New function for parsing config values, and is not otherwise

View file

@ -20,6 +20,7 @@ package web
import ( import (
"fmt" "fmt"
"html/template"
"net/http" "net/http"
"os" "os"
"path/filepath" "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 // Route satisfies the RESTAPIModule interface
func (m *Module) Route(s router.Router) error { func (m *Module) Route(s router.Router) error {
@ -101,5 +106,13 @@ func (m *Module) Route(s router.Router) error {
// 404 handler // 404 handler
s.AttachNoRouteHandler(m.NotFoundHandler) s.AttachNoRouteHandler(m.NotFoundHandler)
s.SetTemplateFuncMap(template.FuncMap{
"noescape": noescape,
})
if err != nil {
return fmt.Errorf("error setting router FuncMap: %s", err)
}
return nil return nil
} }

View file

@ -9,7 +9,7 @@
<h3>This is the default landing page, you can edit it from <span class="accent">./web/template/index.tmpl</span></h1> <h3>This is the default landing page, you can edit it from <span class="accent">./web/template/index.tmpl</span></h1>
<p> <p>
{{.instance.ShortDescription | noescape}} {{.instance.ShortDescription |noescape}}
</p> </p>
</section> </section>