fcc3ed651 Remove some expired new-in a9c5981f5 Fix cascade example 82bb250fa Add some lines about permalinks tokens in front matter 328fe564e Remove some outdated new-in fb140b153 Hide showcase menu entry 42d9d1c79 Update image formats from which EXIF data can be extracted 09ad56b6e netlify: Hugo 0.130.0 1d503f846 Merge branch 'tempv0.130.0' e2458074d math: Add trigonometric functions and some angle helper functions 392afc8f9 Disable the showcase section for now 0300750f2 Improve example of image render hook 60a9306af Improve description of the .Site.RegularPages method 8d759175d Fix typos 55daa4554 Update XxHash.md 397c81cb7 Add namespace for hash functions 70fe8d2f0 netlify: Bump Hugo 0.129.0 5a9771aff Merge branch 'tempv0.129.0' f9146575b Fix typo e6e1fea49 Fix typo in Hugo docs | functions | partial 732d10ec4 source: Expose GitInfo Body 34c97e639 netlify: Hugo 0.128.2 3270587e9 Fix typo 727c5396e netlify: Hugo 0.128.1 80b6ae99c Update GitHub Pages workflow file example 027134102 Update GitHub Pages workflow file example 2600a8a2e Miscellaneous edits 3fdd5819b Update Build.md 7764005c3 Improve example of render hook directory structure 5e3941d82 Fix typos 748bf065f Restructure templates section fafbf6566 Update Defer.md 012162e0d Document changes to template functions in v0.128.0 0990ce35b quick-reference: Update emojis 6677a30ef Update Goldmark configuration documentation 4449d530d Document new pagination config 0af8be439 Update Defer.md 56348196d netlify: Hugo 0.128.0 d67b6d82e Update content/en/functions/templates/Defer.md 23d996b3d Update content/en/functions/templates/Defer.md 7f7fb2f27 Document templates.Defer 5ada1e9d5 Fix docs merge (remove shortcode) d27ee6156 Merge branch 'tempv0.128.0' 5d7317c84 Fix typo 7c18ee546 Update theme 83bfea63b Update theme b274b3238 Merge commit '8b9803425e63e1b1801f8d5d676e96368d706722' ff34a035a deploy: Add stripIndexHtml target option d9e964bdb markup/goldmark: Add the Hugo Goldmark Extras "delete" extension ac5bd16d2 deps: Upgrade github.com/alecthomas/chroma v2.13.0 => v2.14.0 25377171b config: Remove extraneous BuildConfig setting 0d2044f6d docs: Regen docshelper a2548dac9 markup/goldmark: Support extras extension 9d0c86ee8 commands: Add gen chromastyles --lineNumbersTableStyle flag git-subtree-dir: docs git-subtree-split: fcc3ed651a1b6431303c2f88f20fa38531c52b3d
5.4 KiB
title | description | categories | keywords | menu | weight | toc | aliases | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Partial templates | Partials are smaller, context-aware components in your list and page templates that can be used economically to keep your templating DRY. |
|
|
110 | true |
|
{{< youtube pjS4pOLyB7c >}}
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:
layouts/
└── partials/
├── footer/
│ ├── scripts.html
│ └── site-footer.html
├── head/
│ ├── favicons.html
│ ├── metadata.html
│ ├── prerender.html
│ └── twitter.html
└── header/
├── site-header.html
└── site-nav.html
All partials are called within your templates using the following pattern:
{{ partial "<PATH>/<PARTIAL>.html" . }}
{{% note %}}
One of the most common mistakes with new Hugo users is failing to pass a context to the partial call. In the pattern above, note how "the dot" (.
) is required as the second argument to give the partial context. You can read more about "the dot" in the Hugo templating introduction.
{{% /note %}}
{{% note %}}
<PARTIAL>
including baseof
is reserved. (#5373)
{{% /note %}}
As shown in the above example directory structure, you can nest your directories within partials
for better source organization. You only need to call the nested partial's path relative to the partials
directory:
{{ partial "header/site-header.html" . }}
{{ partial "footer/scripts.html" . }}
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.
This means the partial will only be able to access those variables. The partial is isolated and cannot access the outer scope. From within the partial, $.Var
is equivalent to .Var
.
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.
Example GetFeatured
{{/* layouts/partials/GetFeatured.html */}}
{{ return first . (where site.RegularPages "Params.featured" true) }}
{{/* layouts/index.html */}}
{{ range partial "GetFeatured.html" 5 }}
[...]
{{ end }}
Example GetImage
{{/* layouts/partials/GetImage.html */}}
{{ $image := false }}
{{ with .Params.gallery }}
{{ $image = index . 0 }}
{{ end }}
{{ with .Params.image }}
{{ $image = . }}
{{ end }}
{{ return $image }}
{{/* layouts/_default/single.html */}}
{{ with partial "GetImage.html" . }}
[...]
{{ end }}
{{% note %}}
Only one return
statement is allowed per partial file.
{{% /note %}}
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.
Value: {{ partial "my-inline-partial.html" . }}
{{ define "partials/my-inline-partial.html" }}
{{ $value := 32 }}
{{ return $value }}
{{ end }}
Cached partials
The partialCached
template function provides significant performance gains for complex templates that don't need to be re-rendered on every invocation. See details.
Examples
header.html
The following header.html
partial template is used for spf13.com:
{{< code file=layouts/partials/header.html >}}
<html class="no-js" lang="en-US" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#"> <head>{{ partial "meta.html" . }}
<base href="{{ .Site.BaseURL }}">
<title> {{ .Title }} : spf13.com </title>
<link rel="canonical" href="{{ .Permalink }}">
{{ if .RSSLink }}<link href="{{ .RSSLink }}" rel="alternate" type="application/rss+xml" title="{{ .Title }}" />{{ end }}
{{ partial "head_includes.html" . }}
</head>
{{< /code >}}
{{% note %}}
The header.html
example partial was built before the introduction of block templates to Hugo. Read more on base templates and blocks for defining the outer chrome or shell of your master templates (i.e., your site's head, header, and footer). You can even combine blocks and partials for added flexibility.
{{% /note %}}
footer.html
The following footer.html
partial template is used for spf13.com:
{{< code file=layouts/partials/footer.html >}}
© 2013-14 Steve Francia. Some rights reserved; please attribute properly and link back.