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

Add deferred template blocks #8086

Closed
bep opened this issue Dec 22, 2020 · 6 comments · Fixed by #12587
Closed

Add deferred template blocks #8086

bep opened this issue Dec 22, 2020 · 6 comments · Fixed by #12587
Assignees
Milestone

Comments

@bep
Copy link
Member

bep commented Dec 22, 2020

There are certain things we would want to do in Hugo but cannot because it's either 1) Very expensive (build time) to do or 2) Impossible (ref. resources.PostProcess and CSS purging which needs the build to complete before it can do its job).

Note that I'm not saying that would be easy to implement, I just say that it would be cool/useful.

What I'm suggesting would be a way to deffer template processing until after the build, somehow storing away the dot context passed to it, e.g.:

Some regular template blocks:
{{ range .Pages }}{{ .Title }}{{ end }}

{{ defer (dict "page" . "someotherParam" "foo") }}
{{ range .page.BackLinks }}
{{ .Title }}
{{ end }}
{{ end }}

The BackLinks above is just an example of a thing that we cannot effectively calculate upfront.

/cc @regisphilibert and gang.

@bep bep added the Proposal label Dec 22, 2020
@regisphilibert
Copy link
Member

This seems promising but to this day I fail to find any other use case than Backlinks, except maybe info about the build on the site level. (Build time etc...)

In any case, there should be a system for singling out methods available only inside defer blocks. Maybe .Page.Defer.Backlinks, .Site.Defer.Build.Duration ?

While the word defer works great in a template context, it does not work so great as a page method, I'm just using it as an example.

@bep
Copy link
Member Author

bep commented Dec 22, 2020

In any case, there should be a system for singling out methods available only inside defer blocks. Maybe .Page.Defer.Backlinks, .Site.Defer.Build.Duration ?

What you propose above is a variant of resources.PostProcess -- which is useful, but relatively complex behind the scenes, and with some shortcomings. This is, as one example, not possible:

{{ $css := $css | resources.PostProcess }}
{{ $upper := $css.Title | upper }}

Not the greatest example. But the defer keyword solves the above in a much cleaner and powerful way. If we implement defer I will probably eventually deprecate/remove resources.PostProcess.

The more I think about this, the more I like it.

@regisphilibert
Copy link
Member

I see! Then I can see more present benefits and can't wait for the future ones!

@bep bep added this to the v0.80 milestone Dec 22, 2020
@bep
Copy link
Member Author

bep commented Dec 23, 2020

Thinking a little about this, we cannot "invent new syntax" (because of code formatters etc.), so what I now think makes most sense is to use the with keyword, e.g.:

{{ with defer (dict "page" . "someotherParam" "foo") }}
    {{ range .page.BackLinks }}
        {{ .Title }}
   {{ end }}
{{ end }}

Where defer wold be a template func that just echoes what it gets -- but that happens after the build.

@bep bep modified the milestones: v0.80, v0.81 Jan 2, 2021
@bep bep modified the milestones: v0.81, v0.82 Feb 19, 2021
@bep bep modified the milestones: v0.82, v0.83 Mar 21, 2021
@bep bep modified the milestones: v0.83, v0.84 May 1, 2021
@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
@swamidass
Copy link

I like this. It would be great to have.

@bep bep modified the milestones: v0.90, v0.91.0 Dec 13, 2021
@bep bep removed this from the v0.91.0 milestone Dec 22, 2021
@bep bep added this to the v0.128.0 milestone Jun 8, 2024
@bep bep added the Enhancement label Jun 8, 2024
@bep bep self-assigned this Jun 8, 2024
@bep bep removed the Proposal label Jun 8, 2024
bep added a commit to bep/hugo that referenced this issue Jun 9, 2024
@bep bep mentioned this issue Jun 9, 2024
bep added a commit to bep/hugo that referenced this issue Jun 9, 2024
bep added a commit to bep/hugo that referenced this issue Jun 11, 2024
bep added a commit to bep/hugo that referenced this issue Jun 20, 2024
bep added a commit to bep/hugo that referenced this issue Jun 21, 2024
bep added a commit to bep/hugo that referenced this issue Jun 21, 2024
bep added a commit to bep/hugo that referenced this issue Jun 21, 2024
bep added a commit to bep/hugo that referenced this issue Jun 21, 2024
@bep bep modified the milestones: v0.128.0, v0.129.0 Jun 21, 2024
bep added a commit to bep/hugo that referenced this issue Jun 22, 2024
bep added a commit to bep/hugo that referenced this issue Jun 22, 2024
bep added a commit to bep/hugo that referenced this issue Jun 22, 2024
bep added a commit to bep/hugo that referenced this issue Jun 22, 2024
bep added a commit to bep/hugo that referenced this issue Jun 22, 2024
bep added a commit to bep/hugo that referenced this issue Jun 22, 2024
@bep bep closed this as completed in 6cd0784 Jun 23, 2024
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants