During the process of learning Golang templates, certain aspects were confusing to me. The goal of this little repo is to document what I eventually wound up doing. Hopefully with feedback this repo could serve as an example of at least one way to use templates effectively.
By no means is this intended to be a proper (or even correct) howto on Golang templates, rather it's just what I've learned so far. Here's what I was trying to accomplish:
- Have a directory of templates (
header.html
,foobar.html
, etc). - Have a directory of static files (css, images, etc).
- Use some templates as full pages (
about.html
,hello.html
). - Use some templates as partials (
header.html
,footer.html
). - Serve static content in a manner similar to http.FileServer.
- Exclude templates from the static files being served.
- Support custom template functions.
- Compile everything into a single static binary (including templates and static files).
go get github.com/GeertJohan/go.rice/rice
go get -d github.com/jmcfarlane/golang-templates-example
cd $GOPATH/src/github.com/jmcfarlane/golang-templates-example
go get -t ./...
go generate
go test -v
go build
./golang-templates-example
curl http://localhost:8080
If you have make
installed, you can try this repo by doing:
make
./golang-templates-example