3 ms·
While I agree in theory, I can't stand that in practice :). For example, let's say I want to show a usage stat, I'd pass something like { usage: 100, limit: 10
by eknkc 10y ago
While I agree in theory, I can't stand that in practice :).
For example, let's say I want to show a usage stat, I'd pass something like { usage: 100, limit: 1000 } to the template engine. Let's say I want to add a percentage. I believe the calculation should be on the presentation layer. That's how I decide to present this particular data.
Go templates lack that functionality for no good reason. And if I'm gonna calculate that on the backend, why exactly am I using a template engine? I'd concatenate strings.
- candiodari 10y agoActually you can do function calls on Go templates [1], and indeed this is what is done in practice. Libraries got written to do this, and badly integrated [2]. You will quickly find if you use this that it's in fact worse: go templates have LISP syntax. A set of atoms, specified in reverse Polish notation, doing function application to all but the first atom. [1] https://golang.org/pkg/text/template/#Template.Funcs https://golang.org/pkg/text/template/#Template.Funcs [2] https://github.com/Masterminds/sprig https://github.com/Masterminds/sprig
- eknkc 10y agoYeah this is the reason I went ahead and wrote the template engine I mentioned in the parent comment. It compiles down to go templates. I inject generic math functions [1] and convert an expression like 4 + 5 * 3 to go template function calls. It's ugly but the compiler generates it so whatever :) [1] https://github.com/eknkc/amber/blob/master/runtime.go https://github.com/eknkc/amber/blob/master/runtime.go