Repository: gohugoio/hugo (language: go)

Issue:
error calling apply: called apply using string as type context.Context
I use the latest example in [the apply function](https://gohugo.io/functions/apply/) to render my tags in my blog. With the new version, this is not possible anymore and I get the following error:

```txt
Error: Error building site: failed to render pages: render of "page" failed: execute of template failed: template: block/single.html:18:5: executing "main" at <partial "page/footer.html" .>: error calling partial: "~/site/themes/theme/layouts/partials/page/footer.html:22:19": execute of template failed: template: partials/page/footer.html:22:19: executing "partials/page/footer.html" at <apply $sort "partial" "post-tag-link" ".">: error calling apply: called apply using string as type context.Context
```

<!-- Please answer these questions before submitting your issue. Thanks! -->

### What version of Hugo are you using (`hugo version`)?

<pre>
$ hugo version
hugo v0.93.0+extended darwin/amd64 BuildDate=unknown
</pre>

### Does this issue reproduce with the latest release?

Yes, it was working fine with v0.92.2.



Discussion / hints:

Fix patch:
diff --git a/common/hreflect/helpers.go b/common/hreflect/helpers.go
index 0cc42ecc5b2..beab182bbdb 100644
--- a/common/hreflect/helpers.go
+++ b/common/hreflect/helpers.go
@@ -17,6 +17,7 @@
 package hreflect
 
 import (
+	"context"
 	"reflect"
 
 	"github.com/gohugoio/hugo/common/types"
@@ -124,3 +125,5 @@ func indirectInterface(v reflect.Value) reflect.Value {
 	}
 	return v.Elem()
 }
+
+var ContextInterface = reflect.TypeOf((*context.Context)(nil)).Elem()
diff --git a/tpl/collections/apply.go b/tpl/collections/apply.go
index 6eedb4b6351..0833e5507c2 100644
--- a/tpl/collections/apply.go
+++ b/tpl/collections/apply.go
@@ -14,16 +14,18 @@
 package collections
 
 import (
+	"context"
 	"errors"
 	"fmt"
 	"reflect"
 	"strings"
 
+	"github.com/gohugoio/hugo/common/hreflect"
 	"github.com/gohugoio/hugo/tpl"
 )
 
 // Apply takes a map, array, or slice and returns a new slice with the function fname applied over it.
-func (ns *Namespace) Apply(seq interface{}, fname string, args ...interface{}) (interface{}, error) {
+func (ns *Namespace) Apply(ctx context.Context, seq interface{}, fname string, args ...interface{}) (interface{}, error) {
 	if seq == nil {
 		return make([]interface{}, 0), nil
 	}
@@ -43,15 +45,13 @@ func (ns *Namespace) Apply(seq interface{}, fname string, args ...interface{}) (
 		return nil, errors.New("can't find function " + fname)
 	}
 
-	// fnv := reflect.ValueOf(fn)
-
 	switch seqv.Kind() {
 	case reflect.Array, reflect.Slice:
 		r := make([]interface{}, seqv.Len())
 		for i := 0; i < seqv.Len(); i++ {
 			vv := seqv.Index(i)
 
-			vvv, err := applyFnToThis(fnv, vv, args...)
+			vvv, err := applyFnToThis(ctx, fnv, vv, args...)
 			if err != nil {
 				return nil, err
 			}
@@ -65,7 +65,12 @@ func (ns *Namespace) Apply(seq interface{}, fname string, args ...interface{}) (
 	}
 }
 
-func applyFnToThis(fn, this reflect.Value, args ...interface{}) (reflect.Value, error) {
+func applyFnToThis(ctx context.Context, fn, this reflect.Value, args ...interface{}) (reflect.Value, error) {
+	num := fn.Type().NumIn()
+	if num > 0 && fn.Type().In(0).Implements(hreflect.ContextInterface) {
+		args = append([]interface{}{ctx}, args...)
+	}
+
 	n := make([]reflect.Value, len(args))
 	for i, arg := range args {
 		if arg == "." {
@@ -75,8 +80,6 @@ func applyFnToThis(fn, this reflect.Value, args ...interface{}) (reflect.Value,
 		}
 	}
 
-	num := fn.Type().NumIn()
-
 	if fn.Type().IsVariadic() {
 		num--
 	}


Test patch:
diff --git a/tpl/collections/apply_test.go b/tpl/collections/apply_test.go
index 1afb66808f8..9ad0f405d63 100644
--- a/tpl/collections/apply_test.go
+++ b/tpl/collections/apply_test.go
@@ -73,21 +73,23 @@ func TestApply(t *testing.T) {
 
 	strings := []interface{}{"a\n", "b\n"}
 
-	result, err := ns.Apply(strings, "print", "a", "b", "c")
+	ctx := context.Background()
+
+	result, err := ns.Apply(ctx, strings, "print", "a", "b", "c")
 	c.Assert(err, qt.IsNil)
 	c.Assert(result, qt.DeepEquals, []interface{}{"abc", "abc"})
 
-	_, err = ns.Apply(strings, "apply", ".")
+	_, err = ns.Apply(ctx, strings, "apply", ".")
 	c.Assert(err, qt.Not(qt.IsNil))
 
 	var nilErr *error
-	_, err = ns.Apply(nilErr, "chomp", ".")
+	_, err = ns.Apply(ctx, nilErr, "chomp", ".")
 	c.Assert(err, qt.Not(qt.IsNil))
 
-	_, err = ns.Apply(strings, "dobedobedo", ".")
+	_, err = ns.Apply(ctx, strings, "dobedobedo", ".")
 	c.Assert(err, qt.Not(qt.IsNil))
 
-	_, err = ns.Apply(strings, "foo.Chomp", "c\n")
+	_, err = ns.Apply(ctx, strings, "foo.Chomp", "c\n")
 	if err == nil {
 		t.Errorf("apply with unknown func should fail")
 	}
diff --git a/tpl/collections/integration_test.go b/tpl/collections/integration_test.go
new file mode 100644
index 00000000000..d767c384c64
--- /dev/null
+++ b/tpl/collections/integration_test.go
@@ -0,0 +1,45 @@
+// Copyright 2022 The Hugo Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package collections_test
+
+import (
+	"testing"
+
+	"github.com/gohugoio/hugo/hugolib"
+)
+
+// Issue 9585
+func TestApplyWithContext(t *testing.T) {
+	t.Parallel()
+
+	files := `
+-- config.toml --
+baseURL = 'http://example.com/'
+-- layouts/index.html --
+{{ apply (seq 3) "partial" "foo.html"}}
+-- layouts/partials/foo.html --
+{{ return "foo"}}
+  `
+
+	b := hugolib.NewIntegrationTestBuilder(
+		hugolib.IntegrationTestConfig{
+			T:           t,
+			TxtarString: files,
+		},
+	).Build()
+
+	b.AssertFileContent("public/index.html", `
+	[foo foo foo]
+`)
+}