github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gofrontend/libgo/go/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 // +build ignore 6 7 package template_test 8 9 import ( 10 "fmt" 11 "html/template" 12 "log" 13 "os" 14 ) 15 16 func Example() { 17 const tpl = ` 18 <!DOCTYPE html> 19 <html> 20 <head> 21 <meta charset="UTF-8"> 22 <title>{{.Title}}</title> 23 </head> 24 <body> 25 {{range .Items}}<div>{{ . }}</div>{{else}}<div><strong>no rows</strong></div>{{end}} 26 </body> 27 </html>` 28 29 check := func(err error) { 30 if err != nil { 31 log.Fatal(err) 32 } 33 } 34 t, err := template.New("webpage").Parse(tpl) 35 36 data := struct { 37 Title string 38 Items []string 39 }{ 40 Title: "My page", 41 Items: []string{ 42 "My photos", 43 "My blog", 44 }, 45 } 46 47 err = t.Execute(os.Stdout, data) 48 check(err) 49 50 noItems := struct { 51 Title string 52 Items []string 53 }{ 54 Title: "My another page", 55 Items: []string{}, 56 } 57 58 err = t.Execute(os.Stdout, noItems) 59 check(err) 60 61 // Output: 62 // <!DOCTYPE html> 63 // <html> 64 // <head> 65 // <meta charset="UTF-8"> 66 // <title>My page</title> 67 // </head> 68 // <body> 69 // <div>My photos</div><div>My blog</div> 70 // </body> 71 // </html> 72 // <!DOCTYPE html> 73 // <html> 74 // <head> 75 // <meta charset="UTF-8"> 76 // <title>My another page</title> 77 // </head> 78 // <body> 79 // <div><strong>no rows</strong></div> 80 // </body> 81 // </html> 82 83 } 84 85 func Example_autoescaping() { 86 check := func(err error) { 87 if err != nil { 88 log.Fatal(err) 89 } 90 } 91 t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`) 92 check(err) 93 err = t.ExecuteTemplate(os.Stdout, "T", "<script>alert('you have been pwned')</script>") 94 check(err) 95 // Output: 96 // Hello, <script>alert('you have been pwned')</script>! 97 } 98 99 func Example_escape() { 100 const s = `"Fran & Freddie's Diner" <tasty@example.com>` 101 v := []interface{}{`"Fran & Freddie's Diner"`, ' ', `<tasty@example.com>`} 102 103 fmt.Println(template.HTMLEscapeString(s)) 104 template.HTMLEscape(os.Stdout, []byte(s)) 105 fmt.Fprintln(os.Stdout, "") 106 fmt.Println(template.HTMLEscaper(v...)) 107 108 fmt.Println(template.JSEscapeString(s)) 109 template.JSEscape(os.Stdout, []byte(s)) 110 fmt.Fprintln(os.Stdout, "") 111 fmt.Println(template.JSEscaper(v...)) 112 113 fmt.Println(template.URLQueryEscaper(v...)) 114 115 // Output: 116 // "Fran & Freddie's Diner" <tasty@example.com> 117 // "Fran & Freddie's Diner" <tasty@example.com> 118 // "Fran & Freddie's Diner"32<tasty@example.com> 119 // \"Fran & Freddie\'s Diner\" \x3Ctasty@example.com\x3E 120 // \"Fran & Freddie\'s Diner\" \x3Ctasty@example.com\x3E 121 // \"Fran & Freddie\'s Diner\"32\x3Ctasty@example.com\x3E 122 // %22Fran+%26+Freddie%27s+Diner%2232%3Ctasty%40example.com%3E 123 124 }