A web blog and personal homepage engine written in Go.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.0 KiB

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(
`<input type="hidden" name="%s" value="%s">`,
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")
}
}