diff --git a/hugolib/site.go b/hugolib/site.go index 0695a0be9..870301d41 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -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 := "\n \n \n \n \n \n \n " alias_xhtml := "\n \n \n \n \n \n \n " - 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 diff --git a/hugolib/site_test.go b/hugolib/site_test.go new file mode 100644 index 000000000..eea725c8f --- /dev/null +++ b/hugolib/site_test.go @@ -0,0 +1,116 @@ +package hugolib + +import ( + "fmt" + "strings" + "testing" +) + +var TEMPLATE_TITLE = "{{ .Title }}" +var PAGE_SIMPLE_TITLE = `--- +title: simple template +--- +content` + +var TEMPLATE_MISSING_FUNC = "{{ .Title | funcdoesnotexists }}" +var TEMPLATE_FUNC = "{{ .Title | urlize }}" + +func pageMust(p *Page, err error) *Page { + if err != nil { + panic(err) + } + return p +} + +func TestDegenerateRenderThingMissingTemplate(t *testing.T) { + p, _ := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md") + s := new(Site) + s.prepTemplates() + _, err := s.RenderThing(p, "foobar") + if err == nil { + t.Errorf("Expected err to be returned when missing the template.") + } +} + +func TestPrimeTempaltes(t *testing.T) { + s := new(Site) + s.prepTemplates() + s.primeTemplates() + if s.Tmpl.Lookup("alias") == nil { + t.Fatalf("alias template not created.") + } +} + +func TestAddInvalidTemplate(t *testing.T) { + s := new(Site) + s.prepTemplates() + err := s.addTemplate("missing", TEMPLATE_MISSING_FUNC) + if err == nil { + t.Fatalf("Expecting the template to return an error") + } +} + +func matchRender(t *testing.T, s *Site, p *Page, tmplName string, expected string) { + content, err := s.RenderThing(p, tmplName) + if err != nil { + t.Fatalf("Unable to render template.") + } + + if string(content.Bytes()) != expected { + t.Fatalf("Content did not match expected: %s. got: %s", expected, content) + } +} + +func _TestAddSameTemplateTwice(t *testing.T) { + p := pageMust(ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")) + s := new(Site) + s.prepTemplates() + err := s.addTemplate("foo", TEMPLATE_TITLE) + if err != nil { + t.Fatalf("Unable to add template foo") + } + + matchRender(t, s, p, "foo", "simple template") + + err = s.addTemplate("foo", "NEW {{ .Title }}") + if err != nil { + t.Fatalf("Unable to add template foo: %s", err) + } + + matchRender(t, s, p, "foo", "NEW simple template") +} + +func TestRenderThing(t *testing.T) { + tests := []struct { + content string + template string + expected string + }{ + {PAGE_SIMPLE_TITLE, TEMPLATE_TITLE, "simple template"}, + {PAGE_SIMPLE_TITLE, TEMPLATE_FUNC, "simple-template"}, + } + + s := new(Site) + s.prepTemplates() + + for i, test := range tests { + p, err := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md") + if err != nil { + t.Fatalf("Error parsing buffer: %s", err) + } + templateName := fmt.Sprintf("foobar%d", i) + err = s.addTemplate(templateName, test.template) + if err != nil { + t.Fatalf("Unable to add template") + } + + html, err := s.RenderThing(p, templateName) + if err != nil { + t.Errorf("Unable to render html: %s", err) + } + + if string(html.Bytes()) != test.expected { + t.Errorf("Content does not match. Expected '%s', got '%s'", test.expected, html) + } + } +}