tpl: Allow the partial template func to return any type

This commit adds support for return values in partials.

This means that you can now do this and similar:

    {{ $v := add . 42 }}
    {{ return $v }}

Partials without a `return` statement will be rendered as before.

This works for both `partial` and `partialCached`.

Fixes #5783
This commit is contained in:
Bjørn Erik Pedersen
2019-04-02 10:30:24 +02:00
committed by GitHub
parent 9225db636e
commit a55640de8e
11 changed files with 371 additions and 79 deletions

View File

@@ -264,3 +264,44 @@ Hugo: {{ hugo.Generator }}
)
}
func TestPartialWithReturn(t *testing.T) {
b := newTestSitesBuilder(t).WithSimpleConfigFile()
b.WithTemplatesAdded(
"index.html", `
Test Partials With Return Values:
add42: 50: {{ partial "add42.tpl" 8 }}
dollarContext: 60: {{ partial "dollarContext.tpl" 18 }}
adder: 70: {{ partial "dict.tpl" (dict "adder" 28) }}
complex: 80: {{ partial "complex.tpl" 38 }}
`,
"partials/add42.tpl", `
{{ $v := add . 42 }}
{{ return $v }}
`,
"partials/dollarContext.tpl", `
{{ $v := add $ 42 }}
{{ return $v }}
`,
"partials/dict.tpl", `
{{ $v := add $.adder 42 }}
{{ return $v }}
`,
"partials/complex.tpl", `
{{ return add . 42 }}
`,
)
b.CreateSites().Build(BuildCfg{})
b.AssertFileContent("public/index.html",
"add42: 50: 50",
"dollarContext: 60: 60",
"adder: 70: 70",
"complex: 80: 80",
)
}