6c2195936 Update featured.png 109a0fcca add len function to navigation side menu 39a356bc5 Revert "Add some rickrolls redirects" b8393b1b5 Add some rickrolls redirects 2ce21c34b Update configuration-markup.md (add rel="noopener") 95bd7974e Disambiguate global and page resources 5e233dc4b Update base.md 959b9dc3a Fix typo on "where" page aff8059a1 Release 0.70.0 44a172ac0 releaser: Add release notes to /docs for release of 0.70.0 1b01c8988 Release 0.70.0 5ece21c6c Merge commit '89044b8f8795f17c36396c67823183a20fc88139' 0894aec5b Rename transpileJS to babel 5da27c7a6 resources: Add JavaScript transpiling solution git-subtree-dir: docs git-subtree-split: 6c21959360394165435fa36eac489bf6a701ae9a
3.7 KiB
title | linktitle | description | godocref | date | publishdate | lastmod | categories | keywords | menu | weight | sections_weight | draft | aliases | toc | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Base Templates and Blocks | The base and block constructs allow you to define the outer shell of your master templates (i.e., the chrome of the page). | https://golang.org/pkg/text/template/#example_Template_block | 2017-02-01 | 2017-02-01 | 2017-02-01 |
|
|
|
20 | 20 | false |
|
true |
The block
keyword allows you to define the outer shell of your pages' one or more master template(s) and then fill in or override portions as necessary.
{{< youtube QVOMCYitLEc >}}
Base Template Lookup Order
{{< new-in "0.63.0" >}} Since Hugo v0.63, the base template lookup order closely follows that of the template it applies to (e.g. _default/list.html
).
See Template Lookup Order for details and examples.
Define the Base Template
The following defines a simple base template at _default/baseof.html
. As a default template, it is the shell from which all your pages will be rendered unless you specify another *baseof.html
closer to the beginning of the lookup order.
{{< code file="layouts/_default/baseof.html" download="baseof.html" >}}
<html> <head> </head> {{ block "main" . }} {{ end }} {{ block "footer" . }} {{ end }} </html> {{< /code >}}Override the Base Template
From the above base template, you can define a default list template. The default list template will inherit all of the code defined above and can then implement its own "main"
block from:
{{< code file="layouts/_default/list.html" download="list.html" >}} {{ define "main" }}
Posts
{{ range .Pages }}{{ .Title }}
{{ .Content }}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.
{{% warning %}} Code that you put outside the block definitions can break your layout. This even includes HTML comments. For example:
<!-- Seemingly harmless HTML comment..that will break your layout at build -->
{{ define "main" }}
...your code here
{{ end }}
See this thread from the Hugo discussion forums. {{% /warning %}}
The following shows how you can override both the "main"
and "title"
block areas from the base template with code unique to your default single page template:
{{< code file="layouts/_default/single.html" download="single.html" >}} {{ define "title" }}
{{ .Title }} – {{ .Site.Title }} {{ end }} {{ define "main" }}