Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ERB in place

  • Loading branch information...
commit becd6d8ab0d5a8dbeddf3c948d4c73e88466f2c0 1 parent 44d2b66
Blake Mizerany authored
41 lib/sinatra.rb
View
@@ -152,26 +152,45 @@ def headers(header = nil)
module RenderingHelpers
+ def text(content, options={})
+ render(content, options.merge(:renderer => :text, :ext => :html))
+ end
+
+ def erb(content, options={})
+ render(content, options.merge(:renderer => :erb, :ext => :erb))
+ end
+
def render(content, options={})
- renderer = options.delete(:renderer) || :text
+ options[:layout] ||= :layout
template = resolve_template(content, options)
- @content = send(renderer, template)
+ @content = evaluate_renderer(template, options)
layout = resolve_layout(options[:layout], options)
- @content = send(renderer, layout) if layout
+ @content = evaluate_renderer(layout, options) if layout
@content
end
private
- def text(content, options={})
- case content
+ def evaluate_text(content, options={})
+ instance_eval(%Q{"#{content}"})
+ end
+
+ def evaluate_erb(content, options={})
+ require 'erb'
+ ERB.new(content).result(binding)
+ end
+
+ def evaluate_renderer(content, options={})
+ renderer = "evaluate_#{options[:renderer] || :text}"
+ result = case content
when String
- instance_eval(%Q{"#{content}"})
+ content
when Proc
- instance_eval(&content)
+ content.call
when File
- instance_eval(%Q{"#{content.read}"})
+ content.read
end
+ send(renderer, result, options)
end
def resolve_template(content, options={})
@@ -194,11 +213,7 @@ def resolve_layout(name, options={})
end
def filename_for(name, options={})
- (options[:views_directory] || 'views') + "/#{name}.#{ext}"
- end
-
- def ext
- :html
+ (options[:views_directory] || 'views') + "/#{name}.#{options[:ext]}"
end
def layouts
88 test/erb_test.rb
View
@@ -0,0 +1,88 @@
+require File.dirname(__FILE__) + '/helper'
+
+context "Erb" do
+
+ setup do
+ Sinatra.application = nil
+ end
+
+ context "without layouts" do
+
+ setup do
+ Sinatra.application = nil
+ end
+
+ specify "should render" do
+
+ get '/no_layout' do
+ erb '<%= 1 + 1 %>'
+ end
+
+ get_it '/no_layout'
+ should.be.ok
+ body.should == '2'
+
+ end
+ end
+
+ context "with layouts" do
+
+ setup do
+ Sinatra.application = nil
+ end
+
+ specify "can be inline" do
+
+ layout do
+ %Q{This is <%= @content %>!}
+ end
+
+ get '/lay' do
+ erb 'Blake'
+ end
+
+ get_it '/lay'
+ should.be.ok
+ body.should.equal 'This is Blake!'
+
+ end
+
+ specify "can use named layouts" do
+
+ layout :pretty do
+ %Q{<h1><%= @content %></h1>}
+ end
+
+ get '/pretty' do
+ erb 'Foo', :layout => :pretty
+ end
+
+ get '/not_pretty' do
+ erb 'Bar'
+ end
+
+ get_it '/pretty'
+ body.should.equal '<h1>Foo</h1>'
+
+ get_it '/not_pretty'
+ body.should.equal 'Bar'
+
+ end
+
+ specify "can be read from a file if they're not inlined" do
+
+ get '/foo' do
+ @title = 'Welcome to the Hello Program'
+ erb 'Blake', :layout => :foo_layout,
+ :views_directory => File.dirname(__FILE__) + "/views"
+ end
+
+ get_it '/foo'
+ body.should.equal "Welcome to the Hello Program\nHi Blake\n"
+
+ end
+
+ end
+
+
+end
60 test/layouts_test.rb
View
@@ -1,60 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-
-context "Layouts (in general)" do
-
- setup do
- Sinatra.application = nil
- end
-
- specify "can be inline" do
-
- layout do
- %Q{This is #{@content}!}
- end
-
- get '/lay' do
- render 'Blake'
- end
-
- get_it '/lay'
- should.be.ok
- body.should.equal 'This is Blake!'
-
- end
-
- specify "can use named layouts" do
-
- layout :pretty do
- %Q{<h1>#{@content}</h1>}
- end
-
- get '/pretty' do
- render 'Foo', :layout => :pretty
- end
-
- get '/not_pretty' do
- render 'Bar'
- end
-
- get_it '/pretty'
- body.should.equal '<h1>Foo</h1>'
-
- get_it '/not_pretty'
- body.should.equal 'Bar'
-
- end
-
- specify "can be read from a file if they're not inlined" do
-
- get '/foo' do
- @title = 'Welcome to the Hello Program'
- render 'Blake', :layout => :foo_layout,
- :views_directory => File.dirname(__FILE__) + "/views"
- end
-
- get_it '/foo'
- body.should.equal "Welcome to the Hello Program\nHi Blake\n"
-
- end
-
-end
14 test/template_test.rb
View
@@ -6,7 +6,7 @@
get '/from_file' do
@name = 'Alena'
- render :foo, :views_directory => File.dirname(__FILE__) + "/views"
+ erb :foo, :views_directory => File.dirname(__FILE__) + "/views"
end
get_it '/from_file'
@@ -15,4 +15,16 @@
end
+ specify "use layout.ext by default if available" do
+
+ get '/layout_from_file' do
+ erb :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test"
+ end
+
+ get_it '/layout_from_file'
+ should.be.ok
+ body.should.equal "x This is foo! x \n"
+
+ end
+
end
1  test/views/foo.erb
View
@@ -0,0 +1 @@
+You rock <%= @name %>!
2  test/views/foo_layout.erb
View
@@ -0,0 +1,2 @@
+<%= @title %>
+Hi <%= @content %>
1  test/views/layout_test/foo.erb
View
@@ -0,0 +1 @@
+This is foo!
1  test/views/layout_test/layout.erb
View
@@ -0,0 +1 @@
+x <%= @content %> x
Please sign in to comment.
Something went wrong with that request. Please try again.