HandlebarWax is a gem to let you easily share mustache templates between your server-side and client-side code.
On the server, HandlebarWax uses code borrowed from handlebars-rails to support +..hbs+ as a template type (think foo.html.erb
, foo.html.haml
, foo.html.hbs
). These work pretty much exactly like you would expect they should.
For the client side, HandlebarWax writes all your templates into public/javascripts/handlebars_templates.js
, for you to use in your JavaScript code.
-
Add it to your gemfile.
gem "handlebar_wax"
-
Add it to your layout (it’s important that
:handlebar_wax
be a symbol, not a string)= javascript_include_tag :handlebar_wax
-
Create view files.
(app/views/users/_user.html.hbs) <h1>{{greeting}}, {{user/name}}!</h1>
-
Render views from rails. Note that in MustacheWax, hbs templates understand both assigns and locals.
- @user = User.first = render :partial => 'users/user', :locals => {:greeting => "Hello"}
-
Render views from JavaScript
HandlebarWax('users/_user', {user: {name: "Why"}, greeting: "Frabjous Day"})
-
Steven Soroka (ssoroka) for pub-based discussion about this, and initial mustache_wax code
-
Yehuda Katz and Jamesarosen for handlebars-rails, which this project includes in its entirety, with very few modifications as of yet.
-
Yehuda Katz (yes, twice) for the wonderful Handlebars.js.
-
I’m all ears. Shoot me an email or a PM.
Copyright © 2011 Burke Libbey. See LICENSE.txt for further details.