|
|
|
@ -99,6 +99,9 @@ func (a *api) tplFuncs() template.FuncMap { |
|
|
|
|
"DateTimeFormat": func(t time.Time) string { |
|
|
|
|
return t.Format("2006-01-02") |
|
|
|
|
}, |
|
|
|
|
"SafeURL": func(u string) template.URL { |
|
|
|
|
return template.URL(u) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -123,6 +126,7 @@ func (a *api) mustParseTpl(name string) *template.Template { |
|
|
|
|
|
|
|
|
|
func (a *api) mustParseBasedTpl(name string) *template.Template { |
|
|
|
|
tpl := a.mustParseTpl(name) |
|
|
|
|
tpl = template.Must(tpl.New("gemini-cta.html").Parse(mustReadTplFile("gemini-cta.html"))) |
|
|
|
|
tpl = template.Must(tpl.New("base.html").Parse(mustReadTplFile("base.html"))) |
|
|
|
|
return tpl |
|
|
|
|
} |
|
|
|
@ -131,15 +135,19 @@ type tplData struct { |
|
|
|
|
Payload interface{} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func newTPLData(r *http.Request, payload interface{}) tplData { |
|
|
|
|
return tplData{ |
|
|
|
|
Payload: payload, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// executeTemplate expects to be the final action in an http.Handler
|
|
|
|
|
func executeTemplate( |
|
|
|
|
rw http.ResponseWriter, r *http.Request, |
|
|
|
|
tpl *template.Template, payload interface{}, |
|
|
|
|
) { |
|
|
|
|
|
|
|
|
|
tplData := tplData{ |
|
|
|
|
Payload: payload, |
|
|
|
|
} |
|
|
|
|
tplData := newTPLData(r, payload) |
|
|
|
|
|
|
|
|
|
if err := tpl.Execute(rw, tplData); err != nil { |
|
|
|
|
apiutil.InternalServerError( |
|
|
|
@ -164,7 +172,10 @@ func (a *api) renderDumbTplHandler(tplName string) http.Handler { |
|
|
|
|
tpl := a.mustParseBasedTpl(tplName) |
|
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
|
|
|
|
if err := tpl.Execute(rw, nil); err != nil { |
|
|
|
|
|
|
|
|
|
tplData := newTPLData(r, nil) |
|
|
|
|
|
|
|
|
|
if err := tpl.Execute(rw, tplData); err != nil { |
|
|
|
|
apiutil.InternalServerError( |
|
|
|
|
rw, r, fmt.Errorf("rendering %q: %w", tplName, err), |
|
|
|
|
) |
|
|
|
|