Source file
src/html/template/examplefiles_test.go
1
2
3
4
5 package template_test
6
7 import (
8 "io"
9 "log"
10 "os"
11 "path/filepath"
12 "text/template"
13 )
14
15
16 type templateFile struct {
17 name string
18 contents string
19 }
20
21 func createTestDir(files []templateFile) string {
22 dir, err := os.MkdirTemp("", "template")
23 if err != nil {
24 log.Fatal(err)
25 }
26 for _, file := range files {
27 f, err := os.Create(filepath.Join(dir, file.name))
28 if err != nil {
29 log.Fatal(err)
30 }
31 defer f.Close()
32 _, err = io.WriteString(f, file.contents)
33 if err != nil {
34 log.Fatal(err)
35 }
36 }
37 return dir
38 }
39
40
41
42
43 func ExampleTemplate_glob() {
44
45
46
47 dir := createTestDir([]templateFile{
48
49 {"T0.tmpl", `T0 invokes T1: ({{template "T1"}})`},
50
51 {"T1.tmpl", `{{define "T1"}}T1 invokes T2: ({{template "T2"}}){{end}}`},
52
53 {"T2.tmpl", `{{define "T2"}}This is T2{{end}}`},
54 })
55
56 defer os.RemoveAll(dir)
57
58
59 pattern := filepath.Join(dir, "*.tmpl")
60
61
62
63
64 tmpl := template.Must(template.ParseGlob(pattern))
65
66 err := tmpl.Execute(os.Stdout, nil)
67 if err != nil {
68 log.Fatalf("template execution: %s", err)
69 }
70
71
72 }
73
74
75 func ExampleTemplate_parsefiles() {
76
77
78
79 dir1 := createTestDir([]templateFile{
80
81 {"T1.tmpl", `T1 invokes T2: ({{template "T2"}})`},
82 })
83
84 dir2 := createTestDir([]templateFile{
85
86 {"T2.tmpl", `{{define "T2"}}This is T2{{end}}`},
87 })
88
89
90 defer func(dirs ...string) {
91 for _, dir := range dirs {
92 os.RemoveAll(dir)
93 }
94 }(dir1, dir2)
95
96
97
98 paths := []string{
99 filepath.Join(dir1, "T1.tmpl"),
100 filepath.Join(dir2, "T2.tmpl"),
101 }
102 tmpl := template.Must(template.ParseFiles(paths...))
103
104 err := tmpl.Execute(os.Stdout, nil)
105 if err != nil {
106 log.Fatalf("template execution: %s", err)
107 }
108
109
110 }
111
112
113
114
115
116
117 func ExampleTemplate_helpers() {
118
119
120
121 dir := createTestDir([]templateFile{
122
123 {"T1.tmpl", `{{define "T1"}}T1 invokes T2: ({{template "T2"}}){{end}}`},
124
125 {"T2.tmpl", `{{define "T2"}}This is T2{{end}}`},
126 })
127
128 defer os.RemoveAll(dir)
129
130
131 pattern := filepath.Join(dir, "*.tmpl")
132
133
134
135 templates := template.Must(template.ParseGlob(pattern))
136
137 _, err := templates.Parse("{{define `driver1`}}Driver 1 calls T1: ({{template `T1`}})\n{{end}}")
138 if err != nil {
139 log.Fatal("parsing driver1: ", err)
140 }
141
142 _, err = templates.Parse("{{define `driver2`}}Driver 2 calls T2: ({{template `T2`}})\n{{end}}")
143 if err != nil {
144 log.Fatal("parsing driver2: ", err)
145 }
146
147
148 err = templates.ExecuteTemplate(os.Stdout, "driver1", nil)
149 if err != nil {
150 log.Fatalf("driver1 execution: %s", err)
151 }
152 err = templates.ExecuteTemplate(os.Stdout, "driver2", nil)
153 if err != nil {
154 log.Fatalf("driver2 execution: %s", err)
155 }
156
157
158
159 }
160
161
162
163
164
165 func ExampleTemplate_share() {
166
167
168
169 dir := createTestDir([]templateFile{
170
171 {"T0.tmpl", "T0 ({{.}} version) invokes T1: ({{template `T1`}})\n"},
172
173 {"T1.tmpl", `{{define "T1"}}T1 invokes T2: ({{template "T2"}}){{end}}`},
174 })
175
176 defer os.RemoveAll(dir)
177
178
179 pattern := filepath.Join(dir, "*.tmpl")
180
181
182
183 drivers := template.Must(template.ParseGlob(pattern))
184
185
186
187
188
189 first, err := drivers.Clone()
190 if err != nil {
191 log.Fatal("cloning helpers: ", err)
192 }
193
194 _, err = first.Parse("{{define `T2`}}T2, version A{{end}}")
195 if err != nil {
196 log.Fatal("parsing T2: ", err)
197 }
198
199
200
201 second, err := drivers.Clone()
202 if err != nil {
203 log.Fatal("cloning drivers: ", err)
204 }
205
206 _, err = second.Parse("{{define `T2`}}T2, version B{{end}}")
207 if err != nil {
208 log.Fatal("parsing T2: ", err)
209 }
210
211
212
213 err = second.ExecuteTemplate(os.Stdout, "T0.tmpl", "second")
214 if err != nil {
215 log.Fatalf("second execution: %s", err)
216 }
217 err = first.ExecuteTemplate(os.Stdout, "T0.tmpl", "first")
218 if err != nil {
219 log.Fatalf("first: execution: %s", err)
220 }
221
222
223
224
225 }
226
View as plain text