Permalink
Browse files

Add coffee helper method. Tilt supports CoffeeScript again, but it wa…

…s not as easy to use as sass or scss, and not documented. Tests and documentation (English and German) included.
  • Loading branch information...
1 parent 8ce74b3 commit f58d015b6d3512ba739d6bea1722262a3431a9a8 @rkh committed Sep 12, 2010
Showing with 98 additions and 0 deletions.
  1. +13 −0 README.de.rdoc
  2. +15 −0 README.rdoc
  3. +5 −0 lib/sinatra/base.rb
  4. +64 −0 test/coffee_test.rb
  5. +1 −0 test/views/hello.coffee
View
@@ -424,6 +424,19 @@ Das markaby gem wird benötigt um Markaby-Templates rendern zu können:
Dieser Code rendert <tt>./views/index.mab</tt>.
+=== CoffeScript-Templates
+
+Das coffee-script gem und das `coffee`-Programm werden benötigt um CoffeScript-Templates rendern zu können:
+
+ ## coffee-script muss eingebunden werden
+ require 'coffee-script'
+
+ get '/application.js' do
+ coffee :application
+ end
+
+Dieser Code rendert <tt>./views/application.coffee</tt>.
+
=== Inline-Templates
get '/' do
View
@@ -412,6 +412,21 @@ The markaby gem/library is required to render Markaby templates:
Renders <tt>./views/index.mab</tt>.
+=== CoffeeScript Templates
+
+The coffee-script gem/library and the `coffee` binary are required to render
+CoffeeScript templates:
+
+ ## You'll need to require coffee-script in your app
+ require 'coffee-script'
+
+ get '/application.js' do
+ content_type 'text/javascript', :charset => 'utf-8'
+ coffee :application
+ end
+
+Renders <tt>./views/application.coffee</tt>.
+
=== Inline Templates
get '/' do
View
@@ -359,6 +359,11 @@ def markaby(template, options={}, locals={})
render :mab, template, options, locals
end
+ def coffee(template, options={}, locals={})
+ options[:layout] = false
+ render :coffee, template, options, locals
+ end
+
private
def render(engine, data, options={}, locals={}, &block)
# merge app-level options
View
@@ -0,0 +1,64 @@
+require File.dirname(__FILE__) + '/helper'
+
+begin
+require 'coffee-script'
+
+class CoffeeTest < Test::Unit::TestCase
+ def coffee_app(&block)
+ mock_app {
+ set :views, File.dirname(__FILE__) + '/views'
+ get '/', &block
+ }
+ get '/'
+ end
+
+ it 'renders inline Coffee strings' do
+ coffee_app { coffee "alert 'Aye!'\n" }
+ assert ok?
+ assert_equal "(function() {\n alert('Aye!');\n})();\n", body
+ end
+
+ it 'renders .coffee files in views path' do
+ coffee_app { coffee :hello }
+ assert ok?
+ assert_equal "(function() {\n alert(\"Aye!\");\n})();\n", body
+ end
+
+ it 'ignores the layout option' do
+ coffee_app { coffee :hello, :layout => :layout2 }
+ assert ok?
+ assert_equal "(function() {\n alert(\"Aye!\");\n})();\n", body
+ end
+
+ it "raises error if template not found" do
+ mock_app {
+ get('/') { coffee :no_such_template }
+ }
+ assert_raise(Errno::ENOENT) { get('/') }
+ end
+
+ it "passes coffee options to the coffee engine" do
+ coffee_app {
+ coffee "alert 'Aye!'\n",
+ :no_wrap => true
+ }
+ assert ok?
+ assert_equal "alert('Aye!');", body
+ end
+
+ it "passes default coffee options to the coffee engine" do
+ mock_app {
+ set :coffee, :no_wrap => true # default coffee style is :nested
+ get '/' do
+ coffee "alert 'Aye!'\n"
+ end
+ }
+ get '/'
+ assert ok?
+ assert_equal "alert('Aye!');", body
+ end
+end
+
+rescue
+ warn "#{$!.to_s}: skipping coffee tests"
+end
View
@@ -0,0 +1 @@
+alert "Aye!"

0 comments on commit f58d015

Please sign in to comment.