Backtestshugorun-20261013-011256#15077

Issue #15077 Merged fix PR #15225 Run 13 Oct 2026 · run-20261013-011256 gohugoio/hugo

Make .Render take additional data

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Both patches make the same core change. Each widens `Render` to `...any`, updates every implementer (pageContentOutput, LazyContentProvider, nopPage, testPage), and passes the optional data as the template context instead of the Page. Template resolution stays keyed to the page's content type in both.

Correctness: the issue writes the signature as `PAGE.Render [DATA] NAME`, with data first. Patch A accepts both `NAME DATA` and `DATA NAME` through `parseRenderArgs`. It tells them apart by which argument is a string, and if both are strings it treats the first as the name. Patch B accepts only `NAME [CONTEXT]`. Calling it in the order the issue shows, e.g. `.Render (dict ...) "lid"`, makes B try to cast a map to a string and fail. Patch A therefore covers the scenario the issue describes, and Patch B misses half of it.

Edge handling: Patch A rejects non-string layout names such as `42`, rejects an empty name, and limits the call to two arguments, each with a clear error. Patch B checks the argument count but uses `cast.ToStringE`, so a numeric name is silently accepted as "42". When the data argument is nil, Patch A falls back to the Page. That choice is debatable but defensible, and it is tested.

Tests: Patch A tests both argument orders and confirms that a content-type-specific layout (`books/lid.html`) is still chosen when data is passed. It also covers string data, nil data, and five error cases. Patch B's tests are thinner: one data case using a dict that imitates a Title field, and two error cases. Both sets of tests would fail without their fix.

Scope/craft: Patch A also updates the user docs to describe the new argument, which is reasonable because the feature is user-facing. Its code keeps parsing separate from execution and is cleaner for that. Patch B is shorter, but it renames the concept to "view" in its error messages, which doesn't match the existing "layout" wording.

Overall, Patch A matches the issue's stated syntax, handles more edge cases, and is better tested.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

58m 29s

Wall-clock time in the container

Full test suite

No new failures

2,152 passed · 93 skipped · 2,253 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

182vs 90 merged

Changed lines, ours vs merged

See https://gohugo.io/methods/page/render/#article
See #15076

PAGE.Render NAME

This has been discussed before.

  • The contract has always been that the templates that .Renders gets the data context as e.g. page.html, a page.Page.
  • We can only pass 1 argument to Go's template renderer, which I guess is the blocking reason for not having done this sooner.

So, if we want to do:

PAGE.Render [DATA] NAME

