ERB in place
This commit is contained in:
parent
44d2b66d3c
commit
becd6d8ab0
|
@ -152,26 +152,45 @@ module Sinatra
|
|||
|
||||
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 @@ module Sinatra
|
|||
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
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -6,7 +6,7 @@ context "Templates (in general)" do
|
|||
|
||||
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 @@ context "Templates (in general)" do
|
|||
|
||||
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
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
You rock <%= @name %>!
|
|
@ -0,0 +1,2 @@
|
|||
<%= @title %>
|
||||
Hi <%= @content %>
|
|
@ -0,0 +1 @@
|
|||
This is foo!
|
|
@ -0,0 +1 @@
|
|||
x <%= @content %> x
|
Loading…
Reference in New Issue