@@ -120,10 +120,11 @@ type HTMLOptions struct {
120
120
// Render is a service that provides functions for easily writing JSON, XML,
121
121
// binary data, and HTML templates out to a HTTP Response.
122
122
type Render struct {
123
+ lock sync.RWMutex
124
+
123
125
// Customize Secure with an Options struct.
124
126
opt Options
125
127
templates * template.Template
126
- templatesLk sync.RWMutex
127
128
compiledCharset string
128
129
}
129
130
@@ -242,9 +243,9 @@ func (r *Render) compileTemplatesFromDir() {
242
243
return nil
243
244
})
244
245
245
- r .templatesLk .Lock ()
246
+ r .lock .Lock ()
247
+ defer r .lock .Unlock ()
246
248
r .templates = tmpTemplates
247
- r .templatesLk .Unlock ()
248
249
}
249
250
250
251
func (r * Render ) compileTemplatesFromAsset () {
@@ -289,28 +290,29 @@ func (r *Render) compileTemplatesFromAsset() {
289
290
}
290
291
}
291
292
}
292
-
293
- r . templatesLk . Lock ()
293
+ r . lock . Lock ()
294
+ defer r . lock . Unlock ()
294
295
r .templates = tmpTemplates
295
- r .templatesLk .Unlock ()
296
296
}
297
297
298
298
// TemplateLookup is a wrapper around template.Lookup and returns
299
299
// the template with the given name that is associated with t, or nil
300
300
// if there is no such template.
301
301
func (r * Render ) TemplateLookup (t string ) * template.Template {
302
+ r .lock .RLock ()
303
+ defer r .lock .RUnlock ()
302
304
return r .templates .Lookup (t )
303
305
}
304
306
305
- func (r * Render ) execute (name string , binding interface {}) (* bytes.Buffer , error ) {
307
+ func (r * Render ) execute (templates * template. Template , name string , binding interface {}) (* bytes.Buffer , error ) {
306
308
buf := new (bytes.Buffer )
307
- return buf , r . templates .ExecuteTemplate (buf , name , binding )
309
+ return buf , templates .ExecuteTemplate (buf , name , binding )
308
310
}
309
311
310
- func (r * Render ) layoutFuncs (name string , binding interface {}) template.FuncMap {
312
+ func (r * Render ) layoutFuncs (templates * template. Template , name string , binding interface {}) template.FuncMap {
311
313
return template.FuncMap {
312
314
"yield" : func () (template.HTML , error ) {
313
- buf , err := r .execute (name , binding )
315
+ buf , err := r .execute (templates , name , binding )
314
316
// Return safe HTML here since we are rendering our own template.
315
317
return template .HTML (buf .String ()), err
316
318
},
@@ -320,23 +322,23 @@ func (r *Render) layoutFuncs(name string, binding interface{}) template.FuncMap
320
322
"block" : func (partialName string ) (template.HTML , error ) {
321
323
log .Print ("Render's `block` implementation is now depericated. Use `partial` as a drop in replacement." )
322
324
fullPartialName := fmt .Sprintf ("%s-%s" , partialName , name )
323
- if r . TemplateLookup (fullPartialName ) == nil && r .opt .RenderPartialsWithoutPrefix {
325
+ if templates . Lookup (fullPartialName ) == nil && r .opt .RenderPartialsWithoutPrefix {
324
326
fullPartialName = partialName
325
327
}
326
- if r .opt .RequireBlocks || r . TemplateLookup (fullPartialName ) != nil {
327
- buf , err := r .execute (fullPartialName , binding )
328
+ if r .opt .RequireBlocks || templates . Lookup (fullPartialName ) != nil {
329
+ buf , err := r .execute (templates , fullPartialName , binding )
328
330
// Return safe HTML here since we are rendering our own template.
329
331
return template .HTML (buf .String ()), err
330
332
}
331
333
return "" , nil
332
334
},
333
335
"partial" : func (partialName string ) (template.HTML , error ) {
334
336
fullPartialName := fmt .Sprintf ("%s-%s" , partialName , name )
335
- if r . TemplateLookup (fullPartialName ) == nil && r .opt .RenderPartialsWithoutPrefix {
337
+ if templates . Lookup (fullPartialName ) == nil && r .opt .RenderPartialsWithoutPrefix {
336
338
fullPartialName = partialName
337
339
}
338
- if r .opt .RequirePartials || r . TemplateLookup (fullPartialName ) != nil {
339
- buf , err := r .execute (fullPartialName , binding )
340
+ if r .opt .RequirePartials || templates . Lookup (fullPartialName ) != nil {
341
+ buf , err := r .execute (templates , fullPartialName , binding )
340
342
// Return safe HTML here since we are rendering our own template.
341
343
return template .HTML (buf .String ()), err
342
344
}
@@ -402,14 +404,14 @@ func (r *Render) HTML(w io.Writer, status int, name string, binding interface{},
402
404
if r .opt .IsDevelopment {
403
405
r .compileTemplates ()
404
406
}
405
-
406
- r . templatesLk . RLock ()
407
- defer r . templatesLk .RUnlock ()
407
+ r . lock . RLock ()
408
+ templates := r . templates
409
+ r . lock .RUnlock ()
408
410
409
411
opt := r .prepareHTMLOptions (htmlOpt )
410
- if tpl := r . templates .Lookup (name ); tpl != nil {
412
+ if tpl := templates .Lookup (name ); tpl != nil {
411
413
if len (opt .Layout ) > 0 {
412
- tpl .Funcs (r .layoutFuncs (name , binding ))
414
+ tpl .Funcs (r .layoutFuncs (templates , name , binding ))
413
415
name = opt .Layout
414
416
}
415
417
@@ -426,7 +428,7 @@ func (r *Render) HTML(w io.Writer, status int, name string, binding interface{},
426
428
h := HTML {
427
429
Head : head ,
428
430
Name : name ,
429
- Templates : r . templates ,
431
+ Templates : templates ,
430
432
bp : r .opt .BufferPool ,
431
433
}
432
434
0 commit comments