What is Hamstache?

Hamstache combines the awesome power of Mustache’s logic-less templates with the clean elegance of the Haml templating language.


Add Hamstache to your Gemfile:

gem "hamstache"

And run bundle install to ensure it’s added to your apps dependencies. Now you can create your view using a mix of Haml and Mustache syntax. For example to serve a request to WelcomeController#index we’d have the following in app/views/welcome/index.html.hamstache:

%h1 Welcome!
%p Here comes a list:
    %li {{ name }}
  {{/ items}}

Next you need to define your Hamstache template, for this example the template is expected to be found at app/hamstaches/demo/index.rb and could look like the following:

class Welcome::Index < Hamstache::Template
  def items
    [{ :name => "Item 1"},
    { :name => "Item 2"},
    { :name => "Item 3"}]

Now a request to WelcomeController#index would result in the following HTML output:

<p>Here comes a list:</p>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>

For more details on what is possible, read up on the API docs for HAML and Mustache respectively.