Open bep opened 3 years ago
TL;DR: Awesome! Can't wait!
I do see it as an opportunity to step away from the partial
API that I (and possibly others) have been exploiting all around to achieve goals it has never been designed for (having to use dict
to pass >1 arguments is tiresome.) This would also bring the usage of reusable code closer to other templating or even programming languages.
{{ link "https://golang.org" "Google Home" }}
🎉
vs
{{ partial "func/link" (dict "url" "https://golang.org" "text" "Google Home") }}
😩
Even without the return
keyword it would really help create reusable template components for any given projects (link, button, image etc...) aligned with other template languages like Nunjucks and its macros
or other's mixins
and the like.
But I'm very excited about the possibility of adding the return
keyword in the mix! It would really change the way reusable non-templating code is handled in Hugo. This would bring the DX closer to regular programming languages.
Is there a cached solution in place so that the process is never done twice for the same set of argument values?
I already wonder what the best practice should be as in order to keep functions in their own files, we'd have to call a partial containing the define
before using it.
{{ partial "defines/func/process" }}
{{ $first := process .first }}
{{ $second := process .second }}
{{ $third := process .thirst }}
See https://pkg.go.dev/rsc.io/tmplfunc#hdr-Function_Definitions
I don't think it's practical to use that library directly, but the idea (and the parsing logic) is intriguing.
I'll use one the examples in his docs:
It would also be nice if we could support the
return
keyword for these so we could do:The above example probably shows that we should have a check to make sure you don't redefine an existing func.
/cc @regisphilibert