diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-08-19 09:15:14 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-08-19 10:01:16 +0200 |
commit | ae63c2b5c94f68fbabd5dbd821630e747e8959a4 (patch) | |
tree | 7ed3f4ca9adb28841b62872c40339a177cd05a5f /tpl | |
parent | e627449c0a2f1d2ffac29357c4f1832fc5462870 (diff) |
Fail on partials with return when given none or a zero argument
We need to make a proper fix for this, but it is better with an error than just silently continue.
Fixes #7572
Updates #7528
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/partials/partials.go | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/tpl/partials/partials.go b/tpl/partials/partials.go index e03bf471f..648a93686 100644 --- a/tpl/partials/partials.go +++ b/tpl/partials/partials.go @@ -25,6 +25,7 @@ import ( "strings" "sync" + "github.com/gohugoio/hugo/common/hreflect" texttemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate" "github.com/gohugoio/hugo/helpers" @@ -93,14 +94,10 @@ func (c *contextWrapper) Set(in interface{}) string { // Else, the rendered output will be returned: // A string if the partial is a text/template, or template.HTML when html/template. func (ns *Namespace) Include(name string, contextList ...interface{}) (interface{}, error) { - if strings.HasPrefix(name, "partials/") { - name = name[8:] - } - var context interface{} + name = strings.TrimPrefix(name, "partials/") - if len(contextList) == 0 { - context = nil - } else { + var context interface{} + if len(contextList) > 0 { context = contextList[0] } @@ -124,6 +121,10 @@ func (ns *Namespace) Include(name string, contextList ...interface{}) (interface var w io.Writer if info.HasReturn { + if !hreflect.IsTruthful(context) { + // TODO(bep) we need to fix this, but it is non-trivial. + return nil, errors.New("partials that returns a value needs a non-zero argument.") + } // Wrap the context sent to the template to capture the return value. // Note that the template is rewritten to make sure that the dot (".") // and the $ variable points to Arg. |