jade: a node.js template engine
jade-lang.com
jade-lang.com
http://jsperf.com/dom-vs-innerhtml-based-templating/45
Here's a screenshot of the results: http://cl.ly/42CP
It can make a difference, because UI-rendering can often be the performance bottleneck in a JS web app ... especially in older browsers like Internet Explorer.
Although, perhaps it isn't a problem at all -- looking at the source, it doesn't appear as though Jade supports IE:
https://github.com/visionmedia/jade/blob/master/lib/jade.js#...
I use this one with a js-app in conjunction with knockout.js and a few others.
So performance of this one would be interesting, too.
Btw: The actual benchmark is really compelling.
Should be plenty fast enough for all but the most demanding use-cases.
http://jsperf.com/dom-vs-innerhtml-based-templating/46
It performs really poor..
Also it's just beta 1, lets see what happens next.
But adding Eco Template as engine for knockout.js would be a great idea, i think...
[1]: http://jsperf.com/dom-vs-innerhtml-based-templating/47 [2]: http://cl.ly/3002090Y093I383N013r
- Clean, nice HAML-like syntax. Expect less typing and easier reading.
- Extensible. It's extremly easy to provide your own filters, doctypes.
- Extremely cool ability to hook the compiler and manipulate the result tree. See CSRF example — it's just awesome.
- Command-line tool to compile templates. A must for static mock-ups.
- High quality code. I'd really wish I could write code as pretty as that.
Cons:
- No template inheritance. And no fragment inclusion (unless you write your own filter or extend the compiler to support some "jade:include" attribute).
- Parsing and rendering is synchronous. So expect Node to block while doing jade.render().
- Filters are synchronous, too. So are iterators (therefore, forget your idea to write model, which lazily fetches data from DB on demand).
- No client-side support. Not like this is important, but this would be neat, considering it's JS after all.
Jade is awesome, but synchronous code in asynchronous world somehow feels wrong.
Express+jade=win:
On the topic of javascript templates, I tried Jaml for a while because I really wanted to have templates that could be cached on the client-side, but the function-based approach was really cumbersome. I ended up creating a server-side pre-processor that converts Haml to vanilla javascript functions that just build the output using string concatenation. These are called at run-time by underscore's template function. Overall, I'm totally happy with it so far.
1) Not making nesting mistakes
2) Less escaping junk(<? ?> or <% %> or whatever)
3) Generally fewer characters to wade through
4) Not having to think about the fact that HTML files are strings... thinking in terms of DOM structure instead.
The things it doesn't handle as well:
1) Micromanaging whitespace between tags (I know, there are solutions, but they're still awkward)
But in truth, HAML kind of reminds me of Rails in that it doesn't actually DO anything you can't do for yourself... but it does change how you think about/structure your code. I'm pretty sure that if I were to go back to writing raw HTML, I would structure/indent it like a HAML document... the same way my non-Rails apps end up looking a lot like Rails now that I've seen how that works.
- less thinking
- easier to modify and understand
I've been writing longhand for 10 years, and it was a no brainer. Just download it and try it on a project for a day. If you are not sold, you can always go back (since it just generates regular html).
I also highly recommend sass over css. The benefits of sass over css is much greater than the benefits of haml over html.
Haml is Much easier to diff/ merge
I'd like to also recommend Nickolay Platonov's little-known but powerful JS templating library "Shotenjin":
https://github.com/SamuraiJack/Shotenjin
Shotenjin is built with Joose v3, which also rocks:
It's also got a couple extra features, but haml could match it.
Read more here: http://tjholowaychuk.com/post/759178288/jade-haml-killer-for...
Very nice.