This generator and template handler for Mustache in Rails 3 is based on the work of Paul Barry, Louis T., and Martin Gamsjaeger. I am indebted to them for allowing me to stand on their shoulders.
This is also available as a rubygem.
I'm just getting started. This really is a low-numbered prerelease. :-) I have asked for comments on the mustache project's Rails Support issue ticket. Please leave feedback there, and thanks.
For your view files, subclass Mustache::Rails as (:controller)::(:action) in app/views/:controller/:action.rb
#app/views/home/index.rb
class Home::Index < Mustache::Rails
def world
'New Caprica'
end
end
Mustache::Rails registers a TemplateHandler for ".rb" files. Templates go in app/templates/:controller/:action.format.mustache
#app/templates/home/index.html.mustache
Hello {{world}}!
Layouts work much the same way, using a similar naming convention. Subclass Mustache::Rails as Layouts::(:layout) in app/views/layouts/:layout.rb
#app/views/layouts/main.rb
class Layouts::Main < Mustache::Rails
def default_title
'A Cylon fleet has jumped into orbit!'
end
end
Place the template for your layout in app/templates/layouts/:layout.format.mustache
#app/templates/layouts/main.html.mustache
<h1>{{default_title}}</h1>
{{{yield}}}
A Rails 3 reminder: be sure to add
gem 'mustache'
to your project's Gemfile before running any generators or starting the server.
If you're using the mustache_rails3 gem, be sure to also add
gem 'mustache_rails3'
You can enable the mustache template handler by running
rails g mustache:install
in your project directory.
- Add controller-retrofit generator to build default mustache views for existing controllers
- Generate different fields for different attribute types
- Add support for easy conversion of Rails::Mustache objects to JSON representations
- Think about allowing to overwrite layout methods in subclassing views: http://github.com/defunkt/mustache/blob/master/lib/mustache/sinatra.rb#L79-82 http://github.com/defunkt/mustache/blob/master/lib/mustache/sinatra.rb#L96-102