1 |
747 |
jeremybenn |
// Copyright 2011 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
|
6 |
|
|
|
7 |
|
|
import (
|
8 |
|
|
"fmt"
|
9 |
|
|
"io"
|
10 |
|
|
"io/ioutil"
|
11 |
|
|
"path/filepath"
|
12 |
|
|
"sync"
|
13 |
|
|
"text/template"
|
14 |
|
|
"text/template/parse"
|
15 |
|
|
)
|
16 |
|
|
|
17 |
|
|
// Template is a specialized template.Template that produces a safe HTML
|
18 |
|
|
// document fragment.
|
19 |
|
|
type Template struct {
|
20 |
|
|
escaped bool
|
21 |
|
|
// We could embed the text/template field, but it's safer not to because
|
22 |
|
|
// we need to keep our version of the name space and the underlying
|
23 |
|
|
// template's in sync.
|
24 |
|
|
text *template.Template
|
25 |
|
|
*nameSpace // common to all associated templates
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
// nameSpace is the data structure shared by all templates in an association.
|
29 |
|
|
type nameSpace struct {
|
30 |
|
|
mu sync.Mutex
|
31 |
|
|
set map[string]*Template
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
// Execute applies a parsed template to the specified data object,
|
35 |
|
|
// writing the output to wr.
|
36 |
|
|
func (t *Template) Execute(wr io.Writer, data interface{}) (err error) {
|
37 |
|
|
t.nameSpace.mu.Lock()
|
38 |
|
|
if !t.escaped {
|
39 |
|
|
if err = escapeTemplates(t, t.Name()); err != nil {
|
40 |
|
|
t.escaped = true
|
41 |
|
|
}
|
42 |
|
|
}
|
43 |
|
|
t.nameSpace.mu.Unlock()
|
44 |
|
|
if err != nil {
|
45 |
|
|
return
|
46 |
|
|
}
|
47 |
|
|
return t.text.Execute(wr, data)
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
// ExecuteTemplate applies the template associated with t that has the given
|
51 |
|
|
// name to the specified data object and writes the output to wr.
|
52 |
|
|
func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) error {
|
53 |
|
|
tmpl, err := t.lookupAndEscapeTemplate(wr, name)
|
54 |
|
|
if err != nil {
|
55 |
|
|
return err
|
56 |
|
|
}
|
57 |
|
|
return tmpl.text.Execute(wr, data)
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
// lookupAndEscapeTemplate guarantees that the template with the given name
|
61 |
|
|
// is escaped, or returns an error if it cannot be. It returns the named
|
62 |
|
|
// template.
|
63 |
|
|
func (t *Template) lookupAndEscapeTemplate(wr io.Writer, name string) (tmpl *Template, err error) {
|
64 |
|
|
t.nameSpace.mu.Lock()
|
65 |
|
|
defer t.nameSpace.mu.Unlock()
|
66 |
|
|
tmpl = t.set[name]
|
67 |
|
|
if (tmpl == nil) != (t.text.Lookup(name) == nil) {
|
68 |
|
|
panic("html/template internal error: template escaping out of sync")
|
69 |
|
|
}
|
70 |
|
|
if tmpl != nil && !tmpl.escaped {
|
71 |
|
|
err = escapeTemplates(tmpl, name)
|
72 |
|
|
}
|
73 |
|
|
return tmpl, err
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
// Parse parses a string into a template. Nested template definitions
|
77 |
|
|
// will be associated with the top-level template t. Parse may be
|
78 |
|
|
// called multiple times to parse definitions of templates to associate
|
79 |
|
|
// with t. It is an error if a resulting template is non-empty (contains
|
80 |
|
|
// content other than template definitions) and would replace a
|
81 |
|
|
// non-empty template with the same name. (In multiple calls to Parse
|
82 |
|
|
// with the same receiver template, only one call can contain text
|
83 |
|
|
// other than space, comments, and template definitions.)
|
84 |
|
|
func (t *Template) Parse(src string) (*Template, error) {
|
85 |
|
|
t.nameSpace.mu.Lock()
|
86 |
|
|
t.escaped = false
|
87 |
|
|
t.nameSpace.mu.Unlock()
|
88 |
|
|
ret, err := t.text.Parse(src)
|
89 |
|
|
if err != nil {
|
90 |
|
|
return nil, err
|
91 |
|
|
}
|
92 |
|
|
// In general, all the named templates might have changed underfoot.
|
93 |
|
|
// Regardless, some new ones may have been defined.
|
94 |
|
|
// The template.Template set has been updated; update ours.
|
95 |
|
|
t.nameSpace.mu.Lock()
|
96 |
|
|
defer t.nameSpace.mu.Unlock()
|
97 |
|
|
for _, v := range ret.Templates() {
|
98 |
|
|
name := v.Name()
|
99 |
|
|
tmpl := t.set[name]
|
100 |
|
|
if tmpl == nil {
|
101 |
|
|
tmpl = t.new(name)
|
102 |
|
|
}
|
103 |
|
|
tmpl.escaped = false
|
104 |
|
|
tmpl.text = v
|
105 |
|
|
}
|
106 |
|
|
return t, nil
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
// AddParseTree is unimplemented.
|
110 |
|
|
func (t *Template) AddParseTree(name string, tree *parse.Tree) error {
|
111 |
|
|
return fmt.Errorf("html/template: AddParseTree unimplemented")
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
// Clone is unimplemented.
|
115 |
|
|
func (t *Template) Clone(name string) error {
|
116 |
|
|
return fmt.Errorf("html/template: Clone unimplemented")
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
// New allocates a new HTML template with the given name.
|
120 |
|
|
func New(name string) *Template {
|
121 |
|
|
tmpl := &Template{
|
122 |
|
|
false,
|
123 |
|
|
template.New(name),
|
124 |
|
|
&nameSpace{
|
125 |
|
|
set: make(map[string]*Template),
|
126 |
|
|
},
|
127 |
|
|
}
|
128 |
|
|
tmpl.set[name] = tmpl
|
129 |
|
|
return tmpl
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
// New allocates a new HTML template associated with the given one
|
133 |
|
|
// and with the same delimiters. The association, which is transitive,
|
134 |
|
|
// allows one template to invoke another with a {{template}} action.
|
135 |
|
|
func (t *Template) New(name string) *Template {
|
136 |
|
|
t.nameSpace.mu.Lock()
|
137 |
|
|
defer t.nameSpace.mu.Unlock()
|
138 |
|
|
return t.new(name)
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
// new is the implementation of New, without the lock.
|
142 |
|
|
func (t *Template) new(name string) *Template {
|
143 |
|
|
tmpl := &Template{
|
144 |
|
|
false,
|
145 |
|
|
t.text.New(name),
|
146 |
|
|
t.nameSpace,
|
147 |
|
|
}
|
148 |
|
|
tmpl.set[name] = tmpl
|
149 |
|
|
return tmpl
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
// Name returns the name of the template.
|
153 |
|
|
func (t *Template) Name() string {
|
154 |
|
|
return t.text.Name()
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
// FuncMap is the type of the map defining the mapping from names to
|
158 |
|
|
// functions. Each function must have either a single return value, or two
|
159 |
|
|
// return values of which the second has type error. In that case, if the
|
160 |
|
|
// second (error) argument evaluates to non-nil during execution, execution
|
161 |
|
|
// terminates and Execute returns that error. FuncMap has the same base type
|
162 |
|
|
// as template.FuncMap, copied here so clients need not import "text/template".
|
163 |
|
|
type FuncMap map[string]interface{}
|
164 |
|
|
|
165 |
|
|
// Funcs adds the elements of the argument map to the template's function map.
|
166 |
|
|
// It panics if a value in the map is not a function with appropriate return
|
167 |
|
|
// type. However, it is legal to overwrite elements of the map. The return
|
168 |
|
|
// value is the template, so calls can be chained.
|
169 |
|
|
func (t *Template) Funcs(funcMap FuncMap) *Template {
|
170 |
|
|
t.text.Funcs(template.FuncMap(funcMap))
|
171 |
|
|
return t
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
// Delims sets the action delimiters to the specified strings, to be used in
|
175 |
|
|
// subsequent calls to Parse, ParseFiles, or ParseGlob. Nested template
|
176 |
|
|
// definitions will inherit the settings. An empty delimiter stands for the
|
177 |
|
|
// corresponding default: {{ or }}.
|
178 |
|
|
// The return value is the template, so calls can be chained.
|
179 |
|
|
func (t *Template) Delims(left, right string) *Template {
|
180 |
|
|
t.text.Delims(left, right)
|
181 |
|
|
return t
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
// Lookup returns the template with the given name that is associated with t,
|
185 |
|
|
// or nil if there is no such template.
|
186 |
|
|
func (t *Template) Lookup(name string) *Template {
|
187 |
|
|
t.nameSpace.mu.Lock()
|
188 |
|
|
defer t.nameSpace.mu.Unlock()
|
189 |
|
|
return t.set[name]
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
// Must panics if err is non-nil in the same way as template.Must.
|
193 |
|
|
func Must(t *Template, err error) *Template {
|
194 |
|
|
if err != nil {
|
195 |
|
|
panic(err)
|
196 |
|
|
}
|
197 |
|
|
return t
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
// ParseFiles creates a new Template and parses the template definitions from
|
201 |
|
|
// the named files. The returned template's name will have the (base) name and
|
202 |
|
|
// (parsed) contents of the first file. There must be at least one file.
|
203 |
|
|
// If an error occurs, parsing stops and the returned *Template is nil.
|
204 |
|
|
func ParseFiles(filenames ...string) (*Template, error) {
|
205 |
|
|
return parseFiles(nil, filenames...)
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
// ParseFiles parses the named files and associates the resulting templates with
|
209 |
|
|
// t. If an error occurs, parsing stops and the returned template is nil;
|
210 |
|
|
// otherwise it is t. There must be at least one file.
|
211 |
|
|
func (t *Template) ParseFiles(filenames ...string) (*Template, error) {
|
212 |
|
|
return parseFiles(t, filenames...)
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
// parseFiles is the helper for the method and function. If the argument
|
216 |
|
|
// template is nil, it is created from the first file.
|
217 |
|
|
func parseFiles(t *Template, filenames ...string) (*Template, error) {
|
218 |
|
|
if len(filenames) == 0 {
|
219 |
|
|
// Not really a problem, but be consistent.
|
220 |
|
|
return nil, fmt.Errorf("template: no files named in call to ParseFiles")
|
221 |
|
|
}
|
222 |
|
|
for _, filename := range filenames {
|
223 |
|
|
b, err := ioutil.ReadFile(filename)
|
224 |
|
|
if err != nil {
|
225 |
|
|
return nil, err
|
226 |
|
|
}
|
227 |
|
|
s := string(b)
|
228 |
|
|
name := filepath.Base(filename)
|
229 |
|
|
// First template becomes return value if not already defined,
|
230 |
|
|
// and we use that one for subsequent New calls to associate
|
231 |
|
|
// all the templates together. Also, if this file has the same name
|
232 |
|
|
// as t, this file becomes the contents of t, so
|
233 |
|
|
// t, err := New(name).Funcs(xxx).ParseFiles(name)
|
234 |
|
|
// works. Otherwise we create a new template associated with t.
|
235 |
|
|
var tmpl *Template
|
236 |
|
|
if t == nil {
|
237 |
|
|
t = New(name)
|
238 |
|
|
}
|
239 |
|
|
if name == t.Name() {
|
240 |
|
|
tmpl = t
|
241 |
|
|
} else {
|
242 |
|
|
tmpl = t.New(name)
|
243 |
|
|
}
|
244 |
|
|
_, err = tmpl.Parse(s)
|
245 |
|
|
if err != nil {
|
246 |
|
|
return nil, err
|
247 |
|
|
}
|
248 |
|
|
}
|
249 |
|
|
return t, nil
|
250 |
|
|
}
|
251 |
|
|
|
252 |
|
|
// ParseGlob creates a new Template and parses the template definitions from the
|
253 |
|
|
// files identified by the pattern, which must match at least one file. The
|
254 |
|
|
// returned template will have the (base) name and (parsed) contents of the
|
255 |
|
|
// first file matched by the pattern. ParseGlob is equivalent to calling
|
256 |
|
|
// ParseFiles with the list of files matched by the pattern.
|
257 |
|
|
func ParseGlob(pattern string) (*Template, error) {
|
258 |
|
|
return parseGlob(nil, pattern)
|
259 |
|
|
}
|
260 |
|
|
|
261 |
|
|
// ParseGlob parses the template definitions in the files identified by the
|
262 |
|
|
// pattern and associates the resulting templates with t. The pattern is
|
263 |
|
|
// processed by filepath.Glob and must match at least one file. ParseGlob is
|
264 |
|
|
// equivalent to calling t.ParseFiles with the list of files matched by the
|
265 |
|
|
// pattern.
|
266 |
|
|
func (t *Template) ParseGlob(pattern string) (*Template, error) {
|
267 |
|
|
return parseGlob(t, pattern)
|
268 |
|
|
}
|
269 |
|
|
|
270 |
|
|
// parseGlob is the implementation of the function and method ParseGlob.
|
271 |
|
|
func parseGlob(t *Template, pattern string) (*Template, error) {
|
272 |
|
|
filenames, err := filepath.Glob(pattern)
|
273 |
|
|
if err != nil {
|
274 |
|
|
return nil, err
|
275 |
|
|
}
|
276 |
|
|
if len(filenames) == 0 {
|
277 |
|
|
return nil, fmt.Errorf("template: pattern matches no files: %#q", pattern)
|
278 |
|
|
}
|
279 |
|
|
return parseFiles(t, filenames...)
|
280 |
|
|
}
|