2013-08-17 12:34:25 +00:00
|
|
|
|
---
|
2014-05-29 22:42:05 +00:00
|
|
|
|
aliases:
|
|
|
|
|
- /layout/chrome/
|
2016-01-06 22:45:19 +00:00
|
|
|
|
lastmod: 2016-01-01
|
2014-05-29 22:42:05 +00:00
|
|
|
|
date: 2013-07-01
|
2014-04-23 07:00:11 +00:00
|
|
|
|
menu:
|
|
|
|
|
main:
|
2014-05-29 22:42:05 +00:00
|
|
|
|
parent: layout
|
|
|
|
|
next: /templates/rss
|
2016-04-02 15:34:04 +00:00
|
|
|
|
prev: /templates/blocks/
|
2014-05-29 22:42:05 +00:00
|
|
|
|
title: Partial Templates
|
|
|
|
|
weight: 80
|
2015-05-22 18:46:09 +00:00
|
|
|
|
toc: true
|
2013-08-17 12:34:25 +00:00
|
|
|
|
---
|
|
|
|
|
|
2014-09-03 04:12:26 +00:00
|
|
|
|
In practice, it's very convenient to split out common template portions into a
|
2014-08-30 03:42:26 +00:00
|
|
|
|
partial template that can be included anywhere. As you create the rest of your
|
2016-12-16 00:09:51 +00:00
|
|
|
|
templates, you will include templates from the ``/layouts/partials` directory
|
|
|
|
|
or from arbitrary subdirectories like `/layouts/partials/post/tag`.
|
2014-06-06 20:15:19 +00:00
|
|
|
|
|
|
|
|
|
Partials are especially important for themes as it gives users an opportunity
|
|
|
|
|
to overwrite just a small part of your theme, while maintaining future compatibility.
|
|
|
|
|
|
2014-09-03 04:12:26 +00:00
|
|
|
|
Theme developers may want to include a few partials with empty HTML
|
2014-06-06 20:15:19 +00:00
|
|
|
|
files in the theme just so end users have an easy place to inject their
|
|
|
|
|
customized content.
|
2013-08-17 12:34:25 +00:00
|
|
|
|
|
2014-05-27 22:32:57 +00:00
|
|
|
|
I've found it helpful to include a header and footer template in
|
|
|
|
|
partials so I can include those in all the full page layouts. There is
|
|
|
|
|
nothing special about header.html and footer.html other than they seem
|
|
|
|
|
like good names to use for inclusion in your other templates.
|
2013-08-17 12:34:25 +00:00
|
|
|
|
|
|
|
|
|
▾ layouts/
|
2014-05-27 22:32:57 +00:00
|
|
|
|
▾ partials/
|
2013-08-17 12:34:25 +00:00
|
|
|
|
header.html
|
|
|
|
|
footer.html
|
|
|
|
|
|
2015-08-04 18:00:08 +00:00
|
|
|
|
## Partial vs Template
|
2014-06-06 20:15:19 +00:00
|
|
|
|
|
2014-09-03 04:12:26 +00:00
|
|
|
|
Version v0.12 of Hugo introduced the `partial` call inside the template system.
|
2014-06-06 20:15:19 +00:00
|
|
|
|
This is a change to the way partials were handled previously inside the
|
2014-09-03 04:12:26 +00:00
|
|
|
|
template system. In earlier versions, Hugo didn’t treat partials specially, and
|
2014-08-30 03:42:26 +00:00
|
|
|
|
you could include a partial template with the `template` call in the standard
|
|
|
|
|
template language.
|
2014-06-06 20:15:19 +00:00
|
|
|
|
|
2014-09-03 04:12:26 +00:00
|
|
|
|
With the addition of the theme system in v0.11, it became apparent that a theme
|
2016-12-14 02:37:34 +00:00
|
|
|
|
& override-aware partial was needed.
|
2014-06-06 20:15:19 +00:00
|
|
|
|
|
2014-09-03 04:12:26 +00:00
|
|
|
|
When using Hugo v0.12 and above, please use the `partial` call (and leave out
|
|
|
|
|
the “partial/” path). The old approach would still work, but wouldn’t benefit from
|
2014-06-06 20:15:19 +00:00
|
|
|
|
the ability to have users override the partial theme file with local layouts.
|
|
|
|
|
|
2014-09-03 04:12:26 +00:00
|
|
|
|
## Example header.html
|
2015-01-28 02:17:09 +00:00
|
|
|
|
This header template is used for [spf13.com](http://spf13.com/):
|
2013-08-17 12:34:25 +00:00
|
|
|
|
|
2014-05-15 13:58:55 +00:00
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html class="no-js" lang="en-US" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="utf-8">
|
2013-08-17 12:34:25 +00:00
|
|
|
|
|
2014-06-06 20:15:19 +00:00
|
|
|
|
{{ partial "meta.html" . }}
|
2013-08-17 12:34:25 +00:00
|
|
|
|
|
2015-03-18 06:44:12 +00:00
|
|
|
|
<base href="{{ .Site.BaseURL }}">
|
2014-05-15 13:58:55 +00:00
|
|
|
|
<title> {{ .Title }} : spf13.com </title>
|
|
|
|
|
<link rel="canonical" href="{{ .Permalink }}">
|
2016-11-17 07:18:55 +00:00
|
|
|
|
{{ if .RSSLink }}<link href="{{ .RSSLink }}" rel="alternate" type="application/rss+xml" title="{{ .Title }}" />{{ end }}
|
2013-08-17 12:34:25 +00:00
|
|
|
|
|
2014-06-06 20:15:19 +00:00
|
|
|
|
{{ partial "head_includes.html" . }}
|
2014-05-15 13:58:55 +00:00
|
|
|
|
</head>
|
|
|
|
|
<body lang="en">
|
2013-08-17 12:34:25 +00:00
|
|
|
|
|
2014-09-03 04:12:26 +00:00
|
|
|
|
## Example footer.html
|
2015-01-28 02:17:09 +00:00
|
|
|
|
This footer template is used for [spf13.com](http://spf13.com/):
|
2013-08-17 12:34:25 +00:00
|
|
|
|
|
2014-05-15 13:58:55 +00:00
|
|
|
|
<footer>
|
|
|
|
|
<div>
|
|
|
|
|
<p>
|
2014-09-03 04:12:26 +00:00
|
|
|
|
© 2013-14 Steve Francia.
|
2015-08-04 18:00:08 +00:00
|
|
|
|
<a href="http://creativecommons.org/licenses/by/3.0/" title="Creative Commons Attribution">Some rights reserved</a>;
|
2014-05-15 13:58:55 +00:00
|
|
|
|
please attribute properly and link back. Hosted by <a href="http://servergrove.com">ServerGrove</a>.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</footer>
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
|
|
|
|
|
var _gaq = _gaq || [];
|
|
|
|
|
_gaq.push(['_setAccount', 'UA-XYSYXYSY-X']);
|
|
|
|
|
_gaq.push(['_trackPageview']);
|
|
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
var ga = document.createElement('script');
|
2015-08-04 18:00:08 +00:00
|
|
|
|
ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
|
2014-05-15 13:58:55 +00:00
|
|
|
|
'http://www') + '.google-analytics.com/ga.js';
|
|
|
|
|
ga.setAttribute('async', 'true');
|
|
|
|
|
document.documentElement.firstChild.appendChild(ga);
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
2013-08-17 12:34:25 +00:00
|
|
|
|
|
2016-12-16 00:09:51 +00:00
|
|
|
|
To reference a partial template stored in a subfolder, e.g. `/layouts/partials/post/tag/list.html`, call it this way:
|
2015-04-27 01:34:09 +00:00
|
|
|
|
|
|
|
|
|
{{ partial "post/tag/list" . }}
|
|
|
|
|
|
2016-12-16 00:09:51 +00:00
|
|
|
|
Note that the subdirectories you create under /layouts/partials can be named whatever you like.
|
2015-04-27 01:34:09 +00:00
|
|
|
|
|
2016-01-01 21:08:39 +00:00
|
|
|
|
For more examples of referencing these templates, see
|
|
|
|
|
[single content templates](/templates/content/),
|
|
|
|
|
[list templates](/templates/list/) and
|
|
|
|
|
[homepage templates](/templates/homepage/).
|
|
|
|
|
|
|
|
|
|
|
2016-12-14 02:37:34 +00:00
|
|
|
|
## Variable scoping
|
2016-01-01 21:08:39 +00:00
|
|
|
|
|
2016-08-28 13:21:19 +00:00
|
|
|
|
As you might have noticed, `partial` calls receive two parameters.
|
2016-01-01 21:08:39 +00:00
|
|
|
|
|
|
|
|
|
1. The first is the name of the partial and determines the file
|
|
|
|
|
location to be read.
|
|
|
|
|
2. The second is the variables to be passed down to the partial.
|
|
|
|
|
|
2016-04-15 03:05:24 +00:00
|
|
|
|
This means that the partial will _only_ be able to access those variables. It is
|
2016-01-01 21:08:39 +00:00
|
|
|
|
isolated and has no access to the outer scope. From within the
|
|
|
|
|
partial, `$.Var` is equivalent to `.Var`
|
2016-12-14 02:37:34 +00:00
|
|
|
|
|
|
|
|
|
## Cached Partials
|
|
|
|
|
|
|
|
|
|
The `partialCached` template function can offer significant performance gains
|
|
|
|
|
for complex templates that don't need to be rerendered upon every invocation.
|
|
|
|
|
The simplest usage is as follows:
|
|
|
|
|
|
|
|
|
|
{{ partialCached "footer.html" . }}
|
|
|
|
|
|
|
|
|
|
You can also pass additional parameters to `partialCached` to create *variants* of the cached partial.
|
|
|
|
|
For example, say you have a complex partial that should be identical when rendered for pages within the same section.
|
|
|
|
|
You could use a variant based upon section so that the partial is only rendered once per section:
|
|
|
|
|
|
|
|
|
|
{{ partialCached "footer.html" . .Section }}
|
|
|
|
|
|
|
|
|
|
If you need to pass additional parameters to create unique variants,
|
|
|
|
|
you can pass as many variant parameters as you need:
|
|
|
|
|
|
|
|
|
|
{{ partialCached "footer.html" . .Params.country .Params.province }}
|
|
|
|
|
|
|
|
|
|
Note that the variant parameters are not made available to the underlying partial template.
|
|
|
|
|
They are only use to create a unique cache key.
|