Skip to content

Instantly share code, notes, and snippets.

@ntakouris
Created August 5, 2017 18:46
Show Gist options
  • Save ntakouris/c27676e2c2f79acab902f3f5f16c3a6a to your computer and use it in GitHub Desktop.
Save ntakouris/c27676e2c2f79acab902f3f5f16c3a6a to your computer and use it in GitHub Desktop.
Server Side HTML Render snipper
// TemplateField is a template helper for html/template that provides an <input> field
// populated with a CSRF token.
//
// Example:
//
// // The following tag in our form.tmpl template:
// {{ .csrfField }}
//
// // ... becomes:
// <input type="hidden" name="gorilla.csrf.Token" value="<token>">
//
func TemplateField(r *http.Request) template.HTML {
 if name, err := contextGet(r, formKey); err == nil {
  fragment := fmt.Sprintf(`<input type="hidden" name="%s" value="%s">`,
  name, Token(r))
  return template.HTML(fragment)
 }
 return template.HTML("")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment