Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Gottschling <paul.gottschling@gmail.com>2021-12-17 10:35:21 +0300
committerGitHub <noreply@github.com>2021-12-17 10:35:21 +0300
commit5758c370eac6c4460cd6bb34d4475c8d347585f6 (patch)
tree8a9f337cf38ed842a1d011f46d43f0e58768fd5a /tpl/tplimpl
parent8ee6de6d96a64395d27416d4f2ad7d172a2686d0 (diff)
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
Diffstat (limited to 'tpl/tplimpl')
-rw-r--r--tpl/tplimpl/template_ast_transformers.go14
1 files changed, 10 insertions, 4 deletions
diff --git a/tpl/tplimpl/template_ast_transformers.go b/tpl/tplimpl/template_ast_transformers.go
index de9b5424f..33461dc7d 100644
--- a/tpl/tplimpl/template_ast_transformers.go
+++ b/tpl/tplimpl/template_ast_transformers.go
@@ -112,7 +112,11 @@ func getParseTree(templ tpl.Template) *parse.Tree {
}
const (
- partialReturnWrapperTempl = `{{ $_hugo_dot := $ }}{{ $ := .Arg }}{{ with .Arg }}{{ $_hugo_dot.Set ("PLACEHOLDER") }}{{ end }}`
+ // We parse this template and modify the nodes in order to assign
+ // the return value of a partial to a contextWrapper via Set. We use
+ // "range" over a one-element slice so we can shift dot to the
+ // partial's argument, Arg, while allowing Arg to be falsy.
+ partialReturnWrapperTempl = `{{ $_hugo_dot := $ }}{{ $ := .Arg }}{{ range (slice .Arg) }}{{ $_hugo_dot.Set ("PLACEHOLDER") }}{{ end }}`
)
var partialReturnWrapper *parse.ListNode
@@ -125,16 +129,18 @@ func init() {
partialReturnWrapper = templ.Tree.Root
}
+// wrapInPartialReturnWrapper copies and modifies the parsed nodes of a
+// predefined partial return wrapper to insert those of a user-defined partial.
func (c *templateContext) wrapInPartialReturnWrapper(n *parse.ListNode) *parse.ListNode {
wrapper := partialReturnWrapper.CopyList()
- withNode := wrapper.Nodes[2].(*parse.WithNode)
- retn := withNode.List.Nodes[0]
+ rangeNode := wrapper.Nodes[2].(*parse.RangeNode)
+ retn := rangeNode.List.Nodes[0]
setCmd := retn.(*parse.ActionNode).Pipe.Cmds[0]
setPipe := setCmd.Args[1].(*parse.PipeNode)
// Replace PLACEHOLDER with the real return value.
// Note that this is a PipeNode, so it will be wrapped in parens.
setPipe.Cmds = []*parse.CommandNode{c.returnNode}
- withNode.List.Nodes = append(n.Nodes, retn)
+ rangeNode.List.Nodes = append(n.Nodes, retn)
return wrapper
}