mustache_wax

Automatically compiles *.html.mustache view files in app/views to /javascripts/mustache_templates.js so that you can easily access them from javascript.

Installation with bundler

In your Gemfile:

gem 'mustache_wax'

Then

$ bundle install

in your application layout:

<%= javascript_include_tag 'mustache_templates' %>

If you are using Rails 3, it should automatically share the rake task (as long as you’re allowing bundler to require the gem, like above), if you’re using an older version of Rails, or don’t want to require the gem in Gemfile, then edit your Rakefile and add:

Dir["#{Gem.searcher.find('mustache_wax').full_gem_path}/lib/tasks/*.rake"].each { |ext| load ext }

If you are using capistrano for deployment, in your deploy.rb, add:

require 'mustache_wax/capistrano'

This will automatically compile your .html.mustache files on deploy.

If you are not using capistrano, you’ll need to modify your deploy script to run:

rake mustache:generate_templates

Usage

Anywhere in javascript, you can now use the mustache_templates variable to reference any template.

If you have a mustache view file named app/views/users/user_profile.html.mustache, you can now reference it in javascript using:

mustache_templates['users/user_profile']

eg:

// render the user object with the template
var html = Mustache.to_html(mustache_templates['users/user_profile'], user_object);

Compiling in development

You can trigger this manually with “rake mustache:generate_templates”, but that shouldn’t be necessary. Rails 2 & 3 will automatically recompile the templates on server start. With passenger you’ll need to touch tmp/restart.txt, or touch tmp/always_restart.txt to always restart on every request; it’s slower, but perhaps useful if you’re doing a lot of template changes.

Contributing to mustache_wax

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • write specs to describe the bug or feature. This is important so I don’t break it in a future version unintentionally.

  • get specs passing

  • Commit with a message that explains why the change was necessary, not just what was changed.

  • push and send me a pull request

  • Please do not to mess with the Rakefile, or version.

Copyright © 2011 Steven Soroka. See LICENSE.txt for further details.