Debian Codegraph @

docs/content/en/functions/partials/IncludeCached.md

raw
1 ---
2 title: partials.IncludeCached
3 description: Executes the given template and caches the result, optionally passing one or more variant keys. If the partial template contains a return statement, returns the given value, else returns the rendered output.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: [partialCached]
9 returnType: any
10 signatures: ['partials.IncludeCached LAYOUT CONTEXT [VARIANT...]']
11 aliases: [/functions/partialcached]
12 ---
13
14 Without a [`return`][] statement, the `partialCached` function returns a string of type `template.HTML`. With a `return` statement, the `partialCached` function can return any data type.
15
16 The `partialCached` function can offer significant performance gains for complex templates that don't need to be re-rendered on every invocation.
17
18 > [!NOTE]
19 > Each site (or language) has its own `partialCached` cache, so each site will execute a _partial_ template once.
20 >
21 > Hugo renders pages in parallel, and will render the _partial_ template more than once with concurrent calls to the `partialCached` function. After Hugo caches the rendered _partial_ template, new pages entering the build pipeline will use the cached result.
22
23 Within a _partial_ template, a path that begins with `./` or `../` is resolved relative to the calling _partial_ template. See [`partials.Include`](/functions/partials/include/#relative-paths).
24
25 Here is the simplest usage:
26
27 ```go-html-template
28 {{ partialCached "footer.html" . }}
29 ```
30
31 Pass additional arguments to `partialCached` to create variants of the cached _partial_ template. For example, if you have a complex _partial_ template that should be identical when rendered for pages within the same section, use a variant based on section so that the _partial_ template is only rendered once per section:
32
33 ```go-html-template {file="layouts/baseof.html"}
34 {{ partialCached "footer.html" . .Section }}
35 ```
36
37 Pass additional arguments, of any data type, as needed to create unique variants:
38
39 ```go-html-template
40 {{ partialCached "footer.html" . .Params.country .Params.province }}
41 ```
42
43 The variant arguments are not available to the underlying _partial_ template; they are only used to create unique cache keys.
44
45 To return a value from a _partial_ template, use the `return` statement:
46
47 ```go-html-template
48 {{ if math.ModBool . 2 }}
49 {{ return "even" }}
50 {{ end }}
51 {{ return "odd" }}
52 ```
53
54 [`return`]: /functions/go-template/return/