2015-09-13 11:14:18 +00:00
|
|
|
package page
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hacdias/caddy-hugo/assets"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
templateExtension = ".tmpl"
|
|
|
|
headerMark = "{{#HEADER#}}"
|
|
|
|
footerMark = "{{#FOOTER#}}"
|
|
|
|
)
|
|
|
|
|
2015-09-13 11:28:26 +00:00
|
|
|
// Page type
|
|
|
|
type Page struct {
|
2015-09-13 11:14:18 +00:00
|
|
|
Title string
|
|
|
|
Body interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render the page
|
2015-09-13 11:28:26 +00:00
|
|
|
func (p *Page) Render(name string, w http.ResponseWriter) (int, error) {
|
2015-09-13 11:14:18 +00:00
|
|
|
rawHeader, err := assets.Asset("templates/header" + templateExtension)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 500, err
|
|
|
|
}
|
|
|
|
|
|
|
|
header := string(rawHeader)
|
|
|
|
|
|
|
|
rawFooter, err := assets.Asset("templates/footer" + templateExtension)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 500, err
|
|
|
|
}
|
|
|
|
|
|
|
|
footer := string(rawFooter)
|
|
|
|
|
|
|
|
rawPage, err := assets.Asset("templates/" + name + templateExtension)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 500, err
|
|
|
|
}
|
|
|
|
|
|
|
|
page := string(rawPage)
|
|
|
|
page = strings.Replace(page, headerMark, header, -1)
|
|
|
|
page = strings.Replace(page, footerMark, footer, -1)
|
|
|
|
|
|
|
|
tpl, err := template.New("page").Parse(page)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 500, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl.Execute(w, p)
|
|
|
|
return 200, nil
|
|
|
|
}
|