mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-31 22:41:53 +02:00
Merge commit '87de22d7464e239c775fbd48ebce1665d5b1e80d'
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
---
|
||||
title: Partial Templates
|
||||
title: Partial templates
|
||||
description: Partials are smaller, context-aware components in your list and page templates that can be used economically to keep your templating DRY.
|
||||
categories: [templates]
|
||||
keywords: [lists,sections,partials]
|
||||
menu:
|
||||
docs:
|
||||
parent: templates
|
||||
weight: 90
|
||||
weight: 90
|
||||
weight: 120
|
||||
weight: 120
|
||||
aliases: [/templates/partial/,/layout/chrome/,/extras/analytics/]
|
||||
toc: true
|
||||
---
|
||||
|
||||
{{< youtube pjS4pOLyB7c >}}
|
||||
|
||||
## Partial Template Lookup Order
|
||||
## Partial template lookup order
|
||||
|
||||
Partial templates---like [single page templates][singletemps] and [list page templates][listtemps]---have a specific [lookup order]. However, partials are simpler in that Hugo will only check in two places:
|
||||
|
||||
@@ -23,7 +23,7 @@ Partial templates---like [single page templates][singletemps] and [list page tem
|
||||
|
||||
This allows a theme's end user to copy a partial's contents into a file of the same name for [further customization][customize].
|
||||
|
||||
## Use Partials in your Templates
|
||||
## Use partials in your templates
|
||||
|
||||
All partials for your Hugo project are located in a single `layouts/partials` directory. For better organization, you can create multiple subdirectories within `partials` as well:
|
||||
|
||||
@@ -64,13 +64,13 @@ As shown in the above example directory structure, you can nest your directories
|
||||
{{ partial "footer/scripts.html" . }}
|
||||
```
|
||||
|
||||
### Variable Scoping
|
||||
### Variable scoping
|
||||
|
||||
The second argument in a partial call is the variable being passed down. The above examples are passing the `.`, which tells the template receiving the partial to apply the current [context][context].
|
||||
|
||||
This means the partial will *only* be able to access those variables. The partial is isolated and *has no access to the outer scope*. From within the partial, `$.Var` is equivalent to `.Var`.
|
||||
|
||||
## Returning a value from a Partial
|
||||
## Returning a value from a partial
|
||||
|
||||
In addition to outputting markup, partials can be used to return a value of any type. In order to return a value, a partial must include a lone `return` statement *at the end of the partial*.
|
||||
|
||||
@@ -113,7 +113,7 @@ In addition to outputting markup, partials can be used to return a value of any
|
||||
Only one `return` statement is allowed per partial file.
|
||||
{{% /note %}}
|
||||
|
||||
## Inline Partials
|
||||
## Inline partials
|
||||
|
||||
You can also define partials inline in the template. But remember that template namespace is global, so you need to make sure that the names are unique to avoid conflicts.
|
||||
|
||||
@@ -126,7 +126,7 @@ Value: {{ partial "my-inline-partial.html" . }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
## Cached Partials
|
||||
## Cached partials
|
||||
|
||||
The [`partialCached` template function][partialcached] can offer significant performance gains for complex templates that don't need to be re-rendered on every invocation. The simplest usage is as follows:
|
||||
|
||||
|
Reference in New Issue
Block a user