Repository navigation
Closed
Closed
Copy link
Description
Currently, when you invoke .Page.Render and pass it the name of a non-existent template, the method returns an empty string without warning or error. This isn't anything new; Hugo has behaved this way since 0.54.0, and probably earlier.
There may be a good reason for the existing behavior, but it was surprising to me.
Relevant code:
hugo/hugolib/page__per_output.go
Lines 107 to 126 in d15baf5
| func (pco *pageContentOutput) Render(ctx context.Context, layout ...string) (template.HTML, error) { | |
| if len(layout) == 0 { | |
| return "", errors.New("no layout given") | |
| } | |
| templ, found, err := pco.po.p.resolveTemplate(layout...) | |
| if err != nil { | |
| return "", pco.po.p.wrapError(err) | |
| } | |
| if !found { | |
| return "", nil | |
| } | |
| // Make sure to send the *pageState and not the *pageContentOutput to the template. | |
| res, err := executeToString(ctx, pco.po.p.s.GetTemplateStore(), templ, pco.po.p) | |
| if err != nil { | |
| return "", pco.po.p.wrapError(fmt.Errorf("failed to execute template %s: %w", templ.Name(), err)) | |
| } | |
| return template.HTML(res), nil | |
| } |
Integration test:
// See issue 15052.
func TestRenderViewNotFound(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
-- layouts/home.html --
{{ .Render "view_foo" }}
{{ .Render "view_bar" }}
-- layouts/view_foo.html --
foo
`
b, err := hugolib.TestE(t, files)
b.Assert(err, qt.IsNotNil) // fails (no error is thrown)
}
Activity
hugolib: Return error from .Render when template not found
hugolib: Return error from .Render when template not found
chore(deps): update dependency gohugoio/hugo to v0.164.0 (#9)
github-actions commentedon Sep 2, 2026
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.