Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support template function definitions #8509

Open
bep opened this issue May 7, 2021 · 1 comment
Open

Support template function definitions #8509

bep opened this issue May 7, 2021 · 1 comment
Labels
Milestone

Comments

@bep
Copy link
Member

bep commented May 7, 2021

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:

{{define "link url text?"}}<a href="{{.url}}">{{or .text .url}}</a>{{end}}

The Go home page is {{link "https://golang.org"}}.
The Go home page is {{link "https://golang.org" "Google Home"}}.

It would also be nice if we could support the return keyword for these so we could do:

{{define "add a b"}}{{ $result := add .a .b }}{{ return $result }}{{end}}

The above example probably shows that we should have a check to make sure you don't redefine an existing func.

/cc @regisphilibert

@bep bep added the Proposal label May 7, 2021
@bep bep added this to the v0.84 milestone May 7, 2021
@regisphilibert
Copy link
Member

regisphilibert commented May 7, 2021

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.

return

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.

partialCached equivalent?

Is there a cached solution in place so that the process is never done twice for the same set of argument values?

best practices (too soon?)

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.

  1. Call a partial once which declares all the defines (stored in their own partials maybe)
  2. Call a single partial containing the define in every template file the "defined function" needs to be used.
{{ partial "defines/func/process" }}

{{ $first := process .first }}
{{ $second := process .second }}
{{ $third := process .thirst }}

@bep bep modified the milestones: v0.84, v0.85 Jun 18, 2021
@bep bep modified the milestones: v0.85, v0.86 Jul 5, 2021
@bep bep modified the milestones: v0.86, v0.87, v0.88 Jul 26, 2021
@bep bep modified the milestones: v0.88, v0.89 Sep 2, 2021
@bep bep modified the milestones: v0.89, v0.90 Nov 2, 2021
@bep bep modified the milestones: v0.90, v0.91.0 Dec 13, 2021
@bep bep modified the milestones: v0.91.0, v0.92.0 Dec 22, 2021
@bep bep modified the milestones: v0.92.0, v0.93.0 Jan 12, 2022
@bep bep modified the milestones: v0.93.0, v0.94.0 Mar 1, 2022
@bep bep modified the milestones: v0.94.0, v0.95.0, v0.96.0 Mar 9, 2022
@bep bep modified the milestones: v0.96.0, v0.97.0 Mar 24, 2022
@bep bep removed this from the v0.97.0 milestone Apr 13, 2022
@bep bep modified the milestones: v0.113.0, v0.115.0 Jun 13, 2023
@bep bep modified the milestones: v0.115.0, v0.116.0 Jun 30, 2023
@bep bep modified the milestones: v0.116.0, v0.117.0 Aug 1, 2023
@bep bep modified the milestones: v0.117.0, v0.118.0 Aug 30, 2023
@bep bep modified the milestones: v0.118.0, v0.119.0 Sep 15, 2023
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 4, 2023
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
@bep bep modified the milestones: v0.121.0, v0.122.0 Dec 6, 2023
@bep bep modified the milestones: v0.122.0, v0.123.0, v0.124.0 Jan 27, 2024
@bep bep modified the milestones: v0.124.0, v0.125.0 Mar 4, 2024
@bep bep modified the milestones: v0.125.0, v0.126.0 Apr 23, 2024
@bep bep modified the milestones: v0.126.0, v0.127.0 May 15, 2024
@bep bep modified the milestones: v0.127.0, v0.128.0 Jun 8, 2024
@bep bep modified the milestones: v0.128.0, v0.129.0 Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants