2013-08-17 08:34:25 -04:00
|
|
|
---
|
|
|
|
title: "Go Templates"
|
|
|
|
date: "2013-07-01"
|
2013-10-31 09:51:13 -04:00
|
|
|
groups: ["layout"]
|
2013-10-26 02:18:14 -04:00
|
|
|
groups_weight: 80
|
2013-10-31 09:51:13 -04:00
|
|
|
draft: true
|
2013-08-17 08:34:25 -04:00
|
|
|
---
|
|
|
|
|
2013-11-18 18:31:02 -05:00
|
|
|
Hugo uses the excellent [golang][] [html/template][gohtmltemplate] library for
|
|
|
|
its template engine.
|
2013-08-17 08:34:25 -04:00
|
|
|
It is an extremely lightweight engine that provides a very small amount of
|
2013-11-18 18:31:02 -05:00
|
|
|
logic.
|
|
|
|
In our experience that it is just the right amount of logic to be able to
|
2013-08-17 08:34:25 -04:00
|
|
|
create a good static website.
|
|
|
|
|
2013-11-18 18:31:02 -05:00
|
|
|
This is a brief primer on using go templates. The [golang docs][gohtmltemplate]
|
|
|
|
provide more details.
|
2013-08-17 08:34:25 -04:00
|
|
|
|
2013-11-18 18:31:02 -05:00
|
|
|
In your top-level configuration file (eg, `config.yaml`) you can define site
|
|
|
|
parameters, which are values which will be available to you in chrome.
|
2013-08-17 08:34:25 -04:00
|
|
|
|
2013-11-18 18:31:02 -05:00
|
|
|
For instance, you might declare:
|
2013-08-17 08:34:25 -04:00
|
|
|
|
2013-11-18 18:31:02 -05:00
|
|
|
```yaml
|
|
|
|
params:
|
|
|
|
CopyrightHTML: "Copyright © 2013 John Doe. All Rights Reserved."
|
|
|
|
TwitterUser: "spf13"
|
|
|
|
SidebarRecentLimit: 5
|
|
|
|
```
|
|
|
|
|
|
|
|
Within a footer layout, you might then declare a `<footer>` which is only
|
|
|
|
provided if the `CopyrightHTML` parameter is provided, and if it is given,
|
|
|
|
you would declare it to be HTML-safe, so that the HTML entity is not escaped
|
|
|
|
again. This would let you easily update just your top-level config file each
|
|
|
|
January 1st, instead of hunting through your templates.
|
|
|
|
|
|
|
|
```
|
|
|
|
{{if .Site.Params.CopyrightHTML}}<footer>
|
|
|
|
<div class="text-center">{{.Site.Params.CopyrightHTML | safeHtml}}</div>
|
|
|
|
</footer>{{end}}
|
|
|
|
```
|
|
|
|
|
|
|
|
An alternative way of writing the "if" and then referencing the same value is
|
|
|
|
to use "with" instead, which rebinds the pointer `.` within its scope, and
|
|
|
|
elides the scope if the variable is absent:
|
|
|
|
|
|
|
|
```
|
|
|
|
{{with .Site.Params.TwitterUser}}<span class="twitter">
|
|
|
|
<a href="https://twitter.com/{{.}}" rel="author">
|
|
|
|
<img src="/images/twitter.png" width="48" height="48" title="Twitter: {{.}}"
|
|
|
|
alt="Twitter"></a>
|
|
|
|
</span>{{end}}
|
|
|
|
```
|
|
|
|
|
|
|
|
Finally, if you want to pull "magic constants" out of your layouts, you can do
|
|
|
|
so, such as in this example:
|
|
|
|
|
|
|
|
```
|
|
|
|
<nav class="recent">
|
|
|
|
<h1>Recent Posts</h1>
|
|
|
|
<ul>{{range first .Site.Params.SidebarRecentLimit .Site.Recent}}
|
|
|
|
<li><a href="{{.RelPermalink}}">{{.Title}}</a></li>
|
|
|
|
{{end}}</ul>
|
|
|
|
</nav>
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
[golang]: <http://golang.org/>
|
|
|
|
[gohtmltemplate]: <http://golang.org/pkg/html/template/>
|