diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-03-09 14:05:31 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-03-09 14:37:58 +0100 |
commit | 973393c99e3e6927e1c8ef27b0cd8f22eae3f4e4 (patch) | |
tree | 4e2635062426b690c46f8cbf42c164df6154e757 /tpl/template.go | |
parent | 9896cd0030da75bf6acab14dad0a7f78472ceff9 (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 | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/tpl/template.go b/tpl/template.go index 2793e4dd5..b8405d580 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -37,8 +37,10 @@ type Template interface { Lookup(name string) *template.Template Templates() []*template.Template New(name string) *template.Template + GetClone() *template.Template LoadTemplates(absPath string) LoadTemplatesWithPrefix(absPath, prefix string) + MarkReady() AddTemplate(name, tpl string) error AddAceTemplate(name, basePath, innerPath string, baseContent, innerContent []byte) error AddInternalTemplate(prefix, name, tpl string) error @@ -53,6 +55,7 @@ type templateErr struct { type GoHTMLTemplate struct { template.Template + clone *template.Template errors []*templateErr } @@ -109,12 +112,12 @@ func executeTemplate(context interface{}, w io.Writer, layouts ...string) { name := layout - if localTemplates.Lookup(name) == nil { + if Lookup(name) == nil { name = layout + ".html" } - if localTemplates.Lookup(name) != nil { - err := localTemplates.ExecuteTemplate(w, name, context) + if templ := Lookup(name); templ != nil { + err := templ.Execute(w, context) if err != nil { jww.ERROR.Println(err, "in", name) } @@ -135,11 +138,49 @@ func ExecuteTemplateToHTML(context interface{}, layouts ...string) template.HTML return template.HTML(b.String()) } +func Lookup(name string) *template.Template { + return (tmpl.(*GoHTMLTemplate)).Lookup(name) +} + +func (t *GoHTMLTemplate) Lookup(name string) *template.Template { + + templ := localTemplates.Lookup(name) + + if templ != nil { + return templ + } + + if t.clone != nil { + return t.clone.Lookup(name) + } + + return nil + +} + +func (t *GoHTMLTemplate) GetClone() *template.Template { + return t.clone +} + func (t *GoHTMLTemplate) LoadEmbedded() { t.EmbedShortcodes() t.EmbedTemplates() } +// MarkReady marks the template as "ready for execution". No changes allowed +// after this is set. +func (t *GoHTMLTemplate) MarkReady() { + if t.clone == nil { + t.clone = template.Must(t.Template.Clone()) + } +} + +func (t *GoHTMLTemplate) checkState() { + if t.clone != nil { + panic("template is cloned and cannot be modfified") + } +} + func (t *GoHTMLTemplate) AddInternalTemplate(prefix, name, tpl string) error { if prefix != "" { return t.AddTemplate("_internal/"+prefix+"/"+name, tpl) @@ -153,6 +194,7 @@ func (t *GoHTMLTemplate) AddInternalShortcode(name, content string) error { } func (t *GoHTMLTemplate) AddTemplate(name, tpl string) error { + t.checkState() _, err := t.New(name).Parse(tpl) if err != nil { t.errors = append(t.errors, &templateErr{name: name, err: err}) @@ -161,6 +203,7 @@ func (t *GoHTMLTemplate) AddTemplate(name, tpl string) error { } func (t *GoHTMLTemplate) AddAceTemplate(name, basePath, innerPath string, baseContent, innerContent []byte) error { + t.checkState() var base, inner *ace.File name = name[:len(name)-len(filepath.Ext(innerPath))] + ".html" @@ -188,6 +231,7 @@ func (t *GoHTMLTemplate) AddAceTemplate(name, basePath, innerPath string, baseCo } func (t *GoHTMLTemplate) AddTemplateFile(name, baseTemplatePath, path string) error { + t.checkState() // get the suffix and switch on that ext := filepath.Ext(path) switch ext { |