From d26baaf5e740200ba38d63e7c87f6622dacc32dd Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 24 Apr 2014 22:01:27 +0800 Subject: [PATCH 1/2] add predefine template --- render.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/render.go b/render.go index 012c5cb..d6b6b32 100644 --- a/render.go +++ b/render.go @@ -101,6 +101,8 @@ type Options struct { IndentJSON bool // Allows changing of output to XHTML instead of HTML. Default is "text/html" HTMLContentType string + // Predefine templates + Template *template.Template } // HTMLOptions is a struct for overriding some rendering Options for specific HTML call @@ -118,16 +120,24 @@ 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.Template == nil { + t = compile(opt) + } 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.Template == nil { + if martini.Env == martini.Dev { + // recompile for easy development + tc = compile(opt) + } else { + // use a clone of the initial template + tc, _ = t.Clone() + } } else { - // use a clone of the initial template - tc, _ = t.Clone() + tc = opt.Template } + c.MapTo(&renderer{res, req, tc, opt, cs}, (*Render)(nil)) } } From 8972799a0a4a962a3712642e4cc01b6e395a03c0 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 24 Apr 2014 22:11:18 +0800 Subject: [PATCH 2/2] update comment --- render.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/render.go b/render.go index d6b6b32..d05a5dd 100644 --- a/render.go +++ b/render.go @@ -101,7 +101,7 @@ type Options struct { IndentJSON bool // Allows changing of output to XHTML instead of HTML. Default is "text/html" HTMLContentType string - // Predefine templates + // Allow to use predefine compiled template from external Template *template.Template }