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

Going from Java to Scala

Scala, in my opinion, is a wonderful language. I have refrained from saying that publicly until after the “honeymoon period” was over between me and it. Now that it is, I feel comfortable with that statement. Perhaps the greatest feature... Continue →