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.

 
4
Kudos
 
4
Kudos

Now read this

Orthogonality in Go

I wrote briefly about Orthogonality at the end of my last post but it’s an important topic so I’m giving it its own post. Also, I seriously can never remember this word so I hope after this post I’ll be cured. What It Is # Orthogonality... Continue →