PR c++/86342 - -Wdeprecated-copy and system headers.
[official-gcc.git] / libgo / go / html / template / example_test.go
blobde7cdbbd91ac1d5452cbea7ef3a07797f5fbc3d0
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.
5 // +build ignore
7 package template_test
9 import (
10 "fmt"
11 "html/template"
12 "log"
13 "os"
14 "strings"
17 func Example() {
18 const tpl = `
19 <!DOCTYPE html>
20 <html>
21 <head>
22 <meta charset="UTF-8">
23 <title>{{.Title}}</title>
24 </head>
25 <body>
26 {{range .Items}}<div>{{ . }}</div>{{else}}<div><strong>no rows</strong></div>{{end}}
27 </body>
28 </html>`
30 check := func(err error) {
31 if err != nil {
32 log.Fatal(err)
35 t, err := template.New("webpage").Parse(tpl)
36 check(err)
38 data := struct {
39 Title string
40 Items []string
42 Title: "My page",
43 Items: []string{
44 "My photos",
45 "My blog",
49 err = t.Execute(os.Stdout, data)
50 check(err)
52 noItems := struct {
53 Title string
54 Items []string
56 Title: "My another page",
57 Items: []string{},
60 err = t.Execute(os.Stdout, noItems)
61 check(err)
63 // Output:
64 // <!DOCTYPE html>
65 // <html>
66 // <head>
67 // <meta charset="UTF-8">
68 // <title>My page</title>
69 // </head>
70 // <body>
71 // <div>My photos</div><div>My blog</div>
72 // </body>
73 // </html>
74 // <!DOCTYPE html>
75 // <html>
76 // <head>
77 // <meta charset="UTF-8">
78 // <title>My another page</title>
79 // </head>
80 // <body>
81 // <div><strong>no rows</strong></div>
82 // </body>
83 // </html>
87 func Example_autoescaping() {
88 check := func(err error) {
89 if err != nil {
90 log.Fatal(err)
93 t, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
94 check(err)
95 err = t.ExecuteTemplate(os.Stdout, "T", "<script>alert('you have been pwned')</script>")
96 check(err)
97 // Output:
98 // Hello, &lt;script&gt;alert(&#39;you have been pwned&#39;)&lt;/script&gt;!
101 func Example_escape() {
102 const s = `"Fran & Freddie's Diner" <tasty@example.com>`
103 v := []interface{}{`"Fran & Freddie's Diner"`, ' ', `<tasty@example.com>`}
105 fmt.Println(template.HTMLEscapeString(s))
106 template.HTMLEscape(os.Stdout, []byte(s))
107 fmt.Fprintln(os.Stdout, "")
108 fmt.Println(template.HTMLEscaper(v...))
110 fmt.Println(template.JSEscapeString(s))
111 template.JSEscape(os.Stdout, []byte(s))
112 fmt.Fprintln(os.Stdout, "")
113 fmt.Println(template.JSEscaper(v...))
115 fmt.Println(template.URLQueryEscaper(v...))
117 // Output:
118 // &#34;Fran &amp; Freddie&#39;s Diner&#34; &lt;tasty@example.com&gt;
119 // &#34;Fran &amp; Freddie&#39;s Diner&#34; &lt;tasty@example.com&gt;
120 // &#34;Fran &amp; Freddie&#39;s Diner&#34;32&lt;tasty@example.com&gt;
121 // \"Fran & Freddie\'s Diner\" \x3Ctasty@example.com\x3E
122 // \"Fran & Freddie\'s Diner\" \x3Ctasty@example.com\x3E
123 // \"Fran & Freddie\'s Diner\"32\x3Ctasty@example.com\x3E
124 // %22Fran+%26+Freddie%27s+Diner%2232%3Ctasty%40example.com%3E
128 // The following example is duplicated in text/template; keep them in sync.
130 func ExampleTemplate_block() {
131 const (
132 master = `Names:{{block "list" .}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}`
133 overlay = `{{define "list"}} {{join . ", "}}{{end}} `
135 var (
136 funcs = template.FuncMap{"join": strings.Join}
137 guardians = []string{"Gamora", "Groot", "Nebula", "Rocket", "Star-Lord"}
139 masterTmpl, err := template.New("master").Funcs(funcs).Parse(master)
140 if err != nil {
141 log.Fatal(err)
143 overlayTmpl, err := template.Must(masterTmpl.Clone()).Parse(overlay)
144 if err != nil {
145 log.Fatal(err)
147 if err := masterTmpl.Execute(os.Stdout, guardians); err != nil {
148 log.Fatal(err)
150 if err := overlayTmpl.Execute(os.Stdout, guardians); err != nil {
151 log.Fatal(err)
153 // Output:
154 // Names:
155 // - Gamora
156 // - Groot
157 // - Nebula
158 // - Rocket
159 // - Star-Lord
160 // Names: Gamora, Groot, Nebula, Rocket, Star-Lord