7 ms·
Using Go Templates
- enahs-sf 9y agoReally glad someone put this together. The official documentation is pretty good but somewhat lacking in terms of practical examples around templates.
- nishs 9y agoAn 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
- mdaniel 9y agoYou've given me a fantastic Christmas present! Now I just have to lobby the projects which use the defaults to make the delims configurable, saving their users keystrokes and horrible "how do I escape golang template delims" anxiety
- _Chief 9y agoEspecially useful if, for example, you'll be doing additional js templating that also use the mustache tag eg with Vuejs (can also be changed)
- tschellenbach 9y agoThis Django style template library is also quite slick: https://github.com/flosch/pongo2 https://github.com/flosch/pongo2
- piotrkubisa 9y agoI 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.
- dzink 9y agoHave 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 9y agoGo 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.
- abc-xyz 9y agoI 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 :)
- Rapzid 9y agoYou might want to look into resource packers. I believe a few projects will pack files into the binary in a fashion something akin to resources in .Net/Java. Go-bindata is one such project; that or something else may suit your needs.
- deleted 9y ago[deleted]
- martinsb 9y agoRegarding (1), os.Executable function introduced relatively recently could make things better for you. https://golang.org/pkg/os/#Executable https://golang.org/pkg/os/#Executable
- rmuratov 9y ago
- tonetheman 9y agoGopher Academy has been doing great content lately. Good examples for templates past the real docs. Good stuff.
- deleted 9y ago[deleted]
- gkya 9y agoSeeing this from the front page RSS feed I opened the comments page in shock thinking somehow Golang got templates a la C++...