Created
August 5, 2017 18:46
-
-
Save ntakouris/c27676e2c2f79acab902f3f5f16c3a6a to your computer and use it in GitHub Desktop.
Server Side HTML Render snipper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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