summaryrefslogtreecommitdiff
path: root/libgo/go/html/template/template.go
blob: 47334299384bf9c7eedcf60b1c1a46486398bf91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package template

import (
	"fmt"
	"io"
	"path/filepath"
	"text/template"
)

// Set is a specialized template.Set that produces a safe HTML document
// fragment.
type Set struct {
	escaped map[string]bool
	template.Set
}

// Template is a specialized template.Template that produces a safe HTML
// document fragment.
type Template struct {
	escaped bool
	*template.Template
}

// Execute applies the named template to the specified data object, writing
// the output to wr.
func (s *Set) Execute(wr io.Writer, name string, data interface{}) error {
	if !s.escaped[name] {
		if err := escapeSet(&s.Set, name); err != nil {
			return err
		}
		if s.escaped == nil {
			s.escaped = make(map[string]bool)
		}
		s.escaped[name] = true
	}
	return s.Set.Execute(wr, name, data)
}

// Parse parses a string into a set of named templates.  Parse may be called
// multiple times for a given set, adding the templates defined in the string
// to the set.  If a template is redefined, the element in the set is
// overwritten with the new definition.
func (set *Set) Parse(src string) (*Set, error) {
	set.escaped = nil
	s, err := set.Set.Parse(src)
	if err != nil {
		return nil, err
	}
	if s != &(set.Set) {
		panic("allocated new set")
	}
	return set, nil
}

// Parse parses the template definition string to construct an internal
// representation of the template for execution.
func (tmpl *Template) Parse(src string) (*Template, error) {
	tmpl.escaped = false
	t, err := tmpl.Template.Parse(src)
	if err != nil {
		return nil, err
	}
	tmpl.Template = t
	return tmpl, nil
}

// Execute applies a parsed template to the specified data object,
// writing the output to wr.
func (t *Template) Execute(wr io.Writer, data interface{}) error {
	if !t.escaped {
		if err := escape(t.Template); err != nil {
			return err
		}
		t.escaped = true
	}
	return t.Template.Execute(wr, data)
}

// New allocates a new HTML template with the given name.
func New(name string) *Template {
	return &Template{false, template.New(name)}
}

// Must panics if err is non-nil in the same way as template.Must.
func Must(t *Template, err error) *Template {
	t.Template = template.Must(t.Template, err)
	return t
}

// ParseFile creates a new Template and parses the template definition from
// the named file.  The template name is the base name of the file.
func ParseFile(filename string) (*Template, error) {
	t, err := template.ParseFile(filename)
	if err != nil {
		return nil, err
	}
	return &Template{false, t}, nil
}

// ParseFile reads the template definition from a file and parses it to
// construct an internal representation of the template for execution.
// The returned template will be nil if an error occurs.
func (tmpl *Template) ParseFile(filename string) (*Template, error) {
	t, err := tmpl.Template.ParseFile(filename)
	if err != nil {
		return nil, err
	}
	tmpl.Template = t
	return tmpl, nil
}

// SetMust panics if the error is non-nil just like template.SetMust.
func SetMust(s *Set, err error) *Set {
	if err != nil {
		template.SetMust(&(s.Set), err)
	}
	return s
}

// ParseFiles parses the named files into a set of named templates.
// Each file must be parseable by itself.
// If an error occurs, parsing stops and the returned set is nil.
func (set *Set) ParseFiles(filenames ...string) (*Set, error) {
	s, err := set.Set.ParseFiles(filenames...)
	if err != nil {
		return nil, err
	}
	if s != &(set.Set) {
		panic("allocated new set")
	}
	return set, nil
}

// ParseSetFiles creates a new Set and parses the set definition from the
// named files. Each file must be individually parseable.
func ParseSetFiles(filenames ...string) (*Set, error) {
	set := new(Set)
	s, err := set.Set.ParseFiles(filenames...)
	if err != nil {
		return nil, err
	}
	if s != &(set.Set) {
		panic("allocated new set")
	}
	return set, nil
}

// ParseGlob parses the set definition from the files identified by the
// pattern. The pattern is processed by filepath.Glob and must match at
// least one file.
// If an error occurs, parsing stops and the returned set is nil.
func (s *Set) ParseGlob(pattern string) (*Set, error) {
	filenames, err := filepath.Glob(pattern)
	if err != nil {
		return nil, err
	}
	if len(filenames) == 0 {
		return nil, fmt.Errorf("pattern matches no files: %#q", pattern)
	}
	return s.ParseFiles(filenames...)
}

// ParseSetGlob creates a new Set and parses the set definition from the
// files identified by the pattern. The pattern is processed by filepath.Glob
// and must match at least one file.
func ParseSetGlob(pattern string) (*Set, error) {
	set, err := new(Set).ParseGlob(pattern)
	if err != nil {
		return nil, err
	}
	return set, nil
}

// Functions and methods to parse stand-alone template files into a set.

// ParseTemplateFiles parses the named template files and adds them to the set
// in the same way as template.ParseTemplateFiles but ensures that templates
// with upper-case names are contextually-autoescaped.
func (set *Set) ParseTemplateFiles(filenames ...string) (*Set, error) {
	s, err := set.Set.ParseTemplateFiles(filenames...)
	if err != nil {
		return nil, err
	}
	if s != &(set.Set) {
		panic("new set allocated")
	}
	return set, nil
}

// ParseTemplateGlob parses the template files matched by the
// patern and adds them to the set. Each template will be named
// the base name of its file.
// Unlike with ParseGlob, each file should be a stand-alone template
// definition suitable for Template.Parse (not Set.Parse); that is, the
// file does not contain {{define}} clauses. ParseTemplateGlob is
// therefore equivalent to calling the ParseFile function to create
// individual templates, which are then added to the set.
// Each file must be parseable by itself.
// If an error occurs, parsing stops and the returned set is nil.
func (s *Set) ParseTemplateGlob(pattern string) (*Set, error) {
	filenames, err := filepath.Glob(pattern)
	if err != nil {
		return nil, err
	}
	return s.ParseTemplateFiles(filenames...)
}

// ParseTemplateFiles creates a set by parsing the named files,
// each of which defines a single template. Each template will be
// named the base name of its file.
// Unlike with ParseFiles, each file should be a stand-alone template
// definition suitable for Template.Parse (not Set.Parse); that is, the
// file does not contain {{define}} clauses. ParseTemplateFiles is
// therefore equivalent to calling the ParseFile function to create
// individual templates, which are then added to the set.
// Each file must be parseable by itself. Parsing stops if an error is
// encountered.
func ParseTemplateFiles(filenames ...string) (*Set, error) {
	return new(Set).ParseTemplateFiles(filenames...)
}

// ParseTemplateGlob creates a set by parsing the files matched
// by the pattern, each of which defines a single template. The pattern
// is processed by filepath.Glob and must match at least one file. Each
// template will be named the base name of its file.
// Unlike with ParseGlob, each file should be a stand-alone template
// definition suitable for Template.Parse (not Set.Parse); that is, the
// file does not contain {{define}} clauses. ParseTemplateGlob is
// therefore equivalent to calling the ParseFile function to create
// individual templates, which are then added to the set.
// Each file must be parseable by itself. Parsing stops if an error is
// encountered.
func ParseTemplateGlob(pattern string) (*Set, error) {
	return new(Set).ParseTemplateGlob(pattern)
}