Restructure templates to use includes

This commit is contained in:
Las Zenow 2012-08-15 12:00:44 +02:00
parent 8e4a37b558
commit 18454021c3
8 changed files with 19 additions and 14 deletions

View file

@ -1,3 +1,5 @@
{{template "header.html"}}
<h1>{{.Title}}</h1>
<p>{{.Description}}</p>
<img src="{{.Cover}}" alt="{{.Title}}" />
@ -10,3 +12,5 @@
</ul>
<p><a href="/{{.Path}}">download</a></p>
{{template "footer.html"}}

View file

@ -1,3 +1,5 @@
{{template "header.html"}}
<h1>Imperial Library of Trantor</h1>
<form action="/search/">
<input type="search" name="q" />
@ -5,3 +7,5 @@
</form>
<p>Search on {{.}} books.</p>
<p><img src="/img/library.jpg" alt="library" /></p>
{{template "footer.html"}}

View file

@ -1,3 +1,5 @@
{{template "header.html"}}
<h1>Search</h1>
<form action="/search/">
<input type="search" name="q" value="{{.Search}}"/>
@ -17,3 +19,5 @@
<p>
{{if .Prev}}<a href="{{.Prev}}">&lt;Prev</a> {{end}}
{{if .Next}}<a href="{{.Next}}">Next&gt;</a>{{end}}</p>
{{template "footer.html"}}

View file

@ -7,20 +7,9 @@ import (
func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
// TODO: when finish devel conver to global:
var templates = template.Must(template.ParseFiles("head.html", "foot.html", "front.html", "book.html", "search.html", "upload.html"))
var templates = template.Must(template.ParseFiles("header.html", "footer.html", "index.html", "book.html", "search.html", "upload.html"))
// TODO: use includes
err := templates.ExecuteTemplate(w, "head.html", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = templates.ExecuteTemplate(w, tmpl+".html", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = templates.ExecuteTemplate(w, "foot.html", nil)
err := templates.ExecuteTemplate(w, tmpl+".html", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return

View file

@ -39,6 +39,6 @@ func main() {
fileHandler("/img/")
fileHandler("/cover/")
fileHandler("/books/")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { loadTemplate(w, "front", num) })
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { loadTemplate(w, "index", num) })
http.ListenAndServe(":8080", nil)
}

View file

@ -1,4 +1,8 @@
{{template "header.html"}}
<form method="POST" enctype="multipart/form-data">
<input accept="application/epub+zip" type="file" name="epub" />
<input type="submit" />
</form>
{{template "footer.html"}}