From 2663dd176d38dfb929bcc478cac3913827f1246e Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 18 Jun 2019 15:17:48 -0600 Subject: Refactor templates execution; add sprig functions --- modules/caddyhttp/templates/templates.go | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) (limited to 'modules/caddyhttp/templates/templates.go') diff --git a/modules/caddyhttp/templates/templates.go b/modules/caddyhttp/templates/templates.go index 56c3b66..a0fb87f 100644 --- a/modules/caddyhttp/templates/templates.go +++ b/modules/caddyhttp/templates/templates.go @@ -6,7 +6,6 @@ import ( "io" "net/http" "strconv" - "text/template" "github.com/caddyserver/caddy" "github.com/caddyserver/caddy/modules/caddyhttp" @@ -67,29 +66,19 @@ func (t *Templates) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddy // executeTemplate executes the template contianed // in wb.buf and replaces it with the results. func (t *Templates) executeTemplate(wb *responseBuffer, r *http.Request) error { - tpl := template.New(r.URL.Path) - - if len(t.Delimiters) == 2 { - tpl.Delims(t.Delimiters[0], t.Delimiters[1]) - } - - parsedTpl, err := tpl.Parse(wb.buf.String()) - if err != nil { - return caddyhttp.Error(http.StatusInternalServerError, err) - } - var fs http.FileSystem if t.FileRoot != "" { fs = http.Dir(t.FileRoot) } + ctx := &templateContext{ Root: fs, Req: r, RespHeader: tplWrappedHeader{wb.Header()}, + config: t, } - wb.buf.Reset() // reuse buffer for output - err = parsedTpl.Execute(wb.buf, ctx) + err := ctx.executeTemplateInBuffer(r.URL.Path, wb.buf) if err != nil { return caddyhttp.Error(http.StatusInternalServerError, err) } -- cgit v1.2.3