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
This commit is contained in:
Bjørn Erik Pedersen
2020-08-19 09:15:14 +02:00
parent e627449c0a
commit ae63c2b5c9
2 changed files with 71 additions and 33 deletions

View File

@@ -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.