I'm guessing we have 2 top-level options:

  • We say that if DATA is set, we use that as the context passed to the templates,. else PAGE.
  • We somehow (e.g. by embeding it in a { DataAdditionalContext, Page } struct or putting it into the current context.Context) wrap the Page so you can do both .RelPermalink and .DataAdditionalContext (naming of the latter is just picked out of a hat without thinking too hard.

The two patches

Our patch+168 −14
docs/content/en/methods/page/Render.mdindex 044121743..af14a68b2 100644@@ -6,7 +6,7 @@ keywords: [] params:   functions_and_methods:     returnType: template.HTML-    signatures: [PAGE.Render NAME]+    signatures: [PAGE.Render NAME [DATA]] aliases: [/functions/render] --- @@ -21,11 +21,30 @@ Typically used when ranging over a page collection, the `Render` method on a `Pa  In the example above, note that the template ("summary") is identified by its file name without directory or extension. +## Passing data++To pass additional data to the template, provide it as the second argument. When you pass data, it replaces the `Page` object as the template's context, so include the page in the data if the template needs it:++```go-html-template+{{ range site.RegularPages }}+  {{ .Render "summary" (dict "page" . "showDate" true) }}+{{ end }}+```++Then in the "summary" template:++```go-html-template+<h2><a href="{{ .page.RelPermalink }}">{{ .page.LinkTitle }}</a></h2>+{{ if .showDate }}{{ .page.Date.Format "2006-01-02" }}{{ end }}+```++The template is still selected based on the page's [content type](g), regardless of the data passed. The arguments may also be given in the order `DATA NAME`.+ Although similar to the [`partial`][] function, there are key differences.  `Render` method|`partial` function :--|:---The `Page` object is automatically passed to the given template. You cannot pass additional context.|You must specify the context, allowing you to pass a combination of objects, slices, maps, and scalars.+The `Page` object is automatically passed to the given template, unless you pass data as the second argument.|You must specify the context, allowing you to pass a combination of objects, slices, maps, and scalars. The path to the template is determined by the [content type](g).|You must specify the path to the template, relative to the `layouts/_partials` directory.  Consider this layout structure:hugolib/page__per_output.goindex e8dd38ce7..8c5b61e61 100644@@ -18,6 +18,7 @@ import ( 	"errors" 	"fmt" 	"html/template"+	"reflect" 	"sync" 	"sync/atomic" @@ -104,27 +105,76 @@ func (pco *pageContentOutput) Reset() { 	pco.renderHooks = &renderHooks{} } -func (pco *pageContentOutput) Render(ctx context.Context, layout ...string) (template.HTML, error) {-	if len(layout) == 0 {-		return "", errors.New("no layout given")+func (pco *pageContentOutput) Render(ctx context.Context, args ...any) (template.HTML, error) {+	layout, data, err := parseRenderArgs(args)+	if err != nil {+		return "", err 	}-	templ, found, err := pco.po.p.resolveTemplate(layout...)++	templ, found, err := pco.po.p.resolveTemplate(layout) 	if err != nil { 		return "", pco.po.p.wrapError(err) 	}  	if !found {-		return "", fmt.Errorf("template %q not found", layout[0])+		return "", fmt.Errorf("template %q not found", layout)+	}++	if data == nil {+		// Make sure to send the *pageState and not the *pageContentOutput to the template.+		data = pco.po.p 	} -	// 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)+	res, err := executeToString(ctx, pco.po.p.s.GetTemplateStore(), templ, data) 	if err != nil { 		return "", pco.po.p.wrapError(fmt.Errorf("failed to execute template %s: %w", templ.Name(), err)) 	} 	return template.HTML(res), nil } +// parseRenderArgs parses the arguments to Page.Render, which takes the form+// NAME [DATA] or DATA NAME.+// The returned data is nil if not provided, meaning the Page should be used.+func parseRenderArgs(args []any) (string, any, error) {+	isString := func(v any) bool {+		return v != nil && reflect.TypeOf(v).Kind() == reflect.String+	}++	var (+		layout any+		data   any+	)++	switch len(args) {+	case 0:+		return "", nil, errors.New("no layout given")+	case 1:+		layout = args[0]+	case 2:+		if !isString(args[0]) && isString(args[1]) {+			data, layout = args[0], args[1]+		} else {+			layout, data = args[0], args[1]+		}+	default:+		return "", nil, fmt.Errorf("too many arguments to Render: expected NAME and optional DATA, got %d arguments", len(args))+	}++	if !isString(layout) {+		return "", nil, fmt.Errorf("layout name must be a string, got %T", layout)+	}++	s, err := cast.ToStringE(layout)+	if err != nil {+		return "", nil, fmt.Errorf("invalid layout name: %w", err)+	}+	if s == "" {+		return "", nil, errors.New("no layout given")+	}++	return s, data, nil+}+ func (pco *pageContentOutput) Fragments(ctx context.Context) *tableofcontents.Fragments { 	return pco.c().Fragments(ctx) }hugolib/page_test.goindex 635358590..6c4c6f08a 100644@@ -2458,3 +2458,85 @@ isCJKLanguage: true 	b.AssertFileContent("public/p500/index.html", "500|500|1") 	b.AssertFileContent("public/p501/index.html", "501|600|2") }++// See issue 15077.+func TestRenderWithData(t *testing.T) {+	t.Parallel()++	files := `+-- hugo.toml --+disableKinds = ['home','rss','section','sitemap','taxonomy','term']+-- content/p1.md --+---+title: p1+---+-- content/books/b1.md --+---+title: b1+---+-- layouts/page.html --+NoData: {{ .Render "li" }}|+NameData: {{ .Render "lid" (dict "page" . "foo" "bar") }}|+DataName: {{ .Render (dict "page" . "foo" "baz") "lid" }}|+StringData: {{ .Render "str" "hello" }}|+NilData: {{ .Render "li" .Params.doesnotexist }}|+-- layouts/li.html --+default page: {{ .Title }}+-- layouts/books/li.html --+books page: {{ .Title }}+-- layouts/lid.html --+default: {{ .page.Title }}|{{ .foo }}+-- layouts/books/lid.html --+books: {{ .page.Title }}|{{ .foo }}+-- layouts/str.html --+str: {{ . }}+`++	b := Test(t, files)++	b.AssertFileContent("public/p1/index.html",+		"NoData: default page: p1|",+		"NameData: default: p1|bar|",+		"DataName: default: p1|baz|",+		"StringData: str: hello|",+		"NilData: default page: p1|",+	)+	b.AssertFileContent("public/books/b1/index.html",+		"NoData: books page: b1|",+		"NameData: books: b1|bar|",+		"DataName: books: b1|baz|",+	)+}++func TestRenderWithDataErrors(t *testing.T) {+	t.Parallel()++	filesTemplate := `+-- hugo.toml --+disableKinds = ['home','rss','section','sitemap','taxonomy','term']+-- content/p1.md --+---+title: p1+---+-- layouts/page.html --+RENDER+-- layouts/li.html --+li+`++	for _, test := range []struct {+		render string+		expect string+	}{+		{`{{ .Render "li" "a" "b" }}`, "too many arguments to Render"},+		{`{{ .Render (dict "a" 1) (dict "b" 2) }}`, "layout name must be a string"},+		{`{{ .Render 42 }}`, "layout name must be a string"},+		{`{{ .Render "" }}`, "no layout given"},+		{`{{ .Render "missing" (dict "a" 1) }}`, `template "missing" not found`},+	} {+		files := strings.ReplaceAll(filesTemplate, "RENDER", test.render)+		b, err := TestE(t, files)+		b.Assert(err, qt.IsNotNil, qt.Commentf(test.render))+		b.Assert(err.Error(), qt.Contains, test.expect, qt.Commentf(test.render))+	}+}resources/page/page.goindex 6104eb0ed..e42cea4df 100644@@ -328,7 +328,10 @@ type PageMetaInternalProvider interface { // PageRenderProvider provides a way for a Page to render content. type PageRenderProvider interface { 	// Render renders the given layout with this Page as context.-	Render(ctx context.Context, layout ...string) (template.HTML, error)+	// The first argument is the layout name.+	// An optional second argument is the data to pass to the template as its context,+	// replacing the Page. The arguments may also be given in the order DATA, NAME.+	Render(ctx context.Context, args ...any) (template.HTML, error) 	// RenderString renders the first value in args with the content renderer defined 	// for this Page. 	// It takes an optional map as a second argument:resources/page/page_lazy_contentprovider.goindex 84fb24682..2b0516ac5 100644@@ -117,8 +117,8 @@ func (lcp *LazyContentProvider) Len(ctx context.Context) int { 	return lcp.init.Value(ctx).Len(ctx) } -func (lcp *LazyContentProvider) Render(ctx context.Context, layout ...string) (template.HTML, error) {-	return lcp.init.Value(ctx).Render(ctx, layout...)+func (lcp *LazyContentProvider) Render(ctx context.Context, args ...any) (template.HTML, error) {+	return lcp.init.Value(ctx).Render(ctx, args...) }  func (lcp *LazyContentProvider) RenderString(ctx context.Context, args ...any) (template.HTML, error) {resources/page/page_nop.goindex 7dbc5f615..1e92f0f82 100644@@ -399,7 +399,7 @@ func (p *nopPage) RelRef(argsm map[string]any) (string, error) { 	return "", nil } -func (p *nopPage) Render(ctx context.Context, layout ...string) (template.HTML, error) {+func (p *nopPage) Render(ctx context.Context, args ...any) (template.HTML, error) { 	return "", nil } resources/page/testhelpers_test.goindex fdaee35c1..2222aca7b 100644@@ -483,7 +483,7 @@ func (p *testPage) RelRefFrom(argsm map[string]any, source any) (string, error) 	return "", nil } -func (p *testPage) Render(ctx context.Context, layout ...string) (template.HTML, error) {+func (p *testPage) Render(ctx context.Context, args ...any) (template.HTML, error) { 	panic("testpage: not implemented") }  
Merged fix · PR #15225+77 −13
hugolib/page__per_output.goindex e8dd38ce7..05bb49132 100644@@ -104,21 +104,34 @@ func (pco *pageContentOutput) Reset() { 	pco.renderHooks = &renderHooks{} } -func (pco *pageContentOutput) Render(ctx context.Context, layout ...string) (template.HTML, error) {-	if len(layout) == 0 {-		return "", errors.New("no layout given")+func (pco *pageContentOutput) Render(ctx context.Context, args ...any) (template.HTML, error) {+	if len(args) == 0 {+		return "", errors.New("no view given") 	}-	templ, found, err := pco.po.p.resolveTemplate(layout...)+	if len(args) > 2 {+		return "", errors.New("too many arguments, expected VIEW [CONTEXT]")+	}+	view, err := cast.ToStringE(args[0])+	if err != nil {+		return "", fmt.Errorf("failed to convert view argument to string: %w", err)+	}++	// Make sure to send the *pageState and not the *pageContentOutput to the template.+	var data any = pco.po.p+	if len(args) == 2 {+		data = args[1]+	}++	templ, found, err := pco.po.p.resolveTemplate(view) 	if err != nil { 		return "", pco.po.p.wrapError(err) 	}  	if !found {-		return "", fmt.Errorf("template %q not found", layout[0])+		return "", fmt.Errorf("template %q not found", view) 	} -	// 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)+	res, err := executeToString(ctx, pco.po.p.s.GetTemplateStore(), templ, data) 	if err != nil { 		return "", pco.po.p.wrapError(fmt.Errorf("failed to execute template %s: %w", templ.Name(), err)) 	}hugolib/page_test.goindex 635358590..57bcfbe94 100644@@ -1913,6 +1913,56 @@ func TestRenderWithoutArgument(t *testing.T) { 	b.Assert(err, qt.IsNotNil) } +// See issue 15077.+func TestRenderWithContext(t *testing.T) {+	t.Parallel()++	files := `+-- hugo.toml --+-- content/p1.md --+---+title: "P1"+---+-- layouts/page.html --+{{ .Render "li" }}|{{ .Render "li" (dict "Title" "Custom") }}+-- layouts/li.html --+Title: {{ .Title }}{{- /**/ -}}+`++	b := Test(t, files)++	b.AssertFileContent("public/p1/index.html", "Title: P1|Title: Custom")+}++// See issue 15077.+func TestRenderWithContextErrors(t *testing.T) {+	t.Parallel()++	filesTemplate := `+-- hugo.toml --+-- content/p1.md --+---+title: "P1"+---+-- layouts/li.html --+li+-- layouts/page.html --+RENDER+`++	for _, test := range []struct {+		render  string+		message string+	}{+		{`{{ .Render "li" "foo" "bar" }}`, `(?s).*too many arguments, expected VIEW \[CONTEXT\].*`},+		{`{{ .Render (slice "li") }}`, `(?s).*failed to convert view argument to string: unable to cast \[\]string{"li"} of type \[\]string to string.*`},+	} {+		files := strings.ReplaceAll(filesTemplate, "RENDER", test.render)+		b, err := TestE(t, files)+		b.Assert(err, qt.ErrorMatches, test.message)+	}+}+ // Issue #13021 func TestAllStores(t *testing.T) { 	t.Parallel()resources/page/page.goindex 6104eb0ed..e0677e1ed 100644@@ -327,8 +327,9 @@ type PageMetaInternalProvider interface {  // PageRenderProvider provides a way for a Page to render content. type PageRenderProvider interface {-	// Render renders the given layout with this Page as context.-	Render(ctx context.Context, layout ...string) (template.HTML, error)+	// Render renders the given view (a layout template) with this Page as+	// the data context, or, if given, the second argument CONTEXT.+	Render(ctx context.Context, args ...any) (template.HTML, error) 	// RenderString renders the first value in args with the content renderer defined 	// for this Page. 	// It takes an optional map as a second argument:resources/page/page_lazy_contentprovider.goindex 84fb24682..2b0516ac5 100644@@ -117,8 +117,8 @@ func (lcp *LazyContentProvider) Len(ctx context.Context) int { 	return lcp.init.Value(ctx).Len(ctx) } -func (lcp *LazyContentProvider) Render(ctx context.Context, layout ...string) (template.HTML, error) {-	return lcp.init.Value(ctx).Render(ctx, layout...)+func (lcp *LazyContentProvider) Render(ctx context.Context, args ...any) (template.HTML, error) {+	return lcp.init.Value(ctx).Render(ctx, args...) }  func (lcp *LazyContentProvider) RenderString(ctx context.Context, args ...any) (template.HTML, error) {resources/page/page_nop.goindex 7dbc5f615..1e92f0f82 100644@@ -399,7 +399,7 @@ func (p *nopPage) RelRef(argsm map[string]any) (string, error) { 	return "", nil } -func (p *nopPage) Render(ctx context.Context, layout ...string) (template.HTML, error) {+func (p *nopPage) Render(ctx context.Context, args ...any) (template.HTML, error) { 	return "", nil } resources/page/testhelpers_test.goindex fdaee35c1..2222aca7b 100644@@ -483,7 +483,7 @@ func (p *testPage) RelRefFrom(argsm map[string]any, source any) (string, error) 	return "", nil } -func (p *testPage) Render(ctx context.Context, layout ...string) (template.HTML, error) {+func (p *testPage) Render(ctx context.Context, args ...any) (template.HTML, error) { 	panic("testpage: not implemented") }