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