Debian Codegraph @

tpl/internal/go_templates/htmltemplate/example_test.go

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