47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
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 {
|
|
RouteName 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{
|
|
RouteName: mux.CurrentRoute(r).GetName(),
|
|
Data: GetAllData(r),
|
|
})
|
|
}
|
|
|
|
return e.Render.HTML(w, http.StatusOK, template, TemplateData{
|
|
RouteName: mux.CurrentRoute(r).GetName(),
|
|
Data: GetAllData(r),
|
|
})
|
|
}
|