

Templating in Go - lettergram
http://austingwalters.com/templating-in-go/

======
troyk
Doesn't mention imho the best feature of Go html templating, from the docs
([http://golang.org/pkg/html/template/](http://golang.org/pkg/html/template/)):

 _This package understands HTML, CSS, JavaScript, and URIs. It adds sanitizing
functions to each simple action pipeline, so given the excerpt

    
    
      <a href="/search?q={{.}}">{{.}}</a>
    

At parse time each {{.}} is overwritten to add escaping functions as
necessary. In this case it becomes

    
    
      <a href="/search?q={{. | urlquery}}">{{. | html}}</a>
    

._

------
TomNomNom
Rob Pike did a really good talk entitled 'Lexical Scanning In Go'[0], which
covers how the lexing for text/template works. It's far more interesting than
it sounds, and even if you don't know much about lexing/parsing it's easy to
follow.

[0]
[https://www.youtube.com/watch?v=HxaD_trXwRE](https://www.youtube.com/watch?v=HxaD_trXwRE)

------
tyleregeto
This hardly even talks about the templating package in Go, which is really
good. They have found a nice balance between ease of use and functionality.

It reads a lot like mustache, but has the power of a language that allows you
to mix markup and code. Its worth checking out in more detail.

~~~
zippergz
Can you recommend a good document explaining all the features? The main
package doc on golang.org seems to not really go into stuff like conditionals,
loops, etc. which I gather are possible.

~~~
hedgehog
That is documented in text/template.

~~~
zippergz
Wow. I feel like an idiot for not finding that. Thank you!

------
landr0id
The main thing I hate about Go's template package is that it makes it
difficult to define base templates without adding the base file to your
ParseFiles arguments. I want to be able to say that a template extends another
from within that template, not the code.

~~~
unoti
Go doesn't offer a clear simple way to do this[1] in their builtin template
library. But it turns out, it really is possible to do it, as described in
this thread on Stack Exchange[2]. I bookmarked that so that I can boil it down
into some simple-to-reuse code if I ever bite the bullet and start using Go
for the presentation layer on a major website. What they're doing there is a
cool technique, but it's not simple to use out of the box.

1\. [https://sites.google.com/site/usfcomputerscience/django-
temp...](https://sites.google.com/site/usfcomputerscience/django-templates)

2\. [http://stackoverflow.com/questions/11467731/is-it-
possible-t...](http://stackoverflow.com/questions/11467731/is-it-possible-to-
have-nested-templates-in-go-using-the-standard-library-googl)

------
robfig
Having implemented significant projects in each, I greatly prefer the soy
template language.
[http://godoc.org/github.com/robfig/soy](http://godoc.org/github.com/robfig/soy)

(plug for own library)

------
bayesianhorse
Templating was actually the one thing that made me give up the last time I
tried to get started with Go...

~~~
EugeneOZ
For those who use Go for web, it's a good chance to not use templating at all,
and try html + js-ajax + json (from Go backend).

------
chimeracoder
One subtle feature of Go's templating that I really appreciate is that it's
possible (and easy!) to change the template delimiters[0].

This is really useful when dealing with systems like Angular.js, which also
use "{{foo}}" as delimiters for injecting directives.

[0] [https://github.com/ChimeraCoder/go-angular-
jetpack/blob/mast...](https://github.com/ChimeraCoder/go-angular-
jetpack/blob/master/handlers.go#L63)

