From 180575fe4ace9c491940069dc911681b8e4940f3 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 21 May 2022 12:10:38 -0600 Subject: Fix post body templates not being parsed correctly --- srv/src/http/tpl.go | 36 ++++++++---------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) (limited to 'srv/src/http/tpl.go') diff --git a/srv/src/http/tpl.go b/srv/src/http/tpl.go index 2e22370..5c235a1 100644 --- a/srv/src/http/tpl.go +++ b/srv/src/http/tpl.go @@ -1,7 +1,6 @@ package http import ( - "bytes" "embed" "fmt" "html/template" @@ -59,11 +58,8 @@ func (a *api) assetsURL(abs bool) string { return a.blogURL("assets", abs) } -func (a *api) parseTpl(name, tplBody string) (*template.Template, error) { - - tpl := template.New("root") - - tpl = tpl.Funcs(template.FuncMap{ +func (a *api) tplFuncs() template.FuncMap { + return template.FuncMap{ "BlogURL": func(path string) string { return a.blogURL(path, false) }, @@ -81,33 +77,17 @@ func (a *api) parseTpl(name, tplBody string) (*template.Template, error) { "DateTimeFormat": func(t time.Time) string { return t.Format("2006-01-02") }, - }) - - tpl = template.Must(tpl.New("image.html").Parse(mustReadTplFile("image.html"))) - - tpl = tpl.Funcs(template.FuncMap{ - "Image": func(id string) (template.HTML, error) { - - tplPayload := struct { - ID string - Resizable bool - }{ - ID: id, - Resizable: isImgResizable(id), - } + } +} - buf := new(bytes.Buffer) - if err := tpl.ExecuteTemplate(buf, "image.html", tplPayload); err != nil { - return "", err - } +func (a *api) parseTpl(name, tplBody string) (*template.Template, error) { - return template.HTML(buf.Bytes()), nil - }, - }) + tpl := template.New(name) + tpl = tpl.Funcs(a.tplFuncs()) var err error - if tpl, err = tpl.New(name).Parse(tplBody); err != nil { + if tpl, err = tpl.Parse(tplBody); err != nil { return nil, err } -- cgit v1.2.3