Allow helper methods to use blocks in erb views

This commit is contained in:
Matias Käkelä 2009-02-18 20:26:06 +02:00 committed by Ryan Tomayko
parent 004468370b
commit f3ed6e4dd9
2 changed files with 18 additions and 1 deletions

View File

@ -282,7 +282,7 @@ module Sinatra
def render_erb(template, data, options, &block)
data = data.call if data.kind_of? Proc
instance = ::ERB.new(data)
instance = ::ERB.new(data, nil, nil, '@_out_buf')
locals = options[:locals] || {}
locals_assigns = locals.to_a.collect { |k,v| "#{k} = locals[:#{k}]" }
src = "#{locals_assigns.join("\n")}\n#{instance.src}"

View File

@ -47,4 +47,21 @@ describe "ERB Templates" do
assert ok?
assert_equal "ERB Layout!\nHello World\n", body
end
it "renders erb with blocks" do
mock_app {
def container
@_out_buf << "THIS."
yield
@_out_buf << "SPARTA!"
end
def is; "IS." end
get '/' do
erb '<% container do %> <%= is %> <% end %>'
end
}
get '/'
assert ok?
assert_equal 'THIS. IS. SPARTA!', body
end
end