Backtestshugorun-20261005-133023#15077

Issue #15077 Merged fix PR # Run 5 Oct 2026 · run-20261005-133023 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 change the `Render` signature to `...any` across all implementers (`pageContentOutput`, `LazyContentProvider`, `nopPage`, `testPage`). Both fall back to the page as context when no data is given, so existing `.Render NAME` calls keep working. The difference is which argument orders they accept. The issue explicitly proposes `PAGE.Render [DATA] NAME`. Patch A supports that order and also accepts `NAME DATA` when DATA is not a string, via a small `ParseRenderArgs` helper. Patch B supports only `NAME DATA` ("VIEW [CONTEXT]"), which is the reverse of the signature in the issue. A user following the issue's form, e.g. `.Render (dict ...) "li"`, would get a cast error under B. Patch A has one ambiguity: with two strings it treats the first as DATA. That matches the issue's stated order but differs from B's reading, and the rule is documented. On tests, A is stronger. Its integration tests cover no data, DATA NAME, NAME DATA and string data. They also check that template lookup still follows the page's content type (`books/li.html` vs `li.html`) when data is passed, and that the page can be reached through the dict. A also adds unit tests for every parsing branch, including the error cases. B's test covers only NAME DATA with a dict that mimics `.Title`, plus two error cases. A also updates the user docs for the new signature and the comparison table with `partial`. B's change is smaller and clean, but it implements the opposite of the order the issue proposes. Overall A is more correct with respect to the issue and 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

1h 42m

Wall-clock time in the container

Full test suite

No new failures

3,732 passed · 99 skipped · 3,833 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

