From c54dccec17976a1db12da82f520e8295dc36ea50 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 21 Jun 2016 15:02:30 +0100 Subject: [PATCH] Make slight updates --- fileinfo.go | 20 +++++++++++++++++-- page.go | 57 ++++++++++++++++++++++++++++------------------------- 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/fileinfo.go b/fileinfo.go index 220331eb..ee558231 100644 --- a/fileinfo.go +++ b/fileinfo.go @@ -163,7 +163,15 @@ func (fi FileInfo) serveSingleFile(w http.ResponseWriter, r *http.Request, c *Co }, } - return page.PrintAsHTML(w, "single") + templates := []string{"single", "actions", "base"} + for _, t := range templates { + code, err := page.AddTemplate(t, Asset) + if err != nil { + return code, err + } + } + + return page.PrintAsHTML(w) } func (fi FileInfo) serveListing(w http.ResponseWriter, r *http.Request, c *Config) (int, error) { @@ -218,7 +226,15 @@ func (fi FileInfo) serveListing(w http.ResponseWriter, r *http.Request, c *Confi }, } - return page.PrintAsHTML(w, "listing") + templates := []string{"listing", "actions", "base"} + for _, t := range templates { + code, err := page.AddTemplate(t, Asset) + if err != nil { + return code, err + } + } + + return page.PrintAsHTML(w) } func (fi FileInfo) loadDirectoryContents(file http.File, c *Config) (*Listing, error) { diff --git a/page.go b/page.go index 1b7507b9..c268c45c 100644 --- a/page.go +++ b/page.go @@ -11,8 +11,12 @@ import ( // Page contains the informations and functions needed to show the page type Page struct { Info *PageInfo + Tpl *template.Template } +// AssetFunc is an Assets function +type AssetFunc func(name string) ([]byte, error) + // PageInfo contains the information of a page type PageInfo struct { Name string @@ -67,38 +71,37 @@ func (p PageInfo) PreviousLink() string { return parts[len(parts)-2] } -// PrintAsHTML formats the page in HTML and executes the template -func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, error) { - templates = append(templates, "actions", "base") - var tpl *template.Template +// AddTemplate adds a template file to the page template +func (p *Page) AddTemplate(name string, assets AssetFunc) (int, error) { - // For each template, add it to the the tpl variable - for i, t := range templates { - // Get the template from the assets - page, err := Asset("templates/" + t + ".tmpl") + // Get the template from the assets + page, err := assets("templates/" + name + ".tmpl") - // Check if there is some error. If so, the template doesn't exist - if err != nil { - log.Print(err) - return http.StatusInternalServerError, err - } - - // If it's the first iteration, creates a new template and add the - // functions map - if i == 0 { - tpl, err = template.New(t).Parse(string(page)) - } else { - tpl, err = tpl.Parse(string(page)) - } - - if err != nil { - log.Print(err) - return http.StatusInternalServerError, err - } + // Check if there is some error. If so, the template doesn't exist + if err != nil { + log.Print(err) + return http.StatusInternalServerError, err } + // If it's the first iteration, creates a new template and add the + // functions map + if p.Tpl == nil { + p.Tpl, err = template.New(name).Parse(string(page)) + } else { + p.Tpl, err = p.Tpl.Parse(string(page)) + } + + if err != nil { + log.Print(err) + return http.StatusInternalServerError, err + } + return 0, nil +} + +// PrintAsHTML formats the page in HTML and executes the template +func (p Page) PrintAsHTML(w http.ResponseWriter) (int, error) { w.Header().Set("Content-Type", "text/html; charset=utf-8") - err := tpl.Execute(w, p.Info) + err := p.Tpl.Execute(w, p.Info) if err != nil { return http.StatusInternalServerError, err