From 60b77ae610dd5a6e8cbadfeebfb849e8123387a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Casaj=C3=BAs?= Date: Tue, 17 Feb 2015 16:54:52 +0100 Subject: [PATCH] Allow user-defined compile function Allow users to define a custom template compilation function --- render.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/render.go b/render.go index 807e801..b6993cb 100644 --- a/render.go +++ b/render.go @@ -132,6 +132,8 @@ type Options struct { PrefixXML []byte // Allows changing of output to XHTML instead of HTML. Default is "text/html" HTMLContentType string + // Allows to define a user function to generate templates + CompileHandler func(Options) *template.Template } // HTMLOptions is a struct for overriding some rendering Options for specific HTML call @@ -149,13 +151,22 @@ type HTMLOptions struct { func Renderer(options ...Options) martini.Handler { opt := prepareOptions(options) cs := prepareCharset(opt.Charset) - t := compile(opt) + var t *template.Template + if opt.CompileHandler != nil { + t = opt.CompileHandler(opt) + } else { + t = compile(opt) + } bufpool = bpool.NewBufferPool(64) return func(res http.ResponseWriter, req *http.Request, c martini.Context) { var tc *template.Template if martini.Env == martini.Dev { // recompile for easy development - tc = compile(opt) + if opt.CompileHandler != nil { + tc = opt.CompileHandler(opt) + } else { + tc = compile(opt) + } } else { // use a clone of the initial template tc, _ = t.Clone()