Adds a template to Sinatra for rendering CoffeeScript
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib/sinatra
README.rdoc
sinatra-coffee.gemspec

README.rdoc

Sinatra::Coffee

Adds support for compiling a CoffeeScript string or file into JavaScript using Sinatra's template rendering support.

CoffeeScript is a language that offers a much simpler and more concise way to write JavaScript.

Installation

gem install sinatra-coffee

If you don't have CoffeeScript installed, here's how (as of 2010-08-07).

First, install Node.js:

wget http://nodejs.org/dist/node-v0.1.103.tar.gz
tar -zxf node-v0.1.103.tar.gz
cd node-v0.1.103
./configure
make
sudo make install

Now, install and test CoffeeScript:

git clone http://github.com/jashkenas/coffee-script.git
cd coffee-script
./bin/cake test
./bin/cake install

Examples

This will render the file clientstuff.coffee found in Sinatra's :views directory:

get '/clientstuff.js' do
  response['Content-Type'] = "application/javascript"
  coffee :clientstuff
end

Of course, you can always pass a string to compile verbatim CoffeeScript:

get '/alert-time.js' do
  response['Content-Type'] = "application/javascript"
  coffee 'now = new Date; alert "The time now is #{now}"'
end

And, just as an example to any new Sinatra + CoffeeScript folks, here's how you'd serve up an HTML page that references the CoffeeScript-generated .js file:

get '/test.html' do
  response['Content-Type'] = "text/html"
  '<script src="/alert-time.js"></script>'
end