...
1
2
3
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 check(err)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
96
97 }
98
99 func Example_escape() {
100 const s = `"Fran & Freddie's Diner" <[email protected]>`
101 v := []interface{}{`"Fran & Freddie's Diner"`, ' ', `<[email protected]>`}
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
116
117
118
119
120
121
122
123
124 }
125
126 func ExampleTemplate_Delims() {
127 const text = "<<.Greeting>> {{.Name}}"
128
129 data := struct {
130 Greeting string
131 Name string
132 }{
133 Greeting: "Hello",
134 Name: "Joe",
135 }
136
137 t := template.Must(template.New("tpl").Delims("<<", ">>").Parse(text))
138
139 err := t.Execute(os.Stdout, data)
140 if err != nil {
141 log.Fatal(err)
142 }
143
144
145
146 }
147
148
149
150 func ExampleTemplate_block() {
151 const (
152 master = `Names:{{block "list" .}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}`
153 overlay = `{{define "list"}} {{join . ", "}}{{end}} `
154 )
155 var (
156 funcs = template.FuncMap{"join": strings.Join}
157 guardians = []string{"Gamora", "Groot", "Nebula", "Rocket", "Star-Lord"}
158 )
159 masterTmpl, err := template.New("master").Funcs(funcs).Parse(master)
160 if err != nil {
161 log.Fatal(err)
162 }
163 overlayTmpl, err := template.Must(masterTmpl.Clone()).Parse(overlay)
164 if err != nil {
165 log.Fatal(err)
166 }
167 if err := masterTmpl.Execute(os.Stdout, guardians); err != nil {
168 log.Fatal(err)
169 }
170 if err := overlayTmpl.Execute(os.Stdout, guardians); err != nil {
171 log.Fatal(err)
172 }
173
174
175
176
177
178
179
180
181 }
182
View as plain text