Source file
src/text/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 func ExampleTemplate_glob() {
42
43
44
45 dir := createTestDir([]templateFile{
46
47 {"T0.tmpl", `T0 invokes T1: ({{template "T1"}})`},
48
49 {"T1.tmpl", `{{define "T1"}}T1 invokes T2: ({{template "T2"}}){{end}}`},
50
51 {"T2.tmpl", `{{define "T2"}}This is T2{{end}}`},
52 })
53
54 defer os.RemoveAll(dir)
55
56
57 pattern := filepath.Join(dir, "*.tmpl")
58
59
60
61
62 tmpl := template.Must(template.ParseGlob(pattern))
63
64 err := tmpl.Execute(os.Stdout, nil)
65 if err != nil {
66 log.Fatalf("template execution: %s", err)
67 }
68
69
70 }
71
72
73
74
75 func ExampleTemplate_helpers() {
76
77
78
79 dir := createTestDir([]templateFile{
80
81 {"T1.tmpl", `{{define "T1"}}T1 invokes T2: ({{template "T2"}}){{end}}`},
82
83 {"T2.tmpl", `{{define "T2"}}This is T2{{end}}`},
84 })
85
86 defer os.RemoveAll(dir)
87
88
89 pattern := filepath.Join(dir, "*.tmpl")
90
91
92
93 templates := template.Must(template.ParseGlob(pattern))
94
95 _, err := templates.Parse("{{define `driver1`}}Driver 1 calls T1: ({{template `T1`}})\n{{end}}")
96 if err != nil {
97 log.Fatal("parsing driver1: ", err)
98 }
99
100 _, err = templates.Parse("{{define `driver2`}}Driver 2 calls T2: ({{template `T2`}})\n{{end}}")
101 if err != nil {
102 log.Fatal("parsing driver2: ", err)
103 }
104
105
106 err = templates.ExecuteTemplate(os.Stdout, "driver1", nil)
107 if err != nil {
108 log.Fatalf("driver1 execution: %s", err)
109 }
110 err = templates.ExecuteTemplate(os.Stdout, "driver2", nil)
111 if err != nil {
112 log.Fatalf("driver2 execution: %s", err)
113 }
114
115
116
117 }
118
119
120
121 func ExampleTemplate_share() {
122
123
124
125 dir := createTestDir([]templateFile{
126
127 {"T0.tmpl", "T0 ({{.}} version) invokes T1: ({{template `T1`}})\n"},
128
129 {"T1.tmpl", `{{define "T1"}}T1 invokes T2: ({{template "T2"}}){{end}}`},
130 })
131
132 defer os.RemoveAll(dir)
133
134
135 pattern := filepath.Join(dir, "*.tmpl")
136
137
138
139 drivers := template.Must(template.ParseGlob(pattern))
140
141
142
143
144
145 first, err := drivers.Clone()
146 if err != nil {
147 log.Fatal("cloning helpers: ", err)
148 }
149
150 _, err = first.Parse("{{define `T2`}}T2, version A{{end}}")
151 if err != nil {
152 log.Fatal("parsing T2: ", err)
153 }
154
155
156
157 second, err := drivers.Clone()
158 if err != nil {
159 log.Fatal("cloning drivers: ", err)
160 }
161
162 _, err = second.Parse("{{define `T2`}}T2, version B{{end}}")
163 if err != nil {
164 log.Fatal("parsing T2: ", err)
165 }
166
167
168
169 err = second.ExecuteTemplate(os.Stdout, "T0.tmpl", "second")
170 if err != nil {
171 log.Fatalf("second execution: %s", err)
172 }
173 err = first.ExecuteTemplate(os.Stdout, "T0.tmpl", "first")
174 if err != nil {
175 log.Fatalf("first: execution: %s", err)
176 }
177
178
179
180
181 }
182
View as plain text