From 45c9fdd5279e926c629c4e4fe3fd81b61c9c40dd Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 17 Feb 2016 07:54:30 +0100 Subject: Parse template once --- edit.go | 4 +++- index.go | 4 +++- view.go | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/edit.go b/edit.go index a425a1a..3a4ca37 100644 --- a/edit.go +++ b/edit.go @@ -9,8 +9,10 @@ func init() { http.HandleFunc("/edit/", editHandler) } +var edittmpl = template.Must(template.ParseFiles("tmpl/root", "tmpl/edit")) + func editHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[len("/edit/"):] p, _ := loadPage(title) - p.render(w, template.Must(template.ParseFiles("tmpl/root", "tmpl/edit"))) + p.render(w, edittmpl) } diff --git a/index.go b/index.go index 14f0ca9..10160ab 100644 --- a/index.go +++ b/index.go @@ -10,6 +10,8 @@ func init() { http.HandleFunc("/index", indexHandler) } +var indextmpl = template.Must(template.ParseFiles("tmpl/root", "tmpl/index")) + func indexHandler(w http.ResponseWriter, r *http.Request) { files, err := ioutil.ReadDir("data") if err != nil { @@ -23,5 +25,5 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { p.Pages = append(p.Pages, entry) } } - p.render(w, template.Must(template.ParseFiles("tmpl/root", "tmpl/index"))) + p.render(w, indextmpl) } diff --git a/view.go b/view.go index aae362e..3a1c47a 100644 --- a/view.go +++ b/view.go @@ -9,6 +9,8 @@ func init() { http.HandleFunc("/view/", viewHandler) } +var viewtmpl = template.Must(template.ParseFiles("tmpl/root", "tmpl/view")) + func viewHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[len("/view/"):] p, err := loadPage(title) @@ -16,5 +18,5 @@ func viewHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/edit/"+title, http.StatusFound) return } - p.render(w, template.Must(template.ParseFiles("tmpl/root", "tmpl/view"))) + p.render(w, viewtmpl) } -- cgit v1.2.3