diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-02-28 13:51:51 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-02-28 13:51:51 +0300 |
commit | 93b04e67f633e96484c29158cad00e86abe64c1e (patch) | |
tree | 70aadae5aaedf70d3bdc7f05da20ce7bd9ab4cdc /tpl/template.go | |
parent | 804603155f775bbf1b03dbeadbb78f978af6c169 (diff) |
Create template clone for late template execution
Fixing some breaking blogs on Go 1.6
Fixes #1879
Diffstat (limited to 'tpl/template.go')
-rw-r--r-- | tpl/template.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/tpl/template.go b/tpl/template.go index c5a23628a..fa8347efd 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -37,12 +37,14 @@ type Template interface { Lookup(name string) *template.Template Templates() []*template.Template New(name string) *template.Template + Clone() *template.Template LoadTemplates(absPath string) LoadTemplatesWithPrefix(absPath, prefix string) AddTemplate(name, tpl string) error AddAceTemplate(name, basePath, innerPath string, baseContent, innerContent []byte) error AddInternalTemplate(prefix, name, tpl string) error AddInternalShortcode(name, tpl string) error + MarkReady() PrintErrors() } @@ -53,6 +55,8 @@ type templateErr struct { type GoHTMLTemplate struct { template.Template + clone *template.Template + ready bool errors []*templateErr } @@ -140,6 +144,22 @@ func (t *GoHTMLTemplate) LoadEmbedded() { t.EmbedTemplates() } +// MarkReady marks the template as "ready for execution". No changes allowed +// after this is set. +func (t *GoHTMLTemplate) MarkReady() { + t.clone = template.Must(t.Template.Clone()) + t.ready = true +} + +// Since Go 1.6, the template cannot change once executed. So we have to create +// a clone and work with that in some rare cases. +func (t *GoHTMLTemplate) Clone() *template.Template { + if !t.ready { + panic("template clone called too early") + } + return template.Must(t.clone.Clone()) +} + func (t *GoHTMLTemplate) AddInternalTemplate(prefix, name, tpl string) error { if prefix != "" { return t.AddTemplate("_internal/"+prefix+"/"+name, tpl) |