JST Generator
A gem for automatically generating a jst.js
file full of compiled templates.
Currently supports Handlebars and Underscore out of the box, with more to come.
Installation
Add this line to your application's Gemfile:
gem 'jstgenerator'
And then execute:
$ bundle
Usage
Within a Ruby app (such as Sinatra)
Within a Ruby app, you can do something like this:
# or JstGenerator::Underscore.new for Underscore templates
JstGenerator::Handlebars.new({
:dir_glob => "path/to/templates/*.hb",
:jst_path => "output/jst.js"
}).generate
This will:
- create
output/jst.js
that creates awindow.JST
object - loop through each file the
dir_glob
matches - and add that to
window.JST
An example jst.js
file looks like so:
window.JST = {};
window.JST["test"] = Handlebars.compile("My template {{title}}")
You can then load in jst.js
and compile that template:
var html = window.JST["test"]({ title: "Hello World" });
The advantage being the compilation step (Handlebars.compile
) is only run once and that you can keep your templates in their own file, rather than within some HTML.
Without a Ruby app (command line)
$ gem install jstgenerator
$ jstgenerator [type] [glob] [output]
For example:
$ jstgenerator handlebars "views/templates/**/*.hb" lib/js/jst.js
Make sure you quote the glob string, else your shell may well expand it for you, which you don't want in this case.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Changelist
0.1.0
- added CLI tool
0.0.1
- initial release