Mustache: Logic-Free Views For Your Ruby Web Apps
Mustache is a new templating library from Chris Wanstrath (a.k.a. defunkt of GitHub fame) that provides a way to render views in your chosen Ruby web framework. Influenced by ctemplate, Mustache helps to keep your MVC layers separate by actively preventing the inclusion of application logic in your views.
With Mustache, the typical view layer is split up into two sub-layers: a Ruby Class-based "view" and an HTML "template", so you can concentrate on crafting your HTML without polluting it with embedded Ruby logic. This separation also makes it easier to test your view-code.
Amusingly, the name of the project comes from the way that you insert references to the view class into the template. Tags are denoted by double curly braces, or "mustaches". e.g. {{name}}
.
In their simplest form, tags are just calls to methods on your Ruby view class. But they can also take more complex forms such as block definitions, calls to partials or helpers (from modules included into your view class).
There are more examples and documentation in the GitHub project, but here's a canonical usage example from the README:
View Logic:
class Simple < Mustache def name "Chris" end def value 10_000 end def taxed_value value - (value * 0.4) end def in_ca true end end
Template:
Hello {{name}} You have just won ${{value}}! {{#in_ca}} Well, ${{taxed_value}}, after taxes. {{/in_ca}}
At the moment, only Sinatra support is provided "out of the box", but it should be fairly easy to integrate with other Ruby frameworks.
Installation
Mustache is available from Gemcutter or Rip. Install in one of two ways:
$ gem install mustache $ rip install git://github.com/defunkt/mustache.git
[ad] Codebase is a fast & reliable git, mercurial & subversion hosting service with complete project management built-in - ticketing, milestones, wikis & time tracking - all under one roof. Click here to try it - free.
October 7, 2009 at 4:59 pm
How easy is it to swap out ERB and use Mustache in Rails?
October 7, 2009 at 5:03 pm
Very cool! I like keeping as much logic out of the views as possible
October 7, 2009 at 5:09 pm
@Jerod It's not there yet, but we're making progress. Here's a comparison of the two though: http://gist.github.com/204222
October 7, 2009 at 5:50 pm
@Chris: cool, thanks for the comparison. I like Mustache's simplicity, but at this point ERB is so engrained in my brain that I can still grok it faster. That would change with time, of course.
October 7, 2009 at 6:13 pm
Won't beat HAML. HAML is the best ERB replacement.
October 7, 2009 at 6:41 pm
@vojto: true, haml rules. but it allowes you to use ugly^H^H^H dirty^H^H^H dangerous ruby inside your views...
too bad i like ruby, and i like to be allowed to put a little logic in the views -- otherwise i might have liked mustache.
:-{D
October 7, 2009 at 6:49 pm
Did I miss something, or is this not very compatible with I18n out-of-box (all translations in one place)? Hmm.... any ideas?
October 7, 2009 at 6:50 pm
Are we going back to Classic ASP with a code behind page(doing most of the processing) and the asp page displaying it.
October 7, 2009 at 6:51 pm
Hmm...maybe my conclusion was drawn a bi fast. After peepin the gist above maybe it's quite easy to tweak and still keep the views not to messy. Need to sandbox around a bit...
October 7, 2009 at 6:56 pm
andhapp: What's old is new again? :)
October 7, 2009 at 7:48 pm
This is an old idea in many languages - Perl, Java, ASP, etc.
@grimen I'd love to see the results of your experimentation. Reach me on GitHub or Twitter as @defunkt. Thanks!
October 7, 2009 at 9:51 pm
@andhapp Code-Behind in asp.net was my first association as well. But I think it is a pretty useful concept ... Cool thing. I got to try it out ...
October 8, 2009 at 5:24 am
Yep this is JSP custom tags for ruby. I've been sort of doing this with helpers along the way but of course using standard ERB syntax (I've not seen the light of HAML and don't see the win over not being able to have designers work on my pages), to have it output snippets generated by my helpers. After mentioning this to one of my buddies he mentioned that django's templating was the best because you can extend templates and replace content blocks. Is there a ruby/rails equivalent of that yet?
October 8, 2009 at 6:01 am
I really don't see this as ASP.net code-behind at all. That model, especially how it treated HTTP (PostBack? Really?) is fundamentally broken.
I think Mustache could be a great way to really enforce testing for views and make sure they look as neat as possible. Don't get me wrong, I can deal with HAML, and ERB is my wingman, but soon I'll be riding off into the sunset with Mustache.
October 8, 2009 at 1:54 pm
I like the idea. I think it is pretty neat. One of my colleague comes up the idea of using a plain hash for all our web services views, and it worked pretty well.
However, using # and / to open and close tag does not sound great to me. Maybe using instead.
{{in_ca}}
October 8, 2009 at 1:56 pm
My code sample using < and >
{{<in_ca}}
Well, ${{taxed_value}}, after taxes.
{{%gt;in_ca}}
October 8, 2009 at 1:56 pm
Sorry, messed up again
===============================
My code sample using < and >
{{<in_ca}}
Well, ${{taxed_value}}, after taxes.
{{>in_ca}}
October 8, 2009 at 8:18 pm
@Guoliang We plan to make all the syntax customizable in the future.
October 11, 2009 at 5:40 pm
I tried `rip install haml`, but rip complains with an error `undefined method "strip" for nil.NilClass`.
Then I googled `rip install haml`, surprisingly there's a beautiful mustache ... I'm love'in it!
Thanks @defunkt.
October 13, 2009 at 11:09 pm
@Chris: Slick! Do you have any idea how it performs, in comparison with ERB, HAML, Erubis, Pre-Compiled Erubis? If not, let me know and I'd be happy to bench.