Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Mustache: Logic-Free Views For Your Ruby Web Apps

By Ric Roberts / October 7, 2009

mustacheMustache 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

CodebaseLogo-RI.png[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.

Comments

  1. Jerod Santo says:

    How easy is it to swap out ERB and use Mustache in Rails?

  2. eugegim says:

    Very cool! I like keeping as much logic out of the views as possible

  3. Chris says:

    @Jerod It's not there yet, but we're making progress. Here's a comparison of the two though: http://gist.github.com/204222

  4. Jerod Santo says:

    @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.

  5. Vojto says:

    Won't beat HAML. HAML is the best ERB replacement.

  6. cies says:

    @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

  7. grimen says:

    Did I miss something, or is this not very compatible with I18n out-of-box (all translations in one place)? Hmm.... any ideas?

  8. andhapp says:

    Are we going back to Classic ASP with a code behind page(doing most of the processing) and the asp page displaying it.

  9. grimen says:

    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...

  10. Peter Cooper says:

    andhapp: What's old is new again? :)

  11. Chris says:

    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!

  12. peterlih says:

    @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 ...

  13. Michael says:

    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?

  14. Nick Quaranto says:

    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.

  15. Guoliang Cao says:

    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}}

  16. Guoliang Cao says:

    My code sample using < and >

    {{<in_ca}}
    Well, ${{taxed_value}}, after taxes.
    {{%gt;in_ca}}

  17. Guoliang Cao says:

    Sorry, messed up again
    ===============================
    My code sample using < and >

    {{<in_ca}}
    Well, ${{taxed_value}}, after taxes.
    {{>in_ca}}

  18. Chris says:

    @Guoliang We plan to make all the syntax customizable in the future.

  19. Juvenn Woo says:

    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.

  20. Dylan says:

    @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.

Other Posts to Enjoy

Twitter Mentions