mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-17 21:01:26 +02:00
Allow for return partials with falsy arguments (#9298)
Partials with returns values are parsed, then inserted into a partial return wrapper via wrapInPartialReturnWrapper in order to assign the return value via *contextWrapper.Set. The predefined wrapper template for partials inserts a partial's nodes into a "with" template action in order to set dot to a *contextWrapper within the partial. However, because "with" is skipped if its argument is falsy, partials with falsy arguments were not being evaluated. This replaces the "with" action in the partial wrapper with a "range" action that isn't skipped if .Arg is falsy. Fixes #7528
This commit is contained in:
@@ -456,22 +456,34 @@ complex: 80: 80
|
||||
`,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
c.Run("Zero argument", func(c *qt.C) {
|
||||
b := newBuilder(c)
|
||||
// Issue 7528
|
||||
func TestPartialWithZeroedArgs(t *testing.T) {
|
||||
|
||||
b.WithTemplatesAdded(
|
||||
"index.html", `
|
||||
Test Partials With Return Values:
|
||||
b := newTestSitesBuilder(t)
|
||||
b.WithTemplatesAdded("index.html",
|
||||
`
|
||||
X{{ partial "retval" dict }}X
|
||||
X{{ partial "retval" slice }}X
|
||||
X{{ partial "retval" "" }}X
|
||||
X{{ partial "retval" false }}X
|
||||
X{{ partial "retval" 0 }}X
|
||||
{{ define "partials/retval" }}
|
||||
{{ return 123 }}
|
||||
{{ end }}`)
|
||||
|
||||
add42: fail: {{ partial "add42.tpl" 0 }}
|
||||
b.WithContentAdded("p.md", ``)
|
||||
b.Build(BuildCfg{})
|
||||
b.AssertFileContent("public/index.html",
|
||||
`
|
||||
X123X
|
||||
X123X
|
||||
X123X
|
||||
X123X
|
||||
X123X
|
||||
`)
|
||||
|
||||
`,
|
||||
)
|
||||
|
||||
e := b.CreateSites().BuildE(BuildCfg{})
|
||||
b.Assert(e, qt.Not(qt.IsNil))
|
||||
})
|
||||
}
|
||||
|
||||
func TestPartialCached(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user