Golang html/template.HTML type examples
package html/template
HTML encapsulates a known safe HTML document fragment. It should not be used for HTML from a third-party, or HTML with unclosed tags or comments. The outputs of a sound HTML sanitizer and a template escaped by this package are fine for use with HTML.
Golang html/template.HTML type usage examples
Example 1:
func tmpl(w io.Writer, text string, data interface{}) {
t := template.New("top")
t.Funcs(template.FuncMap{"trim": func(s template.HTML) template.HTML {
return template.HTML(strings.TrimSpace(string(s))) // <-- here
}})
template.Must(t.Parse(text))
if err := t.Execute(w, data); err != nil {
panic(err)
}
}
Example 2:
type info struct {
Title string
IE template.HTML
}
var ie template.HTML = `
<!--[if lt IE 9]>
<script src="js/html5shiv.min.js"></script>
<script src="js/respond.min.js"></script>
<![endif]-->`
func rootHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
title := "Home"
templates.ExecuteTemplate(w, "header.html", &info{title, ie})
templates.ExecuteTemplate(w, "index.html", nil)
templates.ExecuteTemplate(w, "footer.html", title)
}
References :
https://github.com/aclissold/andrewclissold.appspot.com/blob/master/app.go
Advertisement
Something interesting
Tutorials
+14.8k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+14.9k Golang : Submit web forms without browser by http.PostForm example
+21.1k Golang : For loop continue,break and range
+9.7k Golang : interface - when and where to use examples
+5.6k PHP : Convert CSV to JSON with YQL example
+12.4k Elastic Search : Return all records (higher than default 10)
+13.6k Golang : Qt progress dialog example
+9.7k PHP : Get coordinates latitude/longitude from string
+10.5k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+10.5k Swift : Convert (cast) String to Integer
+48.5k Golang : Upload file from web browser to server
+8.3k Golang : Oanda bot with Telegram and RSI example