go-bindata-html-template  

A while back, I developed a Go web app on Google App Engine, and naturally I used html/templates to render web pages.

In production, ParseFiles calls were failing, and I’m still confused whether reading from the filesystem is allowed and reasonably performant on GAE. I also have some other Go projects that run a web server, and I’d like to ship only a binary, not extra template files.

I’m using go-bindata to bundle my templates with my code. In doing so, I found I had to keep writing error checking code like:

tmplBytes, err := Asset("templates/my_tmpl.tmpl")
if err != nil {
  return err
}
tmpl, err := template.New("tmpl").Parse(string(tmplBytes))
if err != nil {
  return err
}
//...

So, I decided to write a small library with a large (but logical) name to help: go-bindata-html-template.

The library reduces the above code to:

import "github.com/arschles/go-bindata-html-template"

tmpl, err := template.New("tmpl", Asset).ParseFiles("templates/my_tmpl.tmpl")
if err != nil {
  return err
}

go-bindata-html-template implements a subset of the html/template API, but it works for all of my needs and it dropped into my existing project. It’s also just a matter of adding a func if more coverage is needed. I encourage you to try it out, and let me know what you think: https://github.com/arschles/go-bindata-html-template.

 
3
Kudos
 
3
Kudos

Now read this

Seeking Contributors for Go In 5 Minutes

Summary: I’m looking for contributors to do part or all of a screencast for Go in 5 Minutes. Email me at arschles+gifm@gmail.com if you’re interested. A few months ago, I decided to put together Go In 5 Minutes, which is a series of 5... Continue →