This commit also adds a new command, docshelper, with some utility funcs that adds a JSON datafiles to /docs/data that would be a pain to create and maintain by hand. Fixes #3242
4.2 KiB
date | menu | prev | next | title | weight | ||||
---|---|---|---|---|---|---|---|---|---|
2016-03-29T21:26:20-05:00 |
|
/templates/views/ | /templates/partials/ | Block Templates | 80 |
The block
keyword in Go templates allows you to define the outer shell of your pages one or more master template(s), filling in or overriding portions as necessary.
Base template lookup
In version 0.20
Hugo introduced custom [Output Formats]({{< relref "extras/output-formats.md" >}}), all of which can have their own templates that also can use a base template if needed.
This introduced two new terms relevant in the lookup of the templates, the media type's Suffix
and the output format's Name
.
Given the above, Hugo tries to use the most specific base tamplate it finds:
- /layouts/current-path/template-name-baseof.[output-format].[suffix], e.g. list-baseof.amp.html.
- /layouts/current-path/template-name-baseof.[suffix], e.g. list-baseof.html.
- /layouts/current-path/baseof.[output-format].[suffix], e.g baseof.amp.html
- /layouts/current-path/baseof.[suffix], e.g baseof.html
- /layouts/_default/template-name-baseof.[output-format].[suffix] e.g. list-baseof.amp.html.
- /layouts/_default/template-name-baseof.[suffix], e.g. list-baseof.html.
- /layouts/_default/baseof.[output-format].[suffix]
- /layouts/_default/baseof.[suffix]
For each of the steps above, it will first look in the project, then, if theme is set, in the theme's layouts folder. Hugo picks the first base template found.
As an example, with a site using the theme exampletheme
, when rendering the section list for the section post
for the output format Calendar
. Hugo picks the section/post.calendar.ics
as the template and this template has a define
section that indicates it needs a base template. This is then the lookup order:
/layouts/section/post-baseof.calendar.ics
/layouts/section/post-baseof.ics
/themes/exampletheme/layouts/section/post-baseof.calendar.ics
/themes/exampletheme/layouts/section/post-baseof.ics
/layouts/section/baseof.calendar.ics
/layouts/section/baseof.ics
/themes/exampletheme/layouts/section/baseof.calendar.ics
/themes/exampletheme/layouts/section/baseof.ics
/layouts/_default/post-baseof.calendar.ics
/layouts/_default/post-baseof.ics
/themes/exampletheme/layouts/_default/post-baseof.calendar.ics
/themes/exampletheme/layouts/_default/post-baseof.ics
/layouts/_default/baseof.calendar.ics
/layouts/_default/baseof.ics
/themes/exampletheme/layouts/_default/baseof.calendar.ics
/themes/exampletheme/layouts/_default/baseof.ics
Define the base template
Let's define a simple base template (_default/baseof.html
), a shell from which all our pages will start.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{ block "title" . }}
<!-- Blocks may include default content. -->
{{ .Site.Title }}
{{ end }}</title>
</head>
<body>
<!-- Code that all your templates share, like a header -->
{{ block "main" . }}
<!-- The part of the page that begins to differ between templates -->
{{ end }}
<!-- More shared code, perhaps a footer -->
</body>
</html>
Overriding the base
Your [default list template]({{< relref "templates/list.md" >}}) (_default/list.html
) will inherit all of the code defined in the base template. It could then implement its own "main" block from the base template above like so:
<!-- Note the lack of Go's context "dot" when defining blocks -->
{{ define "main" }}
<h1>Posts</h1>
{{ range .Data.Pages }}
<article>
<h2>{{ .Title }}</h2>
{{ .Content }}
</article>
{{ end }}
{{ end }}
This replaces the contents of our (basically empty) "main" block with something useful for the list template. In this case, we didn't define a "title" block so the contents from our base template remain unchanged in lists.
In our [default single template]({{< relref "templates/content.md" >}}) (_default/single.html
), let's implement both blocks:
{{ define "title" }}
{{ .Title }} – {{ .Site.Title }}
{{ end }}
{{ define "main" }}
<h1>{{ .Title }}</h1>
{{ .Content }}
{{ end }}
This overrides both block areas from the base template with code unique to our single template.