
Using Go Templates - bketelsen
https://blog.gopheracademy.com/advent-2017/using-go-templates/
======
nishs
An additional tip. Template actions don't necessarily have to be delimited by
'{{' and '}}'.

Before parsing, you can call Delims to use custom delimiters.

    
    
      func (t *Template) Delims(left, right string) *Template
    

[https://golang.org/pkg/text/template/#Template.Delims](https://golang.org/pkg/text/template/#Template.Delims)

~~~
_Chief
Especially useful if, for example, you'll be doing additional js templating
that also use the mustache tag eg with Vuejs (can also be changed)

------
abc-xyz
I really want to love templates, but I find many aspects to be quite
confusing.

(1): I just want to deploy a binary file. This does not seem possible with
templates (unless they're defined inside .go files, but never seen anyone do
that).. also rather annoying that the tmpl files must be located in a specific
directory rather than located among their components.. but I guess it wouldn't
make much sense anyway since they won't be a part of the binary file.

(2): it's really difficult to find good and medium-large scale projects that
are using templates

(3): I have no idea what's the right way to initialize and execute templates,
this includes when the same template will be called in multiple funcs. I know
how I can do it, but I don't feel confident that I'm doing it correctly..

(4): it's really exhausting to keep template code clean. With Go (and various
JS frameworks) the code is automatically formatted/linted/whatever, but with
templates nada.. can't even figure out how to minimize it for production.

(5): no idea what to do with css/js.. sometimes use inline, other times call a
static file. Static files obviously benefit from being cached.. not sure how
I'm supposed to cache the templates

I can't really tell if there are any speed gains to using templates, so it
feels like a lot of sacrifices for no obvious gain. I do think templates would
be godlike if they featured vue-like structure with "template/script/style"
syntax, and was easily minified and compiled into the binary. But I guess I
can keep dreaming about the last part :)

~~~
rmuratov
(2) [https://github.com/gohugoio/hugo](https://github.com/gohugoio/hugo) ?

~~~
abc-xyz
I love Hugo, but I was thinking more in the lines of a "standard" website,
like Hacker News, Twitter, the official gohugo.io website (rather than their
generator), etc.

------
enahs-sf
Really glad someone put this together. The official documentation is pretty
good but somewhat lacking in terms of practical examples around templates.

------
tschellenbach
This Django style template library is also quite slick:
[https://github.com/flosch/pongo2](https://github.com/flosch/pongo2)

------
piotrkubisa
I am also really glad someone has glued toghether major aspects of
template/html std library. It is definetely a place where are some 'taboos',
tricks or gotchas, i.e. re-usability of tmpl static variable to add multiple
named templates and call them by name (re: question on disqus below article)
or sharing parent scope to children variable when using range loop
(stackoverflow popular question). I am bit sad I haven't found some free time
to collect all known 'tricks' to me and try to list them somehow or send CR
yet.

~~~
qrv3w
Totally agree, I'm also glad there is another resource for learning
templating! The gotchas are frequent for me when it comes to templating in Go.
Recently I found that html/template will strip tags when inserting into an
attribute [1]. I did not realize that Go made a special template for
attributes (template.HTMLAttr) which is what I needed to make it work. [2]

[1]:
[https://play.golang.org/p/nHG_F2a0t-4](https://play.golang.org/p/nHG_F2a0t-4)

[2]:
[https://play.golang.org/p/TU5CX9Kstj](https://play.golang.org/p/TU5CX9Kstj)

~~~
hellcow
That's not all! Try inserting a Go variable within a script tag, and Go will
automatically convert it to JSON. Makes it possible to easily do this:

    
    
      let msgs = {{ .Messages }}
    

Where .Messages is any slice. Same is true for structs -> JS objects.

------
dzink
Have you tried to benchmark writing out a page with Go templates, vs GO
producing a JSON API that prints out into static HTML pages with
JQUERY/Handlebars? How many RPS can you get with either scenario? What is Time
to First Paint and when does the page finish printing for the user?

~~~
captncraig
Go templates are not particularly fast, but I would be astonished if the
client-side way one that battle.

There are faster server-side template engines in go though. One approach I
particularly like is
[https://github.com/bouk/statictemplate](https://github.com/bouk/statictemplate),
which generates code from your standard go templates that makes them quite a
bit faster.

------
gkya
Seeing this from the front page RSS feed I opened the comments page in shock
thinking somehow Golang got templates a la C++...

------
tonetheman
Gopher Academy has been doing great content lately. Good examples for
templates past the real docs. Good stuff.

