parsing and templating improvements

pull/20/head
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 := new(page.Page)
page.Title = "Edit" page.Title = "Edit"
page.Body = inf page.Body = inf
return page.Render("edit", w) return page.Render(w, "edit")
} }
return 200, nil return 200, nil

View File

@ -24,35 +24,30 @@ type Page struct {
} }
// Render the page // Render the page
func (p *Page) Render(name string, w http.ResponseWriter) (int, error) { func (p *Page) Render(w http.ResponseWriter, templates ...string) (int, error) {
base, err := assets.Asset("templates/base" + templateExtension) templates = append(templates, "header", "footer")
var tpl *template.Template
if err != nil { for i, t := range templates {
log.Print(err) page, err := assets.Asset("templates/" + t + templateExtension)
return 500, err
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) tpl.Execute(w, p)
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)
return 200, nil return 200, nil
} }

View File

@ -42,7 +42,7 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
page := new(page.Page) page := new(page.Page)
page.Title = "Settings" page.Title = "Settings"
page.Body = cnf page.Body = cnf
return page.Render("settings", w) return page.Render(w, "settings", "frontmatter")
} }
return 200, nil 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> <!DOCTYPE html>
<html class="no-js" lang="en"> <html class="no-js" lang="en">
<head> <head>
@ -20,9 +21,4 @@
<header class="site-header"> <header class="site-header">
Admin Admin
</header> </header>
{{ end }}
{{ template "content" . }}
<footer></footer>
</body>
</html>

View File

@ -1,30 +1,12 @@
{{ define "optsLoop" }} {{ template "header" . }}
{{ range $key, $value := . }}
{{ if $value.SubContent }}
<h2>{{ splitCapitalize $value.Name }}</h2>
{{ template "optsLoop" $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 }}
{{ define "content" }}
{{ with .Body }} {{ with .Body }}
<div class="content"> <div class="content">
<h1>Settings</h1> <h1>Settings</h1>
<form> <form>
{{ template "optsLoop" .Settings }} {{ template "frontmatter" .Settings }}
</form> </form>
</div> </div>
{{ end }}
{{ end }} {{ template "footer" . }}
{{ end }}