github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/tpl/internal/go_templates/htmltemplate/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 go1.13 6 7 package template_test 8 9 import ( 10 "fmt" 11 "log" 12 "os" 13 "strings" 14 15 template "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate" 16 ) 17 18 func Example() { 19 const tpl = ` 20 <!DOCTYPE html> 21 <html> 22 <head> 23 <meta charset="UTF-8"> 24 <title>{{.Title}}</title> 25 </head> 26 <body> 27 {{range .Items}}<div>{{ . }}</div>{{else}}<div><strong>no rows</strong></div>{{end}} 28 </body> 29 </html>` 30 31 check := func(err error) { 32 if err != nil { 33 log.Fatal(err) 34 } 35 } 36 t, err := template.New("webpage").Parse(tpl) 37 check(err) 38 39 data := struct { 40 Title string 41 Items []string 42 }{ 43 Title: "My page", 44 Items: []string{ 45 "My photos", 46 "My blog", 47 }, 48 } 49 50 err = t.Execute(os.Stdout, data) 51 check(err) 52 53 noItems := struct { 54 Title string 55 Items []string 56 }{ 57 Title: "My another page", 58 Items: []string{}, 59 } 60 61 err = t.Execute(os.Stdout, noItems) 62 check(err) 63 64 // Output: 65 // <!DOCTYPE html> 66 // <html> 67 // <head> 68 // <meta charset="UTF-8"> 69 // <title>My page</title> 70 // </head> 71 // <body> 72 // <div>My photos</div><div>My blog</div> 73 // </body> 74 // </html> 75 // <!DOCTYPE html> 76 // <html> 77 // <head> 78 // <meta charset="UTF-8"> 79 // <title>My another page</title> 80 // </head> 81 // <body> 82 // <div><strong>no rows</strong></div> 83 // </body> 84 // </html> 85 86 } 87 88 func Example_autoescaping() { 89 check := func(err error) { 90 if err != nil { 91 log.Fatal(err) 92 } 93 } 94 t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`) 95 check(err) 96 err = t.ExecuteTemplate(os.Stdout, "T", "<script>alert('you have been pwned')</script>") 97 check(err) 98 // Output: 99 // Hello, <script>alert('you have been pwned')</script>! 100 } 101 102 func Example_escape() { 103 const s = `"Fran & Freddie's Diner" <tasty@example.com>` 104 v := []interface{}{`"Fran & Freddie's Diner"`, ' ', `<tasty@example.com>`} 105 106 fmt.Println(template.HTMLEscapeString(s)) 107 template.HTMLEscape(os.Stdout, []byte(s)) 108 fmt.Fprintln(os.Stdout, "") 109 fmt.Println(template.HTMLEscaper(v...)) 110 111 fmt.Println(template.JSEscapeString(s)) 112 template.JSEscape(os.Stdout, []byte(s)) 113 fmt.Fprintln(os.Stdout, "") 114 fmt.Println(template.JSEscaper(v...)) 115 116 fmt.Println(template.URLQueryEscaper(v...)) 117 118 // Output: 119 // "Fran & Freddie's Diner" <tasty@example.com> 120 // "Fran & Freddie's Diner" <tasty@example.com> 121 // "Fran & Freddie's Diner"32<tasty@example.com> 122 // \"Fran \u0026 Freddie\'s Diner\" \u003Ctasty@example.com\u003E 123 // \"Fran \u0026 Freddie\'s Diner\" \u003Ctasty@example.com\u003E 124 // \"Fran \u0026 Freddie\'s Diner\"32\u003Ctasty@example.com\u003E 125 // %22Fran+%26+Freddie%27s+Diner%2232%3Ctasty%40example.com%3E 126 127 } 128 129 func ExampleTemplate_Delims() { 130 const text = "<<.Greeting>> {{.Name}}" 131 132 data := struct { 133 Greeting string 134 Name string 135 }{ 136 Greeting: "Hello", 137 Name: "Joe", 138 } 139 140 t := template.Must(template.New("tpl").Delims("<<", ">>").Parse(text)) 141 142 err := t.Execute(os.Stdout, data) 143 if err != nil { 144 log.Fatal(err) 145 } 146 147 // Output: 148 // Hello {{.Name}} 149 } 150 151 // The following example is duplicated in text/template; keep them in sync. 152 153 func ExampleTemplate_block() { 154 const ( 155 master = `Names:{{block "list" .}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}` 156 overlay = `{{define "list"}} {{join . ", "}}{{end}} ` 157 ) 158 var ( 159 funcs = template.FuncMap{"join": strings.Join} 160 guardians = []string{"Gamora", "Groot", "Nebula", "Rocket", "Star-Lord"} 161 ) 162 masterTmpl, err := template.New("master").Funcs(funcs).Parse(master) 163 if err != nil { 164 log.Fatal(err) 165 } 166 overlayTmpl, err := template.Must(masterTmpl.Clone()).Parse(overlay) 167 if err != nil { 168 log.Fatal(err) 169 } 170 if err := masterTmpl.Execute(os.Stdout, guardians); err != nil { 171 log.Fatal(err) 172 } 173 if err := overlayTmpl.Execute(os.Stdout, guardians); err != nil { 174 log.Fatal(err) 175 } 176 // Output: 177 // Names: 178 // - Gamora 179 // - Groot 180 // - Nebula 181 // - Rocket 182 // - Star-Lord 183 // Names: Gamora, Groot, Nebula, Rocket, Star-Lord 184 }