github.com/d4l3k/go@v0.0.0-20151015000803-65fc379daeda/src/html/template/example_test.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package template_test 6 7 import ( 8 "fmt" 9 "html/template" 10 "log" 11 "os" 12 "strings" 13 ) 14 15 func Example() { 16 const tpl = ` 17 <!DOCTYPE html> 18 <html> 19 <head> 20 <meta charset="UTF-8"> 21 <title>{{.Title}}</title> 22 </head> 23 <body> 24 {{range .Items}}<div>{{ . }}</div>{{else}}<div><strong>no rows</strong></div>{{end}} 25 </body> 26 </html>` 27 28 check := func(err error) { 29 if err != nil { 30 log.Fatal(err) 31 } 32 } 33 t, err := template.New("webpage").Parse(tpl) 34 35 data := struct { 36 Title string 37 Items []string 38 }{ 39 Title: "My page", 40 Items: []string{ 41 "My photos", 42 "My blog", 43 }, 44 } 45 46 err = t.Execute(os.Stdout, data) 47 check(err) 48 49 noItems := struct { 50 Title string 51 Items []string 52 }{ 53 Title: "My another page", 54 Items: []string{}, 55 } 56 57 err = t.Execute(os.Stdout, noItems) 58 check(err) 59 60 // Output: 61 // <!DOCTYPE html> 62 // <html> 63 // <head> 64 // <meta charset="UTF-8"> 65 // <title>My page</title> 66 // </head> 67 // <body> 68 // <div>My photos</div><div>My blog</div> 69 // </body> 70 // </html> 71 // <!DOCTYPE html> 72 // <html> 73 // <head> 74 // <meta charset="UTF-8"> 75 // <title>My another page</title> 76 // </head> 77 // <body> 78 // <div><strong>no rows</strong></div> 79 // </body> 80 // </html> 81 82 } 83 84 func Example_autoescaping() { 85 check := func(err error) { 86 if err != nil { 87 log.Fatal(err) 88 } 89 } 90 t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`) 91 check(err) 92 err = t.ExecuteTemplate(os.Stdout, "T", "<script>alert('you have been pwned')</script>") 93 check(err) 94 // Output: 95 // Hello, <script>alert('you have been pwned')</script>! 96 } 97 98 func Example_escape() { 99 const s = `"Fran & Freddie's Diner" <tasty@example.com>` 100 v := []interface{}{`"Fran & Freddie's Diner"`, ' ', `<tasty@example.com>`} 101 102 fmt.Println(template.HTMLEscapeString(s)) 103 template.HTMLEscape(os.Stdout, []byte(s)) 104 fmt.Fprintln(os.Stdout, "") 105 fmt.Println(template.HTMLEscaper(v...)) 106 107 fmt.Println(template.JSEscapeString(s)) 108 template.JSEscape(os.Stdout, []byte(s)) 109 fmt.Fprintln(os.Stdout, "") 110 fmt.Println(template.JSEscaper(v...)) 111 112 fmt.Println(template.URLQueryEscaper(v...)) 113 114 // Output: 115 // "Fran & Freddie's Diner" <tasty@example.com> 116 // "Fran & Freddie's Diner" <tasty@example.com> 117 // "Fran & Freddie's Diner"32<tasty@example.com> 118 // \"Fran & Freddie\'s Diner\" \x3Ctasty@example.com\x3E 119 // \"Fran & Freddie\'s Diner\" \x3Ctasty@example.com\x3E 120 // \"Fran & Freddie\'s Diner\"32\x3Ctasty@example.com\x3E 121 // %22Fran+%26+Freddie%27s+Diner%2232%3Ctasty%40example.com%3E 122 123 } 124 125 // The following example is duplicated in text/template; keep them in sync. 126 127 func ExampleTemplate_block() { 128 const ( 129 master = `Names:{{block "list" .}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}` 130 overlay = `{{define "list"}} {{join . ", "}}{{end}} ` 131 ) 132 var ( 133 funcs = template.FuncMap{"join": strings.Join} 134 guardians = []string{"Gamora", "Groot", "Nebula", "Rocket", "Star-Lord"} 135 ) 136 masterTmpl, err := template.New("master").Funcs(funcs).Parse(master) 137 if err != nil { 138 log.Fatal(err) 139 } 140 overlayTmpl, err := template.Must(masterTmpl.Clone()).Parse(overlay) 141 if err != nil { 142 log.Fatal(err) 143 } 144 if err := masterTmpl.Execute(os.Stdout, guardians); err != nil { 145 log.Fatal(err) 146 } 147 if err := overlayTmpl.Execute(os.Stdout, guardians); err != nil { 148 log.Fatal(err) 149 } 150 // Output: 151 // Names: 152 // - Gamora 153 // - Groot 154 // - Nebula 155 // - Rocket 156 // - Star-Lord 157 // Names: Gamora, Groot, Nebula, Rocket, Star-Lord 158 }