wiki/template/renderer.go

55 lines
949 B
Go

package template
import (
"errors"
"io"
"github.com/GeertJohan/go.rice"
"github.com/flosch/pongo2"
_ "github.com/flosch/pongo2-addons"
)
const DefaultExtension = ".html.twig"
type loader struct {
ricebox *rice.Box
}
func (loader) Abs(base, name string) string {
return name
}
func (l loader) Get(path string) (io.Reader, error) {
return l.ricebox.Open(path)
}
func New(debug bool) *Renderer {
l := loader{
ricebox: rice.MustFindBox("views"),
}
set := pongo2.NewSet("wiki", l)
set.Debug = debug
return &Renderer{
set: set,
}
}
type Renderer struct {
set *pongo2.TemplateSet
}
func (r *Renderer) Render(w io.Writer, name string, data interface{}) error {
t ,e := r.set.FromCache(name+DefaultExtension)
if e != nil {
return e
}
ctx, ok := data.(map[string]interface{})
if !ok {
if data != nil {
return errors.New("data is not compatible with map[string]interface{}")
}
}
return t.ExecuteWriter(ctx, w)
}