Guard::MustacheJs concatenates all your mustache templates into a single javascript file. Your templates are stored in a global object named ‘mustache_templates’ by default.
First, install guard. Then, install the gem:
gem install guard-mustachejs
Or you can add it to your Gemfile:
gem 'guard-mustachejs'
To add the guard definition for mustachejs to your Guardfile, execute:
guard init coffeescript
By default, guard-mustachejs will look for mustache templates in app/mustache/*/.html. Let’s say you have the following files:
app/mustache/home/banner.html app/mustache/table/row.html
Guard will pull these files into a single javascript resource located at:
public/javascripts/mustache-templates.js
Assuming you have included mustache.js, you can then render your templates with:
Mustache.to_html(mustache_templates.home.banner, root) Mustache.to_html(mustache_templates.table.row, root)
By default, guard-mustachejs will look for mustache templates in app/mustache/*/.html. The default guard clause looks like this:
guard 'mustachejs' do watch(%r{app/mustache/(.+)\.html}) end
You can change the output javascript file path and the name of the global variable used to store the templates. You can even have multiple template targets, like so:
guard 'mustachejs', :output => 'public/javascripts/js-templates.js', :variable => 'js_templates' do watch(%r{app/mustache/(.+)\.js}) end guard 'mustachejs', :output => 'public/javascripts/html-templates.js', :variable => 'html_templates' do watch(%r{app/mustache/(.+)\.html}) end
Thanks to the Guard Team and to all the guard gem authors, especially Michael Kessler for his excellent guard-coffeescript gem, which was the inspiration for this one.