Backtestshugorun-20261007-020926#15077

Issue #15077 Merged fix PR #15225 Run 7 Oct 2026 · run-20261007-020926 gohugoio/hugo

Make .Render take additional data

Better

Our patch was judged better than the merged fix.

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

Both patches change the `Render` signature to `args ...any` the same way across the interface, the lazy provider, nopPage and testPage. When data is given, both pass it as the template context and otherwise fall back to the page. That is the issue's first option.

The difference is argument order. The issue writes the proposed form as `PAGE.Render [DATA] NAME`, with data before the name.

- Patch A accepts only `VIEW [CONTEXT]`. A call like `.Render (dict ...) "name"` fails in A with a cast error.
- Patch B accepts NAME, NAME DATA and DATA NAME. It finds the layout name by checking which argument is string-kinded, so it covers the issue's stated form and the alternative order. Its `reflect.Kind` check also accepts string types such as `template.HTML`.
- B has one ambiguity: when both arguments are strings, the first is treated as the name. That edge case would only bite string data in DATA-first order, and the doc comment documents the rule.

Tests:

- B's tests cover both orders, subdirectory views, scalar data, a safeHTML name, and error cases (no string argument, too many arguments, missing template).
- A's tests cover only name-first with a dict, plus two error cases.

B also updates the user docs for the new feature. That adds no risk.

Craft is comparable. B's `parseRenderArgs` helper is clean, and its error messages are informative.

On correctness against the issue's spec and on test breadth, B is the better fix.

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 11s

Wall-clock time in the container

Full test suite

No new failures

3,731 passed · 99 skipped · 3,832 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

168vs 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+154 −14
docs/content/en/methods/page/Render.mdindex 044121743..2e807feb5 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,32 @@ 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++{{< new-in 0.166.0 />}}++To pass additional data to the template, provide it as a second argument. When given, the data is passed to the template as context instead of the page, so include the page in the data if the template needs it:++```go-html-template+{{ range site.RegularPages }}+  {{ .Render "summary" (dict "page" . "headingLevel" 3) }}+{{ end }}+```++Then in the "summary" template:++```go-html-template+<h{{ .headingLevel }}>{{ .page.LinkTitle }}</h{{ .headingLevel }}>+{{ .page.Summary }}+```++The data may also be passed before the template name, as in `{{ .Render $data "summary" }}`.+ 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 context.|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..04b6466e8 100644@@ -18,6 +18,7 @@ import ( 	"errors" 	"fmt" 	"html/template"+	"reflect" 	"sync" 	"sync/atomic" @@ -104,27 +105,73 @@ 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, hasData, 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 !hasData {+		// 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 a layout+// name and optional data to use as the template context, in either order:+//+//	NAME+//	NAME DATA+//	DATA NAME+//+// If both arguments are strings, the first is the layout name.+func parseRenderArgs(args []any) (layout string, data any, hasData bool, err error) {+	switch len(args) {+	case 0:+		return "", nil, false, errors.New("no layout given")+	case 1:+		layout, err = cast.ToStringE(args[0])+		if err != nil {+			return "", nil, false, fmt.Errorf("invalid layout name: %w", err)+		}+		return layout, nil, false, nil+	case 2:+		if s, ok := layoutName(args[0]); ok {+			return s, args[1], true, nil+		}+		if s, ok := layoutName(args[1]); ok {+			return s, args[0], true, nil+		}+		return "", nil, false, fmt.Errorf("no layout name given: expected one of the arguments to be a string, got %T and %T", args[0], args[1])+	default:+		return "", nil, false, fmt.Errorf("too many arguments: expected a layout name and optional data, got %d arguments", len(args))+	}+}++// layoutName returns v as a string if v is a string or a string type+// such as template.HTML.+func layoutName(v any) (string, bool) {+	rv := reflect.ValueOf(v)+	if rv.Kind() != reflect.String {+		return "", false+	}+	return rv.String(), true+}+ func (pco *pageContentOutput) Fragments(ctx context.Context) *tableofcontents.Fragments { 	return pco.c().Fragments(ctx) }hugolib/page_test.goindex 635358590..ab5fa31b2 100644@@ -2458,3 +2458,72 @@ isCJKLanguage: true 	b.AssertFileContent("public/p500/index.html", "500|500|1") 	b.AssertFileContent("public/p501/index.html", "501|600|2") }++// See issue 15077.+func TestRenderViewWithData(t *testing.T) {+	t.Parallel()++	files := `+-- hugo.toml --+disableKinds = ['home','rss','section','sitemap','taxonomy','term']+-- content/p1.md --+---+title: p1+---+-- layouts/page.html --+nodata: {{ .Render "myview" }}|+namedata: {{ .Render "mydataview" (dict "page" . "foo" "bar") }}|+dataname: {{ .Render (dict "page" . "foo" "baz") "mydataview" }}|+subdir: {{ .Render "sub/myview" (dict "page" . "foo" "qux") }}|+scalar: {{ .Render "myscalarview" 42 }}|+safename: {{ .Render (safeHTML "mydataview") (dict "page" . "foo" "safe") }}|+-- layouts/myview.html --+{{- .Title -}}+-- layouts/mydataview.html --+{{- .page.Title }}:{{ .foo -}}+-- layouts/sub/myview.html --+{{- .page.Title }}:{{ .foo -}}+-- layouts/myscalarview.html --+{{- . -}}+`++	b := Test(t, files)+	b.AssertFileContent("public/p1/index.html",+		"nodata: p1|",+		"namedata: p1:bar|",+		"dataname: p1:baz|",+		"subdir: p1:qux|",+		"scalar: 42|",+		"safename: p1:safe|",+	)+}++// See issue 15077.+func TestRenderViewWithDataErrors(t *testing.T) {+	t.Parallel()++	files := `+-- hugo.toml --+disableKinds = ['home','rss','section','sitemap','taxonomy','term']+-- content/p1.md --+---+title: p1+---+-- layouts/page.html --+{{ .Render ARGS }}+-- layouts/myview.html --+{{- .Title -}}+`++	for _, test := range []struct {+		args     string+		errMatch string+	}{+		{`(dict "a" 1) (dict "b" 2)`, `.*no layout name given.*`},+		{`"myview" (dict "a" 1) "extra"`, `.*too many arguments.*`},+		{`"missing" (dict "a" 1)`, `.*template "missing" not found.*`},+	} {+		_, err := TestE(t, strings.ReplaceAll(files, "ARGS", test.args))+		qt.Assert(t, err, qt.ErrorMatches, test.errMatch, qt.Commentf(test.args))+	}+}resources/page/page.goindex 6104eb0ed..aa2f847a6 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 arguments are the layout name and, optionally, data to use as+	// the template context instead of this Page, in either order,+	// e.g. .Render "name" or .Render "name" DATA or .Render 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") }