Thursday, November 28, 2013

Go templates and partials

I was looking around to see how people usually implement partial templates with the standard go templating library.

The usual pattern is to create a layout template which includes a content template, and then each partial template starts with

{{define "content"}}
blah blah

As described in this excellent post the Go template library uses the file base name as template name, so it's very tempting to just use a directive inside the partial template itself to force a particular name.

But I find this boilerplate in each partial very annoying, so I found this work around: