filebrowser/page/page.go

54 lines
893 B
Go
Raw Normal View History

2015-09-13 11:14:18 +00:00
package page
import (
"html/template"
2015-09-14 09:46:31 +00:00
"log"
2015-09-13 11:14:18 +00:00
"net/http"
"github.com/hacdias/caddy-hugo/assets"
2015-09-14 09:46:31 +00:00
"github.com/hacdias/caddy-hugo/utils"
2015-09-13 11:14:18 +00:00
)
const (
templateExtension = ".tmpl"
)
2015-09-13 21:48:52 +00:00
var funcMap = template.FuncMap{
2015-09-14 09:46:31 +00:00
"splitCapitalize": utils.SplitCapitalize,
2015-09-13 21:48:52 +00:00
}
// Page type
type Page struct {
2015-09-13 11:14:18 +00:00
Title string
Body interface{}
}
// Render the page
2015-09-14 13:00:12 +00:00
func (p *Page) Render(w http.ResponseWriter, templates ...string) (int, error) {
2015-09-14 13:54:58 +00:00
templates = append(templates, "base_full")
2015-09-14 13:00:12 +00:00
var tpl *template.Template
for i, t := range templates {
page, err := assets.Asset("templates/" + t + templateExtension)
if err != nil {
log.Print(err)
return 500, err
}
if i == 0 {
tpl, err = template.New(t).Funcs(funcMap).Parse(string(page))
} else {
tpl, err = tpl.Parse(string(page))
}
if err != nil {
log.Print(err)
return 500, err
}
2015-09-13 11:14:18 +00:00
}
2015-09-14 13:00:12 +00:00
tpl.Execute(w, p)
2015-09-13 11:14:18 +00:00
return 200, nil
}