diff --git a/filemanager.go b/filemanager.go index 421621e9..02ddfed4 100644 --- a/filemanager.go +++ b/filemanager.go @@ -79,10 +79,11 @@ type User struct { // assets are the static and front-end assets, such as JS, CSS and HTML templates. type assets struct { - requiredJS *rice.Box // JS that is always required to have in order to be usable. - Templates *rice.Box - CSS *rice.Box - JS *rice.Box + requiredJS *rice.Box // JS that is always required to have in order to be usable. + baseTemplates *rice.Box + Templates *rice.Box + CSS *rice.Box + JS *rice.Box } // Rule is a dissalow/allow rule. @@ -115,9 +116,9 @@ func New(scope string) *FileManager { BeforeSave: func(r *http.Request, m *FileManager, u *User) error { return nil }, AfterSave: func(r *http.Request, m *FileManager, u *User) error { return nil }, Assets: &assets{ - Templates: rice.MustFindBox("./_assets/templates"), - CSS: rice.MustFindBox("./_assets/css"), - requiredJS: rice.MustFindBox("./_assets/js"), + baseTemplates: rice.MustFindBox("./_assets/templates"), + CSS: rice.MustFindBox("./_assets/css"), + requiredJS: rice.MustFindBox("./_assets/js"), }, } diff --git a/http_listing.go b/http_listing.go index 83aceadc..4e79eabd 100644 --- a/http_listing.go +++ b/http_listing.go @@ -90,7 +90,7 @@ func serveListing(w http.ResponseWriter, r *http.Request, c *FileManager, u *Use Data: listing, } - return p.PrintAsHTML(w, c.Assets.Templates, "listing") + return p.PrintAsHTML(w, c, "listing") } // handleSortOrder gets and stores for a Listing the 'sort' and 'order', diff --git a/http_single.go b/http_single.go index 5aa356b1..7d17f7d6 100644 --- a/http_single.go +++ b/http_single.go @@ -43,8 +43,8 @@ func serveSingle(w http.ResponseWriter, r *http.Request, c *FileManager, u *User return http.StatusInternalServerError, err } - return p.PrintAsHTML(w, c.Assets.Templates, "frontmatter", "editor") + return p.PrintAsHTML(w, c, "frontmatter", "editor") } - return p.PrintAsHTML(w, c.Assets.Templates, "single") + return p.PrintAsHTML(w, c, "single") } diff --git a/page.go b/page.go index 74c58011..6a2f0fcc 100644 --- a/page.go +++ b/page.go @@ -9,7 +9,6 @@ import ( "net/http" "strings" - rice "github.com/GeertJohan/go.rice" "github.com/hacdias/filemanager/variables" ) @@ -103,7 +102,7 @@ func (p page) PreviousLink() string { } // PrintAsHTML formats the page in HTML and executes the template -func (p page) PrintAsHTML(w http.ResponseWriter, box *rice.Box, templates ...string) (int, error) { +func (p page) PrintAsHTML(w http.ResponseWriter, m *FileManager, templates ...string) (int, error) { if p.minimal { templates = append(templates, "minimal") } else { @@ -115,7 +114,7 @@ func (p page) PrintAsHTML(w http.ResponseWriter, box *rice.Box, templates ...str // For each template, add it to the the tpl variable for i, t := range templates { // Get the template from the assets - Page, err := box.String(t + ".tmpl") + Page, err := getTemplate(m, t+".tmpl") // Check if there is some error. If so, the template doesn't exist if err != nil { @@ -163,3 +162,13 @@ func (p page) PrintAsJSON(w http.ResponseWriter) (int, error) { return http.StatusOK, nil } + +func getTemplate(m *FileManager, template string) (string, error) { + if m.Assets.Templates != nil { + if tpl, err := m.Assets.Templates.String(template); err == nil { + return tpl, err + } + } + + return m.Assets.baseTemplates.String(template) +}