2015-09-13 19:14:18 +08:00
|
|
|
package page
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
2015-09-14 17:46:31 +08:00
|
|
|
"log"
|
2015-09-13 19:14:18 +08:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/hacdias/caddy-hugo/assets"
|
2015-09-14 17:46:31 +08:00
|
|
|
"github.com/hacdias/caddy-hugo/utils"
|
2015-09-13 19:14:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
templateExtension = ".tmpl"
|
|
|
|
)
|
|
|
|
|
2015-09-14 05:48:52 +08:00
|
|
|
var funcMap = template.FuncMap{
|
2015-09-14 17:46:31 +08:00
|
|
|
"splitCapitalize": utils.SplitCapitalize,
|
2015-09-14 05:48:52 +08:00
|
|
|
}
|
|
|
|
|
2015-09-13 19:28:26 +08:00
|
|
|
// Page type
|
|
|
|
type Page struct {
|
2015-09-13 19:14:18 +08:00
|
|
|
Title string
|
|
|
|
Body interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render the page
|
2015-09-15 01:18:26 +08:00
|
|
|
func (p *Page) Render(w http.ResponseWriter, r *http.Request, templates ...string) (int, error) {
|
|
|
|
if r.URL.Query().Get("minimal") == "true" {
|
|
|
|
templates = append(templates, "base_minimal")
|
|
|
|
} else {
|
|
|
|
templates = append(templates, "base_full")
|
|
|
|
}
|
|
|
|
|
2015-09-14 21:00:12 +08: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 19:14:18 +08:00
|
|
|
}
|
|
|
|
|
2015-09-14 21:00:12 +08:00
|
|
|
tpl.Execute(w, p)
|
2015-09-13 19:14:18 +08:00
|
|
|
return 200, nil
|
|
|
|
}
|