parsing and templating improvements

This commit is contained in:
Henrique Dias 2015-09-14 14:00:12 +01:00
parent e9d507fec3
commit 0d9c1fa6ef
7 changed files with 46 additions and 57 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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

5
templates/footer.tmpl Normal file
View File

@ -0,0 +1,5 @@
{{ define "footer" }}
<footer></footer>
</body>
</html>
{{ end }}

View File

@ -0,0 +1,11 @@
{{ define "frontmatter" }}
{{ range $key, $value := . }}
{{ if $value.SubContent }}
<h2>{{ splitCapitalize $value.Name }}</h2>
{{ template "frontmatter" $value.Content }}
{{ else}}
<label for="{{ $value.Master }}_{{ $value.Name }}">{{ splitCapitalize $value.Name }}</label>
<input name="{{ $value.Master }}_{{ $value.Name }}" id="{{ $value.Master }}_{{ $value.Name }}" value="{{ $value.Content }}"></input><br>
{{ end }}
{{ end }}
{{ end }}

View File

@ -1,3 +1,4 @@
{{ define "header" }}
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
@ -20,9 +21,4 @@
<header class="site-header">
Admin
</header>
{{ template "content" . }}
<footer></footer>
</body>
</html>
{{ end }}

View File

@ -1,30 +1,12 @@
{{ define "optsLoop" }}
{{ range $key, $value := . }}
{{ if $value.SubContent }}
<h2>{{ splitCapitalize $value.Name }}</h2>
{{ template "optsLoop" $value.Content }}
{{ else}}
{{ template "header" . }}
<label for="{{ $value.Master }}_{{ $value.Name }}">{{ splitCapitalize $value.Name }}</label>
<input name="{{ $value.Master }}_{{ $value.Name }}" id="{{ $value.Master }}_{{ $value.Name }}" value="{{ $value.Content }}"></input><br>
{{ end }}
{{ end }}
{{ end }}
{{ define "content" }}
{{ with .Body }}
<div class="content">
<h1>Settings</h1>
<form>
{{ template "optsLoop" .Settings }}
{{ template "frontmatter" .Settings }}
</form>
</div>
{{ end }}
{{ end }}
{{ end }}
{{ template "footer" . }}