filebrowser/page/page.go

59 lines
940 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
func (p *Page) Render(name string, w http.ResponseWriter) (int, error) {
2015-09-14 09:46:31 +00:00
base, err := assets.Asset("templates/base" + templateExtension)
2015-09-13 11:14:18 +00:00
if err != nil {
2015-09-14 09:46:31 +00:00
log.Print(err)
2015-09-13 11:14:18 +00:00
return 500, err
}
2015-09-14 09:46:31 +00:00
page, err := assets.Asset("templates/" + name + templateExtension)
2015-09-13 11:14:18 +00:00
if err != nil {
2015-09-14 09:46:31 +00:00
log.Print(err)
2015-09-13 11:14:18 +00:00
return 500, err
}
2015-09-14 09:46:31 +00:00
tpl, err := template.New("base").Funcs(funcMap).Parse(string(base))
2015-09-13 11:14:18 +00:00
if err != nil {
2015-09-14 09:46:31 +00:00
log.Print(err)
2015-09-13 11:14:18 +00:00
return 500, err
}
2015-09-14 09:46:31 +00:00
tpl, err = tpl.Parse(string(page))
2015-09-13 11:14:18 +00:00
if err != nil {
2015-09-14 09:46:31 +00:00
log.Print(err)
2015-09-13 11:14:18 +00:00
return 500, err
}
2015-09-14 09:46:31 +00:00
tpl.ExecuteTemplate(w, "base", p)
2015-09-13 11:14:18 +00:00
return 200, nil
}