239vs 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+223 −16
docs/content/en/methods/page/Render.mdindex 044121743..9153a712e 100644@@ -1,12 +1,12 @@ --- title: Render-description: Renders the given template with the given page as context.+description: Renders the given template with the given page, or the given data, as context. categories: [] keywords: [] params:   functions_and_methods:     returnType: template.HTML-    signatures: [PAGE.Render NAME]+    signatures: [PAGE.Render NAME, PAGE.Render DATA NAME] aliases: [/functions/render] --- @@ -21,11 +21,29 @@ 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++If you pass DATA, it is used as the context passed to the template instead of the `Page` object. The template is still resolved from the given page's content type. Include the page in DATA if the template needs it:++```go-html-template+{{ range site.RegularPages }}+  {{ .Render (dict "page" . "class" "featured") "summary" }}+{{ end }}+```++And in the `summary` template:++```go-html-template+<article class="{{ .class }}">{{ .page.Title }}</article>+```++The `NAME DATA` order (e.g., `{{ .Render "summary" $data }}`) is also accepted when DATA is not a string. When both arguments are strings, the first is DATA and the second is 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, which is then used 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..3d4ac22cd 100644@@ -15,7 +15,6 @@ package hugolib  import ( 	"context"-	"errors" 	"fmt" 	"html/template" 	"sync"@@ -104,21 +103,26 @@ 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 := page.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)) 	}hugolib/page_test.goindex 635358590..852fd289e 100644@@ -2458,3 +2458,65 @@ isCJKLanguage: true 	b.AssertFileContent("public/p500/index.html", "500|500|1") 	b.AssertFileContent("public/p501/index.html", "501|600|2") }++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" }}|+DataName: {{ .Render (dict "page" . "foo" "bar") "li" }}|+NameData: {{ .Render "li" (dict "page" . "foo" "baz") }}|+StringData: {{ .Render "mystring" "li" }}|+-- layouts/li.html --+{{- if reflect.IsMap . }}default map: {{ .page.Title }}/{{ .foo }}{{ else if eq (printf "%T" .) "string" }}default string: {{ . }}{{ else }}default page: {{ .Title }}/{{ eq page . }}{{ end -}}+-- layouts/books/li.html --+{{- if reflect.IsMap . }}books map: {{ .page.Title }}/{{ .foo }}{{ else if eq (printf "%T" .) "string" }}books string: {{ . }}{{ else }}books page: {{ .Title }}{{ end -}}+`++	b := Test(t, files)++	b.AssertFileContent("public/p1/index.html",+		"NoData: default page: p1/true|",+		"DataName: default map: p1/bar|",+		"NameData: default map: p1/baz|",+		"StringData: default string: mystring|",+	)+	b.AssertFileContent("public/books/b1/index.html",+		"NoData: books page: b1|",+		"DataName: books map: b1/bar|",+		"NameData: books map: b1/baz|",+		"StringData: books string: mystring|",+	)+}++func TestRenderWithDataErrors(t *testing.T) {+	t.Parallel()++	files := `+-- hugo.toml --+disableKinds = ['home','rss','section','sitemap','taxonomy','term']+-- content/p1.md --+---+title: p1+---+-- layouts/page.html --+{{ .Render "li" (dict "a" 1) "extra" }}+-- layouts/li.html --+li+`++	b, err := TestE(t, files)+	b.Assert(err, qt.IsNotNil)+	b.Assert(err.Error(), qt.Contains, "too many arguments")+}resources/page/page.goindex 6104eb0ed..17313abaf 100644@@ -328,7 +328,11 @@ 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)+	// It accepts the layout name and an optional data argument, either as+	// NAME, DATA NAME or NAME DATA. If DATA is set, it is used as the+	// context passed to the template instead of this Page.+	// See ParseRenderArgs.+	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/page_render.gonew file mode 100644index 000000000..7d9920da7@@ -0,0 +1,62 @@+// Copyright 2026 The Hugo Authors. All rights reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package page++import (+	"errors"+	"fmt"++	"github.com/spf13/cast"+)++// ParseRenderArgs parses the arguments passed to Page.Render into a layout name+// and an optional data context.+//+// Supported forms are:+//+//	NAME+//	DATA NAME+//	NAME DATA+//+// When both arguments are strings, the first is DATA and the second NAME.+// The returned data is nil if no data argument was given.+func ParseRenderArgs(args ...any) (layout string, data any, err error) {+	var nameArg any+	switch len(args) {+	case 0:+		return "", nil, errors.New("no layout given")+	case 1:+		nameArg = args[0]+	case 2:+		_, firstIsString := args[0].(string)+		_, secondIsString := args[1].(string)+		if firstIsString && !secondIsString {+			nameArg, data = args[0], args[1]+		} else {+			data, nameArg = args[0], args[1]+		}+	default:+		return "", nil, fmt.Errorf("too many arguments: expected NAME or DATA NAME, got %d arguments", len(args))+	}++	layout, err = cast.ToStringE(nameArg)+	if err != nil {+		return "", nil, fmt.Errorf("invalid layout name: %w", err)+	}+	if layout == "" {+		return "", nil, errors.New("no layout given")+	}++	return layout, data, nil+}resources/page/page_render_test.gonew file mode 100644index 000000000..a027b4d5f@@ -0,0 +1,57 @@+// Copyright 2026 The Hugo Authors. All rights reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package page++import (+	"testing"++	qt "github.com/frankban/quicktest"+)++func TestParseRenderArgs(t *testing.T) {+	c := qt.New(t)+	m := map[string]any{"a": 1}++	layout, data, err := ParseRenderArgs("li")+	c.Assert(err, qt.IsNil)+	c.Assert(layout, qt.Equals, "li")+	c.Assert(data, qt.IsNil)++	layout, data, err = ParseRenderArgs(m, "li")+	c.Assert(err, qt.IsNil)+	c.Assert(layout, qt.Equals, "li")+	c.Assert(data, qt.DeepEquals, m)++	layout, data, err = ParseRenderArgs("li", m)+	c.Assert(err, qt.IsNil)+	c.Assert(layout, qt.Equals, "li")+	c.Assert(data, qt.DeepEquals, m)++	layout, data, err = ParseRenderArgs("s", "li")+	c.Assert(err, qt.IsNil)+	c.Assert(layout, qt.Equals, "li")+	c.Assert(data, qt.Equals, "s")++	_, _, err = ParseRenderArgs()+	c.Assert(err, qt.ErrorMatches, "no layout given")++	_, _, err = ParseRenderArgs("")+	c.Assert(err, qt.ErrorMatches, "no layout given")++	_, _, err = ParseRenderArgs(m, "li", "x")+	c.Assert(err, qt.ErrorMatches, "too many arguments.*")++	_, _, err = ParseRenderArgs(m, m)+	c.Assert(err, qt.ErrorMatches, "invalid layout name.*")+}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 #+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") }