Update content/en/functions/templates/Defer.md

Co-authored-by: Joe Mooring <joe@mooring.com>
This commit is contained in:
Bjørn Erik Pedersen 2024-06-21 15:21:07 +02:00
parent 23d996b3da
commit d67b6d82e4

View File

@ -14,14 +14,10 @@ aliases: [/functions/templates.defer]
{{< new-in "0.128.0" >}} {{< new-in "0.128.0" >}}
{{% note %}} In some rare use cases, you may need to defer the execution of a template until after all sites and output formats have been rendered. One such example could be [TailwindCSS](https://github.com/bep/hugo-starter-tailwind-basic) using the output of [hugo_stats.json](https://gohugo.io/getting-started/configuration/#configure-build) to determine which classes and other HTML identifiers are being used in the final output:
This function only works in combination with the `with` keyword.
{{% /note %}}
In some rare use cases, you may need defer the execution of a template until after all sites and output formats have been rendered. One such example could be [TailwindCSS](https://github.com/bep/hugo-starter-tailwind-basic) using the output of [hugo_stats.json](https://gohugo.io/getting-started/configuration/#configure-build) to determine which classes and other HTML identifiers are being used in the final output:
```go-html-template ```go-html-template
{{ with (templates.Defer (dict "key" "styles" )) }} {{ with (templates.Defer (dict "key" "global-styles" )) }}
{{ $options := dict "inlineImports" true }} {{ $options := dict "inlineImports" true }}
{{ $styles := resources.Get "css/styles.css" }} {{ $styles := resources.Get "css/styles.css" }}
{{ $styles = $styles | resources.PostCSS $options }} {{ $styles = $styles | resources.PostCSS $options }}
@ -32,9 +28,13 @@ In some rare use cases, you may need defer the execution of a template until aft
{{ end }} {{ end }}
``` ```
{{% note %}}
This function only works in combination with the `with` keyword.
{{% /note %}}
{{% note %}} {{% note %}}
Variables defined on the outside are not visible on the inside and vice versa. Variables defined on the outside are not visible on the inside and vice versa. To pass in data, use the `data` [option](#options).
{{% /note %}} {{% /note %}}
For the above to work well when running the server (or `hugo -w`), you want to have a configuration similar to this: For the above to work well when running the server (or `hugo -w`), you want to have a configuration similar to this:
@ -46,7 +46,7 @@ source = "hugo_stats.json"
target = "assets/notwatching/hugo_stats.json" target = "assets/notwatching/hugo_stats.json"
disableWatch = true disableWatch = true
[build.buildStats] [build.buildStats]
writeStats = true enable = true
[[build.cachebusters]] [[build.cachebusters]]
source = "assets/notwatching/hugo_stats\\.json" source = "assets/notwatching/hugo_stats\\.json"
target = "styles\\.css" target = "styles\\.css"
@ -69,13 +69,13 @@ data (`map`)
```go-html-template ```go-html-template
Language Outside: {{ site.Language.Lang }} Language Outside: {{ site.Language.Lang }}
Page Outside: {{ .RelPermalink }} Page Outside: {{ .RelPermalink }}
I18n: {{ i18n "hello" }} I18n Outside: {{ i18n "hello" }}
{{ $data := (dict "page" . )}} {{ $data := (dict "page" . )}}
{{ with (templates.Defer (dict "data" $data )) }} {{ with (templates.Defer (dict "data" $data )) }}
Language Inside: {{ site.Language.Lang }} Language Inside: {{ site.Language.Lang }}
Page Inside: {{ .page.RelPermalink }} Page Inside: {{ .page.RelPermalink }}
I18n: {{ i18n "hello" }} I18n Inside: {{ i18n "hello" }}
{{ end }} {{ end }}
``` ```
The [Output Format](/templates/output-formats/), [Site](/methods/page/site/), and [language](/methods/site/language) will be the same, even if the execution is deferred. In the example above, this means that the `site.Language.Lang` and `.RelPermalink` will be the same inside and outside the deferred template. The [Output Format](/templates/output-formats/), [Site](/methods/page/site/), and [language](/methods/site/language) will be the same, even if the execution is deferred. In the example above, this means that the `site.Language.Lang` and `.RelPermalink` will be the same on the inside and the outside the deferred template.