Add a page template func

Fixes #9339
This commit is contained in:
Bjørn Erik Pedersen
2023-02-25 09:24:59 +01:00
parent 2662faf61f
commit ce524d0b5e
54 changed files with 436 additions and 108 deletions

View File

@@ -40,7 +40,7 @@ func (ns *Namespace) Apply(ctx context.Context, c any, fname string, args ...any
return nil, errors.New("can't iterate over a nil value")
}
fnv, found := ns.lookupFunc(fname)
fnv, found := ns.lookupFunc(ctx, fname)
if !found {
return nil, errors.New("can't find function " + fname)
}
@@ -106,7 +106,7 @@ func applyFnToThis(ctx context.Context, fn, this reflect.Value, args ...any) (re
return reflect.ValueOf(nil), res[1].Interface().(error)
}
func (ns *Namespace) lookupFunc(fname string) (reflect.Value, bool) {
func (ns *Namespace) lookupFunc(ctx context.Context, fname string) (reflect.Value, bool) {
namespace, methodName, ok := strings.Cut(fname, ".")
if !ok {
templ := ns.deps.Tmpl().(tpl.TemplateFuncGetter)
@@ -114,16 +114,16 @@ func (ns *Namespace) lookupFunc(fname string) (reflect.Value, bool) {
}
// Namespace
nv, found := ns.lookupFunc(namespace)
nv, found := ns.lookupFunc(ctx, namespace)
if !found {
return reflect.Value{}, false
}
fn, ok := nv.Interface().(func(...any) (any, error))
fn, ok := nv.Interface().(func(context.Context, ...any) (any, error))
if !ok {
return reflect.Value{}, false
}
v, err := fn()
v, err := fn(ctx)
if err != nil {
panic(err)
}

View File

@@ -14,6 +14,8 @@
package collections
import (
"context"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/tpl/internal"
)
@@ -26,7 +28,7 @@ func init() {
ns := &internal.TemplateFuncsNamespace{
Name: name,
Context: func(args ...any) (any, error) { return ctx, nil },
Context: func(cctx context.Context, args ...any) (any, error) { return ctx, nil },
}
ns.AddMethodMapping(ctx.After,