3 Things You May Not Know About The Go Template Engine

3 Things You May Not Know About The Go Template Engine

A short introduction to the Go template engine

Ee191858f0d96ad93098694537f71998?s=128

Sau Sheong Chang

March 03, 2015
Tweet

Transcript

  1. 3  Things  You  May  Not   Know  About  The  Go

      Template  Engine   Chang  Sau  Sheong   Feb  2015  
  2. Obligatory  stuff  

  3. What  is  a  template   engine?  

  4. None
  5. None
  6. Where’s  Go  template  engine?   Logic-­‐less   Go   template

      engine   Embedded   logic   Mustache,  Handlebars  etc   JSP,  Haml,  Jade  etc  
  7. How  does  the  Go  template   engine  work?  

  8. text/template   html/template  

  9. Parse   template   Execute   template  

  10. <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web

    Programming</title> </head> <body> {{ . }} </body> </html>
  11. package main import ( "net/http" "html/template" ) func process(w http.ResponseWriter,

    r *http.Request) { t, _ := template.ParseFiles("tmpl.html") t.Execute(w, "Hello World!") } func main() { server := http.Server{ Addr: "127.0.0.1:8080", } http.HandleFunc("/process", process) server.ListenAndServe() }
  12. The  Go  template  engine  is   kinda  cool  

  13. 1.  Pipelines  

  14. {{ p1 | p2 | p3 }}

  15. <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/ html; charset=utf-8"> <title>Go

    Web Programming</title> </head> <body> {{ 12.3456 | printf "%.2f" }} </body> </html>
  16. 2.  FuncTons  

  17. func formatDate(t time.Time) string { layout := "2006-01-02" return t.Format(layout)

    } func process(w http.ResponseWriter, r *http.Request) { funcMap := template.FuncMap { "fdate": formatDate } t := template.New("tmpl.html").Funcs(funcMap) t, _ = t.ParseFiles("tmpl.html") t.Execute(w, time.Now()) }
  18. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web Programming</title> </head>

    <body> <div>The date/time is {{ fdate . }}</div> </body> </html>
  19. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web Programming</title> </head>

    <body> <div>The date/time is {{ . | fdate }}</div> </body> </html>
  20. 3.  Context  aware  

  21. func process(w http.ResponseWriter, r *http.Request) { t, _ := template.ParseFiles("tmpl.html")

    content := `I asked: <i>"What's up?"</i>` t.Execute(w, content) }
  22. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web Programming</title> </head>

    <body> <div>{{ . }}</div> <div><a href="/{{ . }}">Path</a></div> <div><a href="/?q={{ . }}">Query</a></div> <div><a onclick="f('{{ . }}')">Onclick</a></div> </body> </html>
  23. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web Programming</title> </head>

    <body> <div>I asked: &lt;i&gt;&#34;What&#39;s up?&#34;&lt;/ i&gt;</div> <div><a href="/I%20asked:%20%3ci%3e%22What%27s%20up? %22%3c/i%3e">Path</a></div> <div><a href="/?q=I%20asked%3a%20%3ci%3e%22What%27s %20up%3f%22%3c%2fi%3e">Query</a></div> <div><a onclick="f('I asked: \x3ci\x3e\x22What\x27s up?\x22\x3c\/i\x3e')">Onclick</a></div> </body> </html>
  24. Context Content Original text I asked: <i>"What's up?"</i> {{ .

    }} I asked: &lt;i&gt;&#34;What&#39;s up?&#34;&lt;/i&gt; <a href="/{{ . }}"> I%20asked:%20%3ci%3e%22What%27s%20up?%22%3c/i%3e <a href="/?q={{ . }}"> I%20asked%3a%20%3ci%3e%22What%27s%20up%3f%22%3c %2fi%3e <a onclick="{{ . }}"> I asked: \x3ci\x3e\x22What\x27s up?\x22\x3c\/i\x3e
  25. XSS  

  26. XSS  Demo    

  27. hWp://manning.com/chang  

  28. Thank  you   sausheong@gmail.com   @sausheong