partials.IncludeCached
- Syntax
- partials.IncludeCached LAYOUT CONTEXT [VARIANT...]
- Returns
- any
- Alias
- partialCached
Usage
The partials.IncludeCached function returns the cached result of executing the given partial template, optionally passing one or more variant keys. If the partial template contains a return statement, the result is the given value. Otherwise, the result is the rendered output, as a template.HTML or string value depending on the output format of the partial template. See partials.Include for details.
Use this function for complex partial templates that do not need to be rendered again on every call. The first call renders the partial template and caches the result. Later calls with the same LAYOUT and VARIANT values return the cached result, even when CONTEXT is different.
Each site, such as each language, has its own cache, so each site executes a partial template at least once.
Hugo renders pages in parallel, so concurrent calls may render the partial template more than once. After Hugo caches the result, pages that enter the build pipeline use the cached result.
Arguments
LAYOUT- (
string) The path to the partial template, relative to thelayouts/_partialsdirectory. Within a partial template, Hugo resolves a path that begins with./or../relative to the directory of the calling partial template. Seepartials.Includefor details. CONTEXT- (
any) The context to pass to the partial template. Unlike thepartials.Includefunction, this function requires this argument. To pass no context, passnil. VARIANT- (
any) One or more values, of any data type, used to create a unique cache key. Hugo caches a separate result for each unique combination of variant values. The partial template cannot access these values.
Examples
{{ partials.IncludeCached "footer.html" . }}Caching one result per section, for a complex partial template that renders the same output for all pages in a section:
{{ partials.IncludeCached "footer.html" . .Section }}Caching one result per combination of two values:
{{ partials.IncludeCached "footer.html" . .Params.country .Params.province }}