gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
73.57k stars 7.39k forks source link

Allow partials to be invoked on the form p.MyPartial #10544

Open bep opened 1 year ago

bep commented 1 year ago

Looking at

https://discourse.gohugo.io/t/possible-to-write-custom-filter/41900

This doesn't look too bad, but having these partials referenced as strings makes the code highlighting less than beautiful.

We could even consider a shorter namespace "alias", so it would look like:

{{ $title := .Title  | emojify | p.CamelCase }}

We have had similar discussions before, and they have digressed pretty quickly. The above should be possible. I see 2 issues:

regisphilibert commented 1 year ago

I'm not sure how these should be represented on the file system (partials/camel_case.html?)

Any reason they cannot match the file system (albeit the nesting which could be represented by a dot) partials/camel_case.html > p.camel_case partials/CamelCase.html > p.CamelCase partials/post/camel_case > p.post.camel_case

bep commented 1 year ago

Any reason they cannot match the file system (albeit the nesting which could be represented by a dot)

The "dot" for nesting would be cool, but maybe a little bit tricky. I'll think about it, but you're probably right in that we can let the developer worry about the "naming problem".

bep commented 1 year ago

OK, thinking about this for a minute. If we

That should just work.

regisphilibert commented 1 year ago

I think that last comment was directed at maintainers rather than users, I sure did not get it :)

partiaCached

In any case we did not evoke partialCached which for me is essential, and I'd love to be able to use it with this. pc. maybe? Of course it couldn't be used in the elegant pipe context (variant being the last argument) but I think people using it would be aware.

Suffix

I wonder if we really need this suffix, if users are aware of existing reserved function... but if not too complicated it could be customized to something different for both partial and partialCached or disabled altogether.

partial_suffix: p or partial_suffix: none

bep commented 1 year ago

In any case we did not evoke partialCached which for me is essential

Well, in my head, most of the partialCached use cases have a "bigger scope" and can be added the old way. The main use I see for this is to create some simple filter functions to be used in a pipe...

Would upper case P (for cached) be too magic?

{{ $title := .Title  | emojify | P.ToUpper }}

I would hate it if we had to destroy the above by adding some "cache options".

regisphilibert commented 1 year ago

I'm fine with P uppercase.

bep commented 1 year ago

/cc @jmooring opionins?

jmooring commented 1 year ago

The only time I've wanted something like this was with functions (returns something), where caching was not desirable. The p/P thing seems like it might be an easy mistake to make, and even harder to track down. And I can't think of anything else like it in the API. But overall I like this.

Maybe someday we can use it with positional args as well:

{{ p.func.mul 6 7 }}
regisphilibert commented 1 year ago

P, pc, pC... I agree the first one could lead to confusion and using casing for two different functions (taking different set of arguments) is unheard of... maybe it will come back and haunt us later but I don't think we should give up on using this on partial caching. So whatever the suffix, I'll support it :)

jmooring commented 1 year ago

I'm good with p.func.foo and pc.func.foo.