diff --git a/edit/edit.go b/edit/edit.go index 9865d23b..0b3d0d7f 100644 --- a/edit/edit.go +++ b/edit/edit.go @@ -50,7 +50,7 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) { page := new(page.Page) page.Title = "Edit" page.Body = inf - return page.Render("edit", w) + return page.Render(w, "edit") } return 200, nil diff --git a/page/page.go b/page/page.go index f83063b9..585b7c61 100644 --- a/page/page.go +++ b/page/page.go @@ -24,35 +24,30 @@ type Page struct { } // Render the page -func (p *Page) Render(name string, w http.ResponseWriter) (int, error) { - base, err := assets.Asset("templates/base" + templateExtension) +func (p *Page) Render(w http.ResponseWriter, templates ...string) (int, error) { + templates = append(templates, "header", "footer") + var tpl *template.Template - if err != nil { - log.Print(err) - return 500, err + for i, t := range templates { + page, err := assets.Asset("templates/" + t + templateExtension) + + if err != nil { + log.Print(err) + return 500, err + } + + if i == 0 { + tpl, err = template.New(t).Funcs(funcMap).Parse(string(page)) + } else { + tpl, err = tpl.Parse(string(page)) + } + + if err != nil { + log.Print(err) + return 500, err + } } - page, err := assets.Asset("templates/" + name + templateExtension) - - if err != nil { - log.Print(err) - return 500, err - } - - tpl, err := template.New("base").Funcs(funcMap).Parse(string(base)) - - if err != nil { - log.Print(err) - return 500, err - } - - tpl, err = tpl.Parse(string(page)) - - if err != nil { - log.Print(err) - return 500, err - } - - tpl.ExecuteTemplate(w, "base", p) + tpl.Execute(w, p) return 200, nil } diff --git a/settings/settings.go b/settings/settings.go index 82952002..67be6471 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -42,7 +42,7 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) { page := new(page.Page) page.Title = "Settings" page.Body = cnf - return page.Render("settings", w) + return page.Render(w, "settings", "frontmatter") } return 200, nil diff --git a/templates/footer.tmpl b/templates/footer.tmpl new file mode 100644 index 00000000..e2692fc3 --- /dev/null +++ b/templates/footer.tmpl @@ -0,0 +1,5 @@ +{{ define "footer" }} + + + +{{ end }} diff --git a/templates/frontmatter.tmpl b/templates/frontmatter.tmpl new file mode 100644 index 00000000..b7062fcd --- /dev/null +++ b/templates/frontmatter.tmpl @@ -0,0 +1,11 @@ +{{ define "frontmatter" }} + {{ range $key, $value := . }} + {{ if $value.SubContent }} +

{{ splitCapitalize $value.Name }}

+ {{ template "frontmatter" $value.Content }} + {{ else}} + +
+ {{ end }} + {{ end }} +{{ end }} diff --git a/templates/base.tmpl b/templates/header.tmpl similarity index 91% rename from templates/base.tmpl rename to templates/header.tmpl index 7309b252..e94b6ea1 100644 --- a/templates/base.tmpl +++ b/templates/header.tmpl @@ -1,3 +1,4 @@ +{{ define "header" }} @@ -20,9 +21,4 @@ - - {{ template "content" . }} - - - - +{{ end }} diff --git a/templates/settings.tmpl b/templates/settings.tmpl index 1dd9c13e..257931d5 100644 --- a/templates/settings.tmpl +++ b/templates/settings.tmpl @@ -1,30 +1,12 @@ -{{ define "optsLoop" }} -{{ range $key, $value := . }} -{{ if $value.SubContent }} -

{{ splitCapitalize $value.Name }}

- {{ template "optsLoop" $value.Content }} -{{ else}} +{{ template "header" . }} - -
-{{ end }} -{{ end }} -{{ end }} - - -{{ define "content" }} {{ with .Body }} -
-

Settings

-
- {{ template "optsLoop" .Settings }} + {{ template "frontmatter" .Settings }}
- -
+{{ end }} -{{ end }} -{{ end }} +{{ template "footer" . }}