package web import ( "html/template" "net/http" "github.com/gorilla/mux" ) // TmplFuncs handles global template functions var tmplFuncs = []template.FuncMap{ map[string]interface{}{ "safeURL": func(s string) template.URL { return template.URL(s) }, }, } // AddTmplFunc adds a template function func AddTmplFunc(name string, f interface{}) error { tmplFuncs = append(tmplFuncs, map[string]interface{}{ name: f, }) return nil } // TemplateData represents object passed to template renderer type TemplateData struct { Route string Data map[string]interface{} } // Rends a view func (e *Env) Rends(w http.ResponseWriter, r *http.Request, template string) error { if r.Header.Get("Accept") == "application/json" { return e.Render.JSON(w, http.StatusOK, TemplateData{ Route: mux.CurrentRoute(r).GetName(), Data: GetAllData(r), }) } return e.Render.HTML(w, http.StatusOK, template, TemplateData{ Route: mux.CurrentRoute(r).GetName(), Data: GetAllData(r), }) }