1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Add coffee helper method. Tilt supports CoffeeScript again, but it was not as easy to use as sass or scss, and not documented. Tests and documentation (English and German) included.

This commit is contained in:
Konstantin Haase 2010-09-12 23:09:10 +02:00
parent 8ce74b3ad2
commit f58d015b6d
5 changed files with 98 additions and 0 deletions

View file

@ -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 file

@ -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 file

@ -359,6 +359,11 @@ module Sinatra
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

64
test/coffee_test.rb Normal file
View file

@ -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

1
test/views/hello.coffee Normal file
View file

@ -0,0 +1 @@
alert "Aye!"