| 1 |
--- |
| 2 |
title: templates.Defer |
| 3 |
description: Defer execution of a template until after all sites and output formats have been rendered. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: string |
| 10 |
signatures: [templates.Defer OPTIONS] |
| 11 |
aliases: [/functions/templates.defer] |
| 12 |
--- |
| 13 |
|
| 14 |
> [!NOTE] |
| 15 |
> Do not call this function within a `partialCached` template. This restriction applies transitively: if `partialCached` calls a partial that calls `templates.Defer`, Hugo returns an error. Using this function within shortcode or render hook templates may also lead to unpredictable results. |
| 16 |
|
| 17 |
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 [css.TailwindCSS][] using the output of [`hugo_stats.json`][] to determine which classes and other HTML identifiers are being used in the final output: |
| 18 |
|
| 19 |
```go-html-template {file="layouts/baseof.html" copy=true} |
| 20 |
<head> |
| 21 |
... |
| 22 |
{{ with (templates.Defer (dict "key" "global")) }} |
| 23 |
{{ partial "css.html" . }} |
| 24 |
{{ end }} |
| 25 |
... |
| 26 |
</head> |
| 27 |
``` |
| 28 |
|
| 29 |
```go-html-template {file="layouts/_partials/css.html" copy=true} |
| 30 |
{{ with resources.Get "css/main.css" }} |
| 31 |
{{ $opts := dict "minify" (not hugo.IsDevelopment) }} |
| 32 |
{{ with . | css.TailwindCSS $opts }} |
| 33 |
{{ if hugo.IsDevelopment }} |
| 34 |
<link rel="stylesheet" href="{{ .RelPermalink }}"> |
| 35 |
{{ else }} |
| 36 |
{{ with . | fingerprint }} |
| 37 |
<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"> |
| 38 |
{{ end }} |
| 39 |
{{ end }} |
| 40 |
{{ end }} |
| 41 |
{{ end }} |
| 42 |
``` |
| 43 |
|
| 44 |
> [!NOTE] |
| 45 |
> This function only works in combination with the `with` keyword. |
| 46 |
> |
| 47 |
> Variables defined on the outside are not visible on the inside and vice versa. To pass in data, use the `data` [option](#options). |
| 48 |
|
| 49 |
For the above to work well when running the server (or `hugo -w`), you want to have a configuration similar to this: |
| 50 |
|
| 51 |
{{< code-toggle file=hugo >}} |
| 52 |
[build] |
| 53 |
[build.buildStats] |
| 54 |
enable = true |
| 55 |
[[build.cachebusters]] |
| 56 |
source = 'assets/notwatching/hugo_stats\.json' |
| 57 |
target = 'css' |
| 58 |
[[build.cachebusters]] |
| 59 |
source = '(postcss|tailwind)\.config\.js' |
| 60 |
target = 'css' |
| 61 |
[module] |
| 62 |
[[module.mounts]] |
| 63 |
source = 'assets' |
| 64 |
target = 'assets' |
| 65 |
[[module.mounts]] |
| 66 |
disableWatch = true |
| 67 |
source = 'hugo_stats.json' |
| 68 |
target = 'assets/notwatching/hugo_stats.json' |
| 69 |
{{< /code-toggle >}} |
| 70 |
|
| 71 |
## Options |
| 72 |
|
| 73 |
The `templates.Defer` function requires a single argument, a map with the following optional keys: |
| 74 |
|
| 75 |
`key` |
| 76 |
: (`string`) The key to use for the deferred template. This will, combined with a hash of the template content, be used as a cache key. If this is not set, Hugo will execute the deferred template on every render. This is not what you want for shared resources like CSS and JavaScript. |
| 77 |
|
| 78 |
`data` |
| 79 |
: (`map`) Optional map to pass as data to the deferred template. This will be available in the deferred template as `.` or `$`. |
| 80 |
|
| 81 |
```go-html-template |
| 82 |
Language Outside: {{ site.Language.Name }} |
| 83 |
Page Outside: {{ .RelPermalink }} |
| 84 |
I18n Outside: {{ i18n "hello" }} |
| 85 |
{{ $data := (dict "page" . )}} |
| 86 |
{{ with (templates.Defer (dict "data" $data )) }} |
| 87 |
Language Inside: {{ site.Language.Name }} |
| 88 |
Page Inside: {{ .page.RelPermalink }} |
| 89 |
I18n Inside: {{ i18n "hello" }} |
| 90 |
{{ end }} |
| 91 |
``` |
| 92 |
|
| 93 |
The [output format][], [site][], and [language][] will be the same, even if the execution is deferred. In the example above, this means that the `site.Language.Name` and `.RelPermalink` will be the same on the inside and the outside of the deferred template. |
| 94 |
|
| 95 |
[`hugo_stats.json`]: /configuration/build/ |
| 96 |
[css.TailwindCSS]: /functions/css/tailwindcss/ |
| 97 |
[language]: /methods/site/language/ |
| 98 |
[output format]: /configuration/output-formats/ |
| 99 |
[site]: /methods/page/site/ |