4 ms·
Templating in Go
- chimeracoder 12y agoOne 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/master/handlers.go#L63 https://github.com/ChimeraCoder/go-angular-jetpack/blob/mast...
- TomNomNom 12y agoRob 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 12y agoThis 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 12y agoCan 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.
- troyk 12y agoDoesn'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> .
- landr0id 12y agoThe 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 12y agoGo 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-templates https://sites.google.com/site/usfcomputerscience/django-temp... 2. http://stackoverflow.com/questions/11467731/is-it-possible-to-have-nested-templates-in-go-using-the-standard-library-googl http://stackoverflow.com/questions/11467731/is-it-possible-t...
- bayesianhorse 12y agoTemplating was actually the one thing that made me give up the last time I tried to get started with Go...
- EugeneOZ 12y agoFor 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).
- robfig 12y agoHaving 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)