RenderThing test cases

Provide unit test support RenderThing.

One observation is that creating the site.Tmpl variable is a one time
event.  site.Tmpl doesn't like additional templates with the same name.
This means that updating a template while in --watch mode requires
throwing away the entire Site object and creating a new one.  Not that
this is a bad idea, but it is something I discovered while working on
these unit tests.
This commit is contained in:
Noah Campbell
2013-08-23 14:47:03 -07:00
parent 9d15262ee5
commit f28a8fa0c2
2 changed files with 127 additions and 7 deletions

View File

@@ -169,9 +169,7 @@ func (s *Site) loadTemplates() {
if err != nil {
return err
}
text := string(filetext)
t := s.Tmpl.New(s.generateTemplateNameFrom(path))
template.Must(t.Parse(text))
s.addTemplate(s.generateTemplateNameFrom(path), string(filetext))
}
return nil
}
@@ -179,6 +177,11 @@ func (s *Site) loadTemplates() {
filepath.Walk(s.absLayoutDir(), walker)
}
func (s *Site) addTemplate(name, tmpl string) (err error) {
_, err = s.Tmpl.New(name).Parse(tmpl)
return
}
func (s *Site) generateTemplateNameFrom(path string) (name string) {
name = filepath.ToSlash(path[len(s.absLayoutDir())+1:])
return
@@ -188,11 +191,9 @@ func (s *Site) primeTemplates() {
alias := "<!DOCTYPE html>\n <html>\n <head>\n <link rel=\"canonical\" href=\"{{ .Permalink }}\"/>\n <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n <meta http-equiv=\"refresh\" content=\"0;url={{ .Permalink }}\" />\n </head>\n </html>"
alias_xhtml := "<!DOCTYPE html>\n <html xmlns=\"http://www.w3.org/1999/xhtml\">\n <head>\n <link rel=\"canonical\" href=\"{{ .Permalink }}\"/>\n <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n <meta http-equiv=\"refresh\" content=\"0;url={{ .Permalink }}\" />\n </head>\n </html>"
t := s.Tmpl.New("alias")
template.Must(t.Parse(alias))
s.addTemplate("alias", alias)
s.addTemplate("alias-xhtml", alias_xhtml)
t = s.Tmpl.New("alias-xhtml")
template.Must(t.Parse(alias_xhtml))
}
func (s *Site) initialize() {
@@ -616,6 +617,9 @@ func (s *Site) NewNode() Node {
}
func (s *Site) RenderThing(d interface{}, layout string) (*bytes.Buffer, error) {
if s.Tmpl.Lookup(layout) == nil {
return nil, errors.New("Layout not found")
}
buffer := new(bytes.Buffer)
err := s.Tmpl.ExecuteTemplate(buffer, layout, d)
return buffer, err