Mustache, ERB and the future of templating
warpspire.com
warpspire.com
ERB and <%= friends %> are great if you have some blob of text and need to sub in some variables.
Markdown is great if you've got a prose document, that want rendered nicely.
You can combine to form erb.md if you need a document with a dynamically filled in field or two.
If you're writing a lot of HTML, then I happen to quite like HAML, since it's a lot less verbose.
However, once you start writing ERB or HAML, or whatever, you're greatly reducing the involvement that designers can have in the process. If you're working on a traditional web site, that's potentially a bad thing. However, if you're making a richer web app, it might actually be a good thing to preclude non-programmers from interfering.
But let's say you have a more traditional web site & have designers on staff who you want to be able to tweak and preview in their browsers. Well, for one thing, it's a lot of work to keep that working. Since even the presence of a single master layout will necessitate some sort of tool to host the rendered page for you... so I'm not sure I understand the benefits of Mustache's HTML-compatibility in that case.
Regarding "power" and "expressiveness" in templates: If you have immutable objects (or at least treat them as such) and you pass data structures, sans methods (such as in a functional language), then you really can't do much harm with those objects alone. It's once you `require 'models'` from your templates, be it explicitly, or implicitly via `template_arg.do_some_query` that bad things start to happen in terms of separation of concerns.
For me, I want to do things like validate that every single `input` element has a valid `id` tag. The fact that I have one syntax for partials and one syntax for elements and another syntax for form fields is confusing to me. Why don't I just make an `element` function and then define an `input` function and then `textbox` function and then a `layout` function and then a `button` function? Consistency and simplicity is beautiful.
Unless I write `require bigscarylibrary` at the top, or pass an unholy (ie. all) ORM object to my views, what could go wrong?
....wow.... I think I'm turning into a curmudgeonly lisper....
This is why I'm digging slim (http://slim-lang.com). It's pretty much html w/o the closing tags and brackets, and also like haml but cleaner looking.
My quest now is to actually push all the rendering logic to the client. The project I am currently working on is already very heavy on javascript, so there is no way to do "graceful degradation" for browsers without javascript enabled. Ideally, my server should just respond with JSON (or XML), and the client takes the response and renders the HTML.
So far, the biggest pain with this is to debug a template. Using the jquery tmpl plugin, you just get a blank screen if your template is wrong.
If someone knows of a good js templating language that offers some decent debugging functionality, I'm all ears.
- the selector is wrong, you get an exception - the data you want to read is not in the JSON, eg: an input value is blank - there is an error in a JS function, you can debug it like any JS code
pure.js use only CSS selectors, HTML and JS/JSON.
[1] http://blog.rassemblr.com/2011/05/on-client-side-templating-...
[2] http://blog.rassemblr.com/2011/04/on-client-side-templating/
[3] backbone http://documentcloud.github.com/backbone/
[4] sammy http://sammyjs.org/
I guess the idea behind Mustache and other similar minimal templating engines is to force the discipline that everyone knows they should have. Personally, I think the claim that designers should be able to write templates with zero programming knowledge is a pipe dream; Mustache still has loops and conditionals (how those don't count as "logic" is beyond me) and you'll almost always have to use them.
This is a bit esoteric, because conditionals and loops are what most programmers are familiar with. I think the big difference for me is that it's not really a conditional or a loop but a context.
So when I say {{#var}} I'm saying within this context, output this partial — that partial can be a separate file or an inline HTML snippet. If that context doesn't exist, it doesn't get rendered (conditional). If that context is a collection of items, it renders each one (loop).
So, yeah. It's easier to say that they're conditionals and loops since that's what programmers are familiar with. But if you start thinking a bit different, you can see how there's really only one mode for Mustache: outputting stuff. You can't filter the output. You can't output parts of it. You can only output it or not.
If you spend some time with liquid (a templating language with logic, but with extremely similar syntax as mustache) you'll notice the difference. It's subtle, but (I think) important.
http://www.javarants.com/2011/10/09/new-features-and-extensi...
All that though is overshadowed by how easy it is to understand the templates and collaborate on them with designers, front-end and back-end developers.
http://code.google.com/webtoolkit/doc/latest/DevGuideUiBinde...
Takes awhile to get used to, but it's nice to have all your logic in the controller, once you're used to it.
Plus being only-static HTML, ui.xml files are amenable to code-generation, e.g. making interfaces for the views (which ends up similar to your controller view classes). Java, XML, and build-time code generation are terribly un-hip, I know, but personally something I've grown to like (I have an open source framework, gwtmpv.org based around these ideas).
As far as most-pure templating language, I think that goes to the appropriately named "pure", which lacks any notion of it's own special characters in the markup:
I haven't used it, so am not entirely sure how well it ends up working in a real-world apps/templates, but it initially seems pretty slick.
But they aren't so great when it comes time to get work done. You shouldn't be fighting the language. The language should enable you to do what you need, cleanly, without getting in your way.
Wow, it makes me wonder what you were doing before?
I'm more a fan of http://www.handlebarsjs.com/. It has a few handy features that are missing from Mustache.
Try KnockoutJS, just for one page, and you'll see how incredibly powerful and time-saving it can be. Your clients have just as much CPU power in their phones as you do in your app servers; let them handle the view rendering. Knockout is beautiful, simple, and extremely elegant.
I did mustache style variable interpolation (but you can stick whatever js you want in it) because it was easy to implement. I just realized that this style doesn't allow the insertion of javascript loops, but it would be compatible with mustache and make for a much better implementation - you also wouldn't need to close your mustache loop just like you don't need to close your tag.