Skip to content

Commit

Permalink
Update Defer.md
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jun 25, 2024
1 parent 5634819 commit 0af8be4
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions content/en/functions/templates/Defer.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,28 @@ aliases: [/functions/templates.defer]
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:

```go-html-template
{{ with (templates.Defer (dict "key" "global-styles" )) }}
{{ $options := dict "inlineImports" true }}
{{ $styles := resources.Get "css/styles.css" }}
{{ $styles = $styles | resources.PostCSS $options }}
{{ if hugo.IsProduction }}
{{ $styles = $styles | minify | fingerprint }}
{{ with (templates.Defer (dict "key" "global")) }}
{{ $t := debug.Timer "tailwindcss" }}
{{ with resources.Get "css/styles.css" }}
{{ $opts := dict
"inlineImports" true
"optimize" hugo.IsProduction
}}
{{ with . | css.TailwindCSS $opts }}
{{ if hugo.IsDevelopment }}
<link rel="stylesheet" href="{{ .RelPermalink }}" />
{{ else }}
{{ with . | minify | fingerprint }}
<link
rel="stylesheet"
href="{{ .RelPermalink }}"
integrity="{{ .Data.Integrity }}"
crossorigin="anonymous" />
{{ end }}
{{ end }}
<link href="{{ $styles.RelPermalink }}" rel="stylesheet" />
{{ end }}
{{ end }}
{{ $t.Stop }}
{{ end }}
```

Expand Down

0 comments on commit 0af8be4

Please sign in to comment.