Skip to content

Commit

Permalink
Document templates.Defer
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jun 21, 2024
1 parent 9b06f95 commit bbf9c46
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions content/en/functions/templates/Defer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
title: templates.Defer
description: Defer execution of a template until after all sites and output formats have been rendered.
categories: []
keywords: []
toc: true
action:
aliases: []
related: []
returnType: string
signatures: [templates.Defer OPTIONS]
aliases: [/functions/templates.exists]
---

{{< new-in "0.128.0" >}}

In some rare use cases, you may want 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:

```go-html-template
{{ with (templates.Defer (dict "key" "styles" )) }}
{{ $options := dict "inlineImports" true }}
{{ $styles := resources.Get "css/styles.css" }}
{{ $styles = $styles | resources.PostCSS $options }}
{{ if hugo.IsProduction }}
{{ $styles = $styles | minify | fingerprint }}
{{ end }}
<link href="{{ $styles.RelPermalink }}" rel="stylesheet" />
{{ end }}
```

For the above to work well when running the server (or `hugo -w`), you want to have a configuration similar to this:

{{< code-toggle file=hugo >}}
[module]
[[module.mounts]]
source = "hugo_stats.json"
target = "assets/notwatching/hugo_stats.json"
disableWatch = true
[build]
writeStats = true
[[build.cachebusters]]
source = "assets/notwatching/hugo_stats\\.json"
target = "styles\\.css"
[[build.cachebusters]]
source = "(postcss|tailwind)\\.config\\.js"
target = "css"
{{< /code-toggle >}}

## Options

The `templates.Defer` function takes a single argument, a map with the following optional keys:

key (`string`)
: The key to use for the deferred template. This can be thought of 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.

data (`map`)
: Optional map to pass as data the deferred template. This will be available in the deferred template as `.` or `$`.


```go-html-template
{{ $data := (dict "page" . )}}
{{ with (templates.Defer (dict "data" $data )) }}
Page: {{ .page.Title }}
{{ end }}
```

0 comments on commit bbf9c46

Please sign in to comment.