Updates on Templates assets

Former-commit-id: 58946e691e
This commit is contained in:
Henrique Dias 2017-06-25 21:54:01 +01:00
parent d91464c77a
commit b02ac893ec
4 changed files with 23 additions and 13 deletions

View File

@ -80,6 +80,7 @@ type User struct {
// assets are the static and front-end assets, such as JS, CSS and HTML templates. // assets are the static and front-end assets, such as JS, CSS and HTML templates.
type assets struct { type assets struct {
requiredJS *rice.Box // JS that is always required to have in order to be usable. requiredJS *rice.Box // JS that is always required to have in order to be usable.
baseTemplates *rice.Box
Templates *rice.Box Templates *rice.Box
CSS *rice.Box CSS *rice.Box
JS *rice.Box JS *rice.Box
@ -115,7 +116,7 @@ func New(scope string) *FileManager {
BeforeSave: func(r *http.Request, m *FileManager, u *User) error { return nil }, BeforeSave: func(r *http.Request, m *FileManager, u *User) error { return nil },
AfterSave: 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{ Assets: &assets{
Templates: rice.MustFindBox("./_assets/templates"), baseTemplates: rice.MustFindBox("./_assets/templates"),
CSS: rice.MustFindBox("./_assets/css"), CSS: rice.MustFindBox("./_assets/css"),
requiredJS: rice.MustFindBox("./_assets/js"), requiredJS: rice.MustFindBox("./_assets/js"),
}, },

View File

@ -90,7 +90,7 @@ func serveListing(w http.ResponseWriter, r *http.Request, c *FileManager, u *Use
Data: listing, 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', // handleSortOrder gets and stores for a Listing the 'sort' and 'order',

View File

@ -43,8 +43,8 @@ func serveSingle(w http.ResponseWriter, r *http.Request, c *FileManager, u *User
return http.StatusInternalServerError, err 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")
} }

15
page.go
View File

@ -9,7 +9,6 @@ import (
"net/http" "net/http"
"strings" "strings"
rice "github.com/GeertJohan/go.rice"
"github.com/hacdias/filemanager/variables" "github.com/hacdias/filemanager/variables"
) )
@ -103,7 +102,7 @@ func (p page) PreviousLink() string {
} }
// PrintAsHTML formats the page in HTML and executes the template // 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 { if p.minimal {
templates = append(templates, "minimal") templates = append(templates, "minimal")
} else { } 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 each template, add it to the the tpl variable
for i, t := range templates { for i, t := range templates {
// Get the template from the assets // 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 // Check if there is some error. If so, the template doesn't exist
if err != nil { if err != nil {
@ -163,3 +162,13 @@ func (p page) PrintAsJSON(w http.ResponseWriter) (int, error) {
return http.StatusOK, nil 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)
}