6 ms·
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 gotcha
by piotrkubisa 9y ago
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 9y agoTotally 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 9y agoThat'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.