HUGO
Menu
GitHub 90056 stars Mastodon

partials.Include

Executes the given template, optionally passing context. If the partial template contains a return statement, returns the given value, else returns the rendered output.
Syntax
partials.Include NAME [CONTEXT]
Returns
any
Alias
partial

Without a return statement, the partial function returns a string of type template.HTML. With a return statement, the partial function can return any data type.

In this example we have three partial templates:

layouts/
└── _partials/
    ├── average.html
    ├── breadcrumbs.html
    └── footer.html

The “average” partial template returns the average of one or more numbers. We pass the numbers in context:

{{ $numbers := slice 1 6 7 42 }}
{{ $average := partial "average.html" $numbers }}

The “breadcrumbs” partial template renders breadcrumb navigation, and needs to receive the current page in context:

{{ partial "breadcrumbs.html" . }}

The “footer” partial template renders the site footer. In this contrived example, the footer does not need access to the current page, so we can omit context:

{{ partial "footer.html" }}

You can pass anything in context: a page, a page collection, a scalar value, a slice, or a map. In this example we pass the current page and three scalar values:

{{ $ctx := dict
  "page" .
  "name" "John Doe"
  "major" "Finance"
  "gpa" 4.0
}}
{{ partial "render-student-info.html" $ctx }}

Then, within the partial template:

<p>{{ .name }} is majoring in {{ .major }}.</p>
<p>Their grade point average is {{ .gpa }}.</p>
<p>See <a href="{{ .page.RelPermalink }}">details.</a></p>

To return a value from a partial template, use the return statement:

{{ if math.ModBool . 2 }}
  {{ return "even" }}
{{ end }}
{{ return "odd" }}

Relative paths

New in v0.167.0

Within a partial template, a path that begins with ./ or ../ is resolved relative to the directory of the calling partial template. For example, given this structure:

layouts/
└── _partials/
    ├── cards/
    │   ├── card.html
    │   └── image.html
    └── footer.html

The “card” partial template can call its sibling and the “footer” partial template like this:

{{ partial "./image.html" . }}
{{ partial "../footer.html" . }}

Relative paths are only supported from within partial templates. Calling partial "./foo.html" from any other template, or with a path that resolves outside the _partials directory, is an error.