package responses import ( "fmt" "html/template" "net/http" "git.kirsle.net/apps/gophertype/pkg/constants" ) // TemplateFuncs available to all templates. func TemplateFuncs(r *http.Request) template.FuncMap { return template.FuncMap{ "CSRF": CSRF(r), "FormValue": FormValue(r), "TestFunction": TestFunction(r), } } // CSRF returns the current CSRF token as an HTML hidden form field. func CSRF(r *http.Request) func() template.HTML { return func() template.HTML { token, _ := r.Cookie(constants.CSRFCookieName) return template.HTML(fmt.Sprintf( ``, constants.CSRFFormName, token.Value, )) } } // FormValue returns a form value (1st item only). func FormValue(r *http.Request) func(string) string { return func(key string) string { return r.FormValue(key) } } // TestFunction is a "hello world" template function. func TestFunction(r *http.Request) func() template.HTML { return func() template.HTML { return template.HTML("TestFunction() called") } }