Prefix html title tag for posts with their title

main
Brian Picciano 1 year ago
parent 7d48d2abba
commit 861070f74d
  1. 5
      src/http/posts.go
  2. 15
      src/http/tpl.go
  3. 2
      src/http/tpl/base.html

@ -271,7 +271,10 @@ func (a *api) getPostHandler() http.Handler {
return return
} }
executeTemplate(rw, r, tpl, tplPayload) executeTemplate(
rw, r, tpl, tplPayload,
executeTemplateWithTitlePrefix(storedPost.Title),
)
}) })
} }

@ -133,11 +133,21 @@ func (a *api) mustParseBasedTpl(name string) *template.Template {
type tplData struct { type tplData struct {
Payload interface{} Payload interface{}
Title string
} }
func newTPLData(r *http.Request, payload interface{}) tplData { func newTPLData(r *http.Request, payload interface{}) tplData {
return tplData{ return tplData{
Payload: payload, Payload: payload,
Title: "mediocregopher's lil web corner",
}
}
type executeTemplateOpt func(*tplData)
func executeTemplateWithTitlePrefix(prefix string) executeTemplateOpt {
return func(d *tplData) {
d.Title = prefix + " - " + d.Title
} }
} }
@ -145,10 +155,15 @@ func newTPLData(r *http.Request, payload interface{}) tplData {
func executeTemplate( func executeTemplate(
rw http.ResponseWriter, r *http.Request, rw http.ResponseWriter, r *http.Request,
tpl *template.Template, payload interface{}, tpl *template.Template, payload interface{},
opts ...executeTemplateOpt,
) { ) {
tplData := newTPLData(r, payload) tplData := newTPLData(r, payload)
for _, opt := range opts {
opt(&tplData)
}
if err := tpl.Execute(rw, tplData); err != nil { if err := tpl.Execute(rw, tplData); err != nil {
apiutil.InternalServerError( apiutil.InternalServerError(
rw, r, fmt.Errorf("rendering template: %w", err), rw, r, fmt.Errorf("rendering template: %w", err),

@ -2,7 +2,7 @@
<html> <html>
<head> <head>
<title>mediocregopher's lil web corner</title> <title>{{ .Title }}</title>
<style>{{ StaticInlineCSS "new.css" }}</style> <style>{{ StaticInlineCSS "new.css" }}</style>
<style>{{ StaticInlineCSS "mediocre.css" }}</style> <style>{{ StaticInlineCSS "mediocre.css" }}</style>

Loading…
Cancel
Save