diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-02-25 09:24:59 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-02-25 19:53:18 +0100 |
commit | ce524d0b5ebaef05d29fa368465f31358f26dcda (patch) | |
tree | e5df54a5deeefacbff4916d3619f85c2cb341b01 /tpl/collections | |
parent | 2662faf61ff0240be1ee0d6c496b6b4a6ed55fb4 (diff) |
Add a page template func
Fixes #9339
Diffstat (limited to 'tpl/collections')
-rw-r--r-- | tpl/collections/apply.go | 10 | ||||
-rw-r--r-- | tpl/collections/init.go | 4 |
2 files changed, 8 insertions, 6 deletions
diff --git a/tpl/collections/apply.go b/tpl/collections/apply.go index 74ecc5b19..1dc09c8e5 100644 --- a/tpl/collections/apply.go +++ b/tpl/collections/apply.go @@ -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) } diff --git a/tpl/collections/init.go b/tpl/collections/init.go index c992d3fb2..8801422ac 100644 --- a/tpl/collections/init.go +++ b/tpl/collections/init.go @@ -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